Shell Script of Linux Basics
1 Shell scipt
A program written in the instruction and basic program design structure to complete a complex process
1.1 Program Writing
Copy Code code as follows:
#!/bin/bash
# program:
# This program shows ' Hello wrold ' in your screen.
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
Echo-e "Hello world!\a\n"
Exit 0
The first line #!/bin/bash the shell type used, different shell syntax may be different, so to explain what kind of shell is used
Other # Start presentation comments, comments generally need to be explained
Program features
Version history
Author and contact method
Set the path variable so that you can directly invoke the command under the appropriate path
Program Body part
Exit 0 indicates a successful program execution, returning 0 to the environment
1.2 Program execution
Bash $bash sh01.sh #如果用sh sh01.sh and SH is not pointing to bash, then the syntax in sh01.sh will be inconsistent because #sh去解释了bash语法写的shell script, for this program, if # $type SH # Get the SH is hashed (/bin/sh) #那么会输出-e Hello world! instead of Hello world!
Copy Code code as follows:
$./xxx.sh $chmod +x sh01.sh $./sh01.sh
SOURCE $ source Sh01.sh
Note: The difference between bash and source is that when executed with bash, the shell script is actually a new bash subroutine in the parent program bash, executed in this subroutine, and when the program is done, the shell The variables defined in the script disappear with the end of the subroutine, and when executed with source, it is executed in the parent program bash, and the variables defined in the shell script are still in.
2 Simple Shell Exercises
2.1 Cases 1 Receive user input
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to read user ' s input
# Site:www.jb51.net
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
Read-p "Your-Name:" FirstName # Tell user to input
Read-p "Your Last Name:" LastName # Tell user to input
Echo-e "\nyour full name: $firstname $lastname"
Exit 0
Call:
Copy Code code as follows:
$ bash sh02.sh
Your-Name:minix
Your last name:007
Your full Name:minix 007
2.2 Cases 2 files with similar names by date
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to create files according to date
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
# get filename from user
Echo-e "I'll use the ' touch ' to create three files."
Read-p "Please input your filename:" Tmpfilename
# Prevent the user input [Enter]
# Check whether filename exists or not
filename=${tmpfilename:-"FileName"}
# get the final filename according to date
date1=$ (Date--date= ' 2 days ago ' +%y%m%d] # Date of 2 days ago
date2=$ (Date--date= ' 1 days ago ' +%y%m%d) # Date of yesterday
date3=$ (date +%y%m%d) # Date of today
FILENAME1=${FILENAME}${DATE1}
FILENAME2=${FILENAME}${DATE2}
Filename3=${filename}${date3}
# Create File
Touch "$filename 1"
Touch "$filename 2"
Touch "$filename 3"
Exit 0
Call:
Copy Code code as follows:
$ bash sh03.sh
I'll use the ' touch ' to create three files.
Please input your filename:whoknows
$ ls w*
WhoKnows20130201 WhoKnows20130202 WhoKnows20130203
3 Judgment Type
3.1 Whether the test file exists
TEST-E filename returns 0 or 1 depending on whether the filename exists, and then the echo displays the result:
Copy Code code as follows:
$ test-e sh01.sh && echo "Exists" | | echo "NOT EXISTS"
Exists
$ test-e sh0x.sh && echo "Exists" | | echo "NOT EXISTS"
NOT EXISTS
3.2 Test Common options
3.2.1 File Type
Copy Code code as follows:
-E File:file is present
-F File:file exists and is a file
-D File:file exists and is a directory
3.2.2 Permissions
-R file:file permission to read
3.2.3 file old and new comparison
-nt File1 File2:file1 is newer than file2
3.2.4 integers, strings, multiple conditional judgments
-Z string:string is empty
Example: output specified file types and attributes
Copy Code code as follows:
#!/bin/bash
# program:
# this are used to output type and permission of the target file
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
# get filename from user
Echo-e "Input Name of the file" you want to check.\n "
Read-p "FileName:" filename
Test-z $filename && echo "You must input a filename." && exit 0
# Check whether the file exists or not
Test! -E $filename && echo "The file ' $filename ' does not exists ' && exit 0
# Check type and permission of the file
Test-f $filename && filetype= "regular file"
test-d $filename && filetype= "directory"
Test-r $filename && perm= "readable"
Test-w $filename && perm= "$perm writable"
Test-x $filename && perm= "$perm executable"
# Output Result
echo "The filename: $filename is a $filetype"
echo "and Permissions are: $perm"
Exit 0
Call:
Copy Code code as follows:
$ bash sh04.sh
Input name of the file that is want to check.
Filename:sh01.sh
The filename:sh01.sh is a regular file
and Permissions are:readable writable executable
3.3 Use [] to judge
Testing whether a file exists
Copy Code code as follows:
$ [E "sh01.sh"]; echo $?
0
$ [E "sh0x.sh"]; echo $?
1
Note [] spaces must have
This method and test of the Test-e "sho1.sh"; echo $? It's consistent.
4 Shell Script Parameters
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to ouput parameter of the shell script
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
echo "The script ' s name is ==> $"
echo "Total parameter number is ==> $#"
# Check Whether number of the parameter is less than 2
["$#"-lt 2] && echo "The number of parameter is less than 2.Stop here." && Exit 0
echo "The whole parameter is ==> ' $@ '"
echo "The parameter is ==> $"
echo "The parameter is ==> $"
Exit 0
Call:
Copy Code code as follows:
$ bash sh05.sh 1a 2b 3c 4d
The script ' s name is ==> sh05.sh
Total parameter number is ==> 4
The whole parameter is ==> ' 1a 2b 3c 4d '
The parameter is ==> 1a
The parameter is ==> 2b
Note: You can see from the above program how the preset variables related to parameters represent
5 conditional expression
5.1 If structure
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to show if else expression
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
Read-p "Please input [y/n]" Choice
If ["$choice" = = "Y"] | | ["$choice" = = "Y"];then
echo "OK, continue"
Exit 0
Fi
If ["$choice" = = "N"] | | ["$choice" = = "N"];then
echo "Oh, Interupt"
Exit 0
Fi
Exit 0
Call:
Copy Code code as follows:
$ bash sh06.sh
Please input [y/n]y
OK, continue.
$ bash sh06.sh
Please input [y/n]n
Oh, Interupt
5.2 If Else structure
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to show if else expression
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
Read-p "Please input [y/n]" Choice
If ["$choice" = = "Y"] | | ["$choice" = = "Y"];then
echo "OK, continue"
Exit 0
elif ["$choice" = = "N"] | | ["$choice" = = "N"];then
echo "Oh, Interupt"
Exit 0
Else
echo "Input [y/n]"
Fi
Exit 0
5.3 Case
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to show case expression
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
Read-p "Tell me your choice:[1-3]=>" choice
Case $choice in
"1")
echo "Your choice is one"
"2")
echo "Your choice is two"
"3")
echo "Your choice is THREE"
Esac
Exit 0
Call:
Copy Code code as follows:
$ bash sh08.sh
Tell me your choice:[1-3]=>2
Your Choice is two
$ bash sh08.sh
Tell me your choice:[1-3]=>1
Your Choice is one
$ bash sh08.sh
Tell me your choice:[1-3]=>3
Your Choice is THREE
6 function
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to test function
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
function Myprint () {
Echo-n "Your choice is"
}
Read-p "Tell me your choice:[1-3]=>" choice
Case $choice in
"1")
Myprint;echo "One"
"2")
Myprint;echo "Two"
"3")
Myprint;echo "THREE"
Esac
Exit 0
Call:
Copy Code code as follows:
$ bash sh09.sh
Tell me your choice:[1-3]=>1
Your Choice is one
$ bash sh09.sh
Tell me your choice:[1-3]=>2
Your Choice is two
$ bash sh09.sh
Tell me your choice:[1-3]=>3
Your Choice is THREE
7 Cycle
7.1 While
Copy Code code as follows:
#!/bin/bash
# program:
# This program shows while expression
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
While ["$choice"!= "yes"]
Todo
Read-p "Give your Choice [yes/no]:" Choice
Done
Exit 0
Call:
Copy Code code as follows:
$ bash sh10.sh
Give your choice [yes/no]:no
Give your choice [yes/no]:no
Give your choice [yes/no]:nx
Give your choice [yes/no]:yes
7.2 for
Copy Code code as follows:
#!/bin/bash
# program:
# This are used to demo for expression
# History:
# 2013/2/3 ON_1Y
Path= $PATH
Export PATH
For choice in 1 2 3
Todo
echo "Your choice is $choice"
Done
Exit 0
Call Example:
Copy Code code as follows:
$ bash sh11.sh
Your choice is 1
Your choice is 2
Your choice is 3
8 shell script tracking and debug
Sh-n xx.sh # Grammar Check
Sh-x xx.sh # Lists the xx.sh execution process