Linux 9 Shell

Source: Internet
Author: User
Tags echo command

Course outline 『
Shell programming syntax
Shell script debugging
Shell application instance

A simple shell program
Shell Structure:
1 .#! Specifies the shell for executing the script
2. # comment rows
3. command and control structure
To create a shell program:
Step 1: create a file that contains commands and control structures.
Step 2: Modify the permission of the file so that it can be executed.
Use chmod U + x
Step 3: Execute./Example
(You can also use "Sh example" for execution)
Shell variable
Variable: a method used by Shell to transmit data. It represents the symbolic name of each value.
Shell has two types of variables: temporary variables and permanent variables.
Temporary variables are defined within the shell program. The scope of use is limited to the program that defines them and are invisible to other programs. Including user-defined Changes
Volume and location variable. A permanent variable is an environment variable and its value does not disappear with the execution of the shell script.
User-Defined variables
A user-defined variable starts with a letter or underline and is composed of letters, numbers, or underscores.
. Variable name length is not limited. When using the variable value, you must add the prefix "$" before the variable name ".
Set and use variables
Set variables: Typically, variables are named with uppercase letters. The variable name must start with an English letter and cannot contain numbers.
Variable assignment: there should be no spaces on both sides of the value "=. Assign values when defining. For example, if num = 1, assign the execution result of a command to a variable, for example, time = 'date'
Assign a variable to another variable, for example, a = $ B. Run the echo command to view the variable value. Example: Echo $
List all variables:
# Set
Variables that contain multiple words:
$ Name = Mike Ron
When an error occurs, change it to: $ name = "Mike Ron" or $ name = 'Mike Ron'
Difference between single quotes and double quotes:
# $ Abc = '$ name Junior'
# Echo $ ABC
$ Name Junior
The content between single quotes is invariably specified to the variable.
Delete variable: # unset name
Location variables and special Variables
Shell interprets the first part of the command line as the command name and the other part as the parameter when executing the USER command. By appearance
A location parameter is a location parameter.
Example: ls-l file1 file2 file3
$0 name of the program LS-l
$ N the nth parameter value of this program, n = 1-9
Special Variables
$ * All parameters of this program
$ # Number of parameters of this program
$ PID of the program
$! PID used to execute the previous background command
$? The Return Value of the previous command.
Shell commands
* 1 read command: read data from the keyboard and assign it to variables such as: Read Username
Example of Read:
#! /Bin/sh
Read first second third
Echo "the first parameter is $ first"
Echo "the second parameter is $ second"
Echo "The third parameter is $ third"
Expr command
Shell variable arithmetic operations:
Expr command: arithmetic operation on Integer Variables
Example: expr 3 + 5
Expr $ var1-5
Expr $ var1/$ var2
Expr $ var3 \ * 10
Complex expr commands
Complex operations:
Expr 'expr 5 + 7'/$ var4
Assign the calculation result to the variable:
Var4 = 'expr $ var1/$ var2'
Expr command

#!/bin/sh    a=10    b=20    c=30    value1=`expr $a + $b + $c`    echo "The value of value1 is $value1"    value2=`expr $c / $b`    echo "The value of value2 is $value2"    value3=`expr $c \* $b`    echo "The value of value3 is $value3"    value4=`expr $a + $c / $b`    echo "The value of value4 is $value4“

Variable test statement
Variable test statement: used to test whether the variables are equal, empty, and file type.
Format: Test conditions
Test Range: integer, string, and file
* 1 string test:
Test str1 = str2 test whether the string is equal
Test str1! = Str2 test whether the string is not equal
Test str1 test whether the string is not empty
Test-N str1 test string is not empty
Test-Z str1 test whether the string is null
* 2 integer test:
Test int1-EQ int2 test whether the integer is equal
Test int1-ge int2 test whether int1> = int2
Test int1-GT int2 test whether int1> int2
Test int1-Le int2 test int1 <= int2
Test int1-lt int2 test whether int1 is <int2
Test int1-ne int2 test whether the integer is not equal
* 3 File test:
Test-D file specifies whether the file is a directory
Test-F file specifies whether the file is a regular file
Test-X file specifies whether the file is executable
Test-r file specifies whether the file is readable.
Test-W file specifies whether the file is writable.
Test-a file specifies whether the file exists
Whether the size of the test-s file is not 0
Variable test statements are generally not used independently. They are generally used as test conditions for if statements, for example:
If test-d $1 then
...
Fi
The variable test statement can be simplified using [], as shown in figure
Test-d $1 is equivalent to [-d $1].

#!/bin/sh if [ $# -ne 2 ]; then echo "Not enough parameters" exit 0 fi if [ $1 -eq $2 ]; then echo "$1 equals $2" elif [ $1 -lt $2 ]; then echo "$1 littler than $2" elif [ $1 -gt $2 ]; then echo "$1 greater than $2" fi

Flow Control statement
Flow Control statement: used to control the flow of Shell programs
Exit statement: exit the program and return a return code. If the return code is 0, the program Exits normally. If the return code is not 0, the program exits abnormally.
Example: Exit 0

* 1 Use of if
If... then... Fi statement, for example:

#!/bin/sh   if [ -x /etc/rc.d/init.d/httpd ]    then   /etc/rc.d/init.d/httpd restart  fi

* 2 more complex if statements:
If condition 1 then
Command 1
Elif condition 2 then
Command 2
Else
Command 3
Fi

* 3 combination of multiple conditions:
-A: logical and. The result is true only when both conditions are true.
-O: logical or. If either of the two conditions is true, the result is true.

* 4 for... done statement
Format: for variable in name table
Do
Command list
Done
Example:

 #!/bin/sh for DAY in Sunday Monday Tuesday Wednesday  Thursday Friday Saturday do  echo "The day is : $DAY" don

* 5 select variable in keywords
Do
Command 1
......
Command n
Done
Select makes every item in the keyword into a similar form and executes the commands between do and done in interactive mode.

* 6 case... esac statement, format:
Case variable in
String 1) command list 1
;;
...
String N) command list n
;;
Esac
* 7 while statement, format:

While condition do Command done #! /Bin/sh num = 1 while [$ num-le 10] Do sum = 'expr $ num \ * $ num' echo $ sum num = 'expr $ num + 1' done

* 8 until statement, format:
Until Condition
Do
Command
Done
Until is similar to a while loop. The difference is that until continues execution only when the return value of the condition is false.

* 9 skip loop: Break and continue
Break: jump out of the entire Loop
Continue: Skip this loop and proceed to the next loop
* 10
Shift command: shifts the parameter left. Each time it is executed, the parameter sequence shifts left one position sequentially. The value of $ # is reduced by 1,
This parameter is used to process each parameter separately. The removed parameter is no longer available.

* 11 function definition:
Function Name ()
{
Command Sequence
}
Function call: ()
Function name parameter 1 parameter 2...
* 12. variables in the function:
All variables are global variables without local variables.
Parameters in a function: When a function is called, parameters can be passed. $1, $2... can be referenced in a function.

* 13 sh-x script
This will execute the script and display the values of all variables.
Sh-N script
If the script is not executed, it only checks the syntax mode and returns all syntax errors.
* 14 awk command Application
Awk-F domain separator 'COMMAND'
Example:
1. Check users with UID 0 in the system.
Awk-F: '$3 = 0 {print $1}'/etc/passwd
2. Check users with blank passwords in the system
Awk-F: 'length ($2) = 0 {print $1} '/etc/shadow
Knowledge Point Summary
Understanding the basic syntax of shell programming
Understanding how to use system commands to compile application scripts
Understanding shell programming and Debugging commands

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.