Linux operational ENGINEER1.4 (Shell Scripting Basics)

Source: Internet
Author: User

ENGINEER1.4

Shell Script Basics

Understanding the shell environment

How bash shells are used

Interactive:

--manual intervention, high degree of intelligence

----explain execution by article, low efficiency

Non-interactive:

-Need to design in advance, the difficulty of intelligence is big;

--Batch execution, high efficiency;

-Easy to implement in the background and quietly;

What is Shell scripting : Designing executable statements in advance to complete a specific task's files

--Explanatory program

--Sequential, batch execution

Standardize the general composition of shell scripts:

#! Environmental statement

#注释文本

Executable code

--------------------------------------------------------------------------------------------------------------- -------------------

Example 1:

Writing the helloworld.sh greeting script

    1. Write script code:

      #vim/root/helloworld.sh

      #! /bin/bash

      echo "Hello World"

    2. Add x Execute Permissions

      #chmod +x/root/helloworld.sh

    3. Run a script test

      #/root/helloworld.sh

Example 2:

Writing System Information Report scripts

    1. Write script code:

      #vim/root/1.sh

#!/bin/bash

Cat/etc/redhat-release

Uname-r

Hostname

2. Add x Execute Permissions

# chmod +x/root/1.sh

3. run the script test:

#/root/1.sh

--------------------------------------------------------------------------------------------------------------- -----------------------

Simple Scripting Tips

Application of redirected output

: Collect only the correct 2>: Collect only error &>: Collect all

This example requires writing a script/root/out.sh with the following features:

    1. Execute this script to show I Love study!!

    2. The execution/root/out.sh 2> Err.log should not be displayed, but the contents of the view Err.log file are I Love study!!

Steps:

    1. #vim/root/out.sh

      #!/bin/bash

      #echo "I love study!!" >&2

    2. #chmod-X/root/out.sh

    3. #/root/out.sh

    4. #/root/out.sh 2>err.log

    5. #cat Err.log

--------------------------------------------------------------------------------------------------------------- -------------------------

Definition and use of variables

Store a value that may change with an immutable name

Reference variable Value: $ variable name with {} to define easily confusing names

Types of variables:

vary depending on the purpose of the variable:

Environment variables: variable names are generally capitalized to set the user, System environment (PATH)

positional variables:bash built-in, command-line arguments provided when storing built-in scripts $n, n is ordinal $ ... ${10}

Predefined variables:bash built-in, special values that can be called directly, cannot be modified directly

$#: Number of position variables loaded $*: The value of all positional variables $?: state value after exit of program, 0 indicates normal, other exception

Custom variables: user-owned design, modification and use

Cases:

Requires writing a script/root/myhead with the following features:

This script can receive 2 positional parameters and can be executed in the following format

After this script executes, you can display "you have provided a total of $# parameters," and then the next line shows "the file's top $:", immediately following the next line to start outputting the first few lines of the corresponding file


Step one : write/root/myuseradd to add a user's script

#vim/root/myuseradd

#! /bin/bash

echo "provides a total of $ #个参数"

echo "Username is $ $, password is $"

Useradd $

echo "$" | passwd--stdin $

Two. Add Execute Permissions

#chmod-X/root/myuseradd.sh

--------------------------------------------------------------------------------------------------------------- -------------------------------

Common Test options:

    • File status detection-F,-D,-E,-R,-W,-X

    • Integer values compare-gt,-ge,-eq,-ne,-lt,-le

    • string comparison = =,! =

    • Take the counter action!

Multi-Branch If selection structure:

If condition test action 1 then command sequence 1

elif condition Test Action 2 then command sequence 2

else Command Sequence 3

Fi

Cases:

This example requires the creation of a/root/foo.sh script on the virtual machine Server0, with the following objectives:

    1. When running/root/foo.sh Redhat, the output is Fedora

    2. When running/root/foo.sh fedora, the output is Redhat

    3. When no parameters or parameters are not redhat or fedora, the error output produces the following information:/root/foo.sh Redhat|fedora

1) Writing script code

[Email protected] ~]# vim/root/foo.sh
#!/bin/bash
If ["$" = "Redhat"]
Then
echo "Fedora"
elif ["$" = "Fedora"]
Then
echo "Redhat"
Else
echo "/root/foo.sh Redhat|fedora" >&2
Fi

2) Add x Execute Permissions

[Email protected] ~]# chmod +x/root/foo.sh


Step two: Test foo.sh judgment Script

1) test conditions that provide the correct parameters

[Email protected] ~]#/root/foo.sh redhat Fedora
[Email protected] ~]#/root/foo.sh Fedora Redhat

2) test to provide unexpected parameters

[Email protected] ~]#/root/foo.sh Ubuntu
/root/foo.sh Redhat|fedora

3) test conditions that do not provide parameters

[Email protected] ~]#/root/foo.sh
/root/foo.sh Redhat|fedora




This article is from the Linux OPS blog, so be sure to keep this source http://13401400.blog.51cto.com/13391400/1979741

Linux operational ENGINEER1.4 (Shell Scripting Basics)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.