1. Possible means of implementation
Execute under linux Method of sh file
The. sh file is a text file. If you want to execute it, you need to use Chmod a + X XXX sh to give executable permissions.
2. Beginning: #/ bin/sh
shell programs must start with "#! / bin/sh". shell # generally means annotation, so it is often thought that "#!" It's also a comment, but it's not.
"#! / bin/sh" is a declaration of the shell, which type of shell you are using and where its path is.
#!/ bin / refers to the use of this script bin/sh.
# Is a special indicator, followed by the path of the shell that explains the script. If it is not declared, the script will be executed in the default shell. The default shell is defined by the user's system as the execution shell script. If the script is written to run in Korn shell KSH and the default shell script is C shell csh, the script is likely to fail in the execution process. Therefore, it is suggested that you take "#! / bin/sh" as the main function of C language, and you must write a shell to make the shell program more rigorous.
3. Variables
Variables must be used in other programming languages. In shell programming, all variables are composed of strings, and there is no need to declare variables. To assign a value to a variable, write:
#!/bin/sh #Assign values to variables: a="hello world" # Now print the contents of variable a: echo "A is:" echo $a
Sometimes variable names are easily confused with other words, such as:
num=2 echo "this is the $numnd"
This does not print "this is the 2nd", but only "this is the", because the shell will search for the value of the variable numnd, but this variable has no value. Therefore, you can use curly braces to tell the shell that we want to print the num variable:
num=2 echo "this is the ${num}nd"
this is the 2nd“
4. Shell command and process control
The following commands can be used in shell scripts:
Unix commands
Although arbitrary unix commands can be used in shell scripts, there are some relatively more commonly used commands. These commands are usually used for file and text operations.
For example:
echo "some text" #Print text content on screen ls #File list cp sourcefile destfile #File copy mv oldname newname #Rename or move files rm file #Delete file grep 'pattern' file #Search for strings in files, such as grep 'searchstring' file txt cat file.txt #Output file contents to standard output device (screen) read var #Displays the user input and assigns the input to the variable
Concept: pipeline, redirection and backtick (backslash)
The pipe "|" takes the output of one command as the input of another command.
grep "hello" file.txt | wc -l
The above code is expressed as: in file Txt and calculate the number of rows containing "hello". Here, the output of grep command is used as the input of wc command.
Redirect: output the results of the command to a file instead of standard output (screen).
>Write file and overwrite old file
>>Append to the end of the file to retain the contents of the old file.
Anti Dash "`": use the anti dash to take the output of one command as a command line parameter of another command.
find . -mtime -1 -type f -print
The above statement is used to find files modified in the past 24 hours (- mtime -2 means the past 48 hours). If you want to package all the found files, you can use the following linux script:
#!/bin/sh # The ticks are backticks (`) not normal quotes ('): tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`
Process control
if
"If" expression, if the condition is true, execute the part after then:
if ....; then .... elif ....; then .... else .... fi #Note that it ends with fi
In most cases, you can use test commands to test conditions. For example, you can compare strings, judge whether files exist and are readable, and so on
Test conditions
"[]" is usually used to represent test conditions. Note that the space here is very important. Make sure the space in square brackets.
[ -f "somefile" ] #Determine whether the file exists [ -d "testResults/" ] #Determine whether the directory testResults / exists [ -x "/bin/ls" ] #Determine whether the / bin/ls file exists and has executable permissions [ -n "$var" ] #Determine whether the $var variable has a value [ "$a" = "$b" ] #Determine whether $a and $b are equal
Shortcut operator
Familiar with C language, you may like the following expressions:
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors"
Here "& &" is a shortcut operator. If the expression on the left is true, the statement on the right will be executed. Of course, the above expression can also be regarded as and operation in logical operation.
Similarly, the or operation "|" is also available in shell programming:
#!/bin/sh mailfolder=/var/spool/mail/james [ -r "$mailfolder" ]' '{ echo "Can not read $mailfolder" ; exit 1; } #I think the 'here' should be|| echo "$mailfolder has mail from:" grep "^From " $mailfolder
The script first determines whether the mailfolder is readable. If readable, print the "From" line in the file. If it is unreadable or the operation takes effect, the script exits after printing the error message. The problem here is that we must have two commands:
◆ print error message
◆ exit procedure
We use curly braces to put two commands together as one command in the form of an anonymous function. General functions will be mentioned below.
We can do anything with an if expression without the and and or operators, but it's much more convenient to use the and or operator.