1.shell scripts are a collection of commands. Record a set of commands that need to be executed to the document, and then call the document.
139 e-mail, receive the message at the same time by SMS notification.
Shell Footsteps Writing recommendations: Custom scripts placed in the/usr/local/sbin directory
2. First shell script
Vim firstshell.sh
#! /bin/bash
# #this is my first shell script
# #writen by Jason 2015-07-02
Date
echo "Hello world!"
#! /bin/bash, which begins with the first line, indicates that the file uses bash syntax
. sh suffix, habit
#后面跟的注释, good habits, easy to see footsteps later
Execute script: Sh firstshell.sh or./firstshell.sh (sometimes chmod modified when a permission is missing)
3. use of variables in scripts
VI variable.sh
#! /bin/bash
# in Thie shell script we'll use variables
# # Writen by Jason 2015-07-02
d= ' Date +%h:%m:%s '
echo "This script begin at $d"
echo "Now we'll sleep 2 seconds."
Sleep 2
d1= ' Date +%h:%m:%s '
echo "This script end at $d 1"
Variables d and D1 are defined using anti-quote ' ', variable content used to the results of other commands (Shell Footstep Basics blog).
Script total reference variable, plus $
3.1 numeric operations , two-digit sum
VI sum.sh
#! /bin/bash
# # Sum of tow numbers
# by Jason 20150702
A=1
b=2
sum=$[$a + $b]
Echo $sum
Mathematical calculation, to be enclosed in [], and the outermost to add a $
3.2 User interaction , user input
VI read.sh
#! /bin/bash
# # Use ' read ' in shell script
# by Jason 20150702
Read-p "Please input a number:" X
Read-p "Please input anoter number" Y
sum=$[$x + $y]
echo "The sum of the number is: $sum"
read is used to interact with the user and use the user input string as the variable value
-X View the shell execution process, sh-x read.sh
3.3 Shell preset variable $1,$2,$0 (the name of the print script itself)
VI option.sh
#! /bin/bash
##
##
SUM=$[$1+$2]
Exho "Sum= $sum"
SH option.sh 3 7
sum=10
$1,$2 is the shell footstep preset variables, by default, enter two numbers sequentially, the default assignment variable $1=3,$2=7.
The same $3,$4,$5 is a preset variable in turn.
4. logic in the script to determine if, if...else,if. Elif. Else
4.1 VI iftest1.sh
#! /bin/bash
##
##
Read-p "Input Your score:" A
if (($a <60)); Then if Judgment statement; Then
echo "didn ' t pass the exam." Commond
Fi fi
(($a <60)), a unique format in the shell's footsteps, must use two ()
Syntax format: if judgment statement; then
Match the result to execute the corresponding command
4.2 If. Else
VI iftest2.sh
#! /bin/bash
##
##
Read-p "Iput Your score:" A
if ($a <60)); then if Judgment statement; Then
echo "Do not pass." Commond
else Else
echo "Good! Pass the exam. " Commond
Fi fi
4.3 If. Esif. Else
VI iftest3.sh
#! /bin/bash
##
##
Read-p "Input Your score:" A
if (($a <60)); Then
echo "Do not pass."
Elif ($a >=60) && (($a <80)); Then
echo "Good,pass."
else echo "Very Good,high."
Two judgment statements by && or | | Together, Two (()) must be separated and judged individually
Determine the value in addition to (()) Form, there is [], but need -lt (less than),-GT (greater than),-le (less than equals),-ge (greater than or equal),-eq (equals),-ne (not equal)
If [$a-lt 60]; Then
If [$a-gt 60] | | [$a-lt 80]; Then ....
4.4 Determining document properties
-E to determine whether a file or directory exists;-D to determine if it is a directory and exists;-F to determine if it is a normal file and exists
;-R Determines whether the document is readable, if-w is feasible, and-X is executable.
if [-f/root/test.txt]; Then echo, "It is file."; Fi
[] must add a space before and after, or error.
5. Script Logic judgment case
VI case.sh
#! /bin/bash
##
##
Read-p "Input a number:" N
a=$[$n%2]
Case $a in case variable in
1) value1)
echo "The number is odd." Commond
;; ;;
0) value2)
echo "The number is even." Commond
;; ;;
*)
echo "This was not a number"
;;
Esac
The number of value values is not limited to judgment.
6. the loop for in the script
VI fortest.sh
#! /bin/bash
For i in ' SEQ 1 5 '; Do for variable name in loop condition; do
echo $i Command
Done
Loop condition SEQ 1 5 represents a sequence from 1-5
The 6.1 loop condition is written as a set of strings or numbers, or it can be a result of a command execution
For I in 1 2 3 4 a B; do echo $i; Done
For file in ' ls '; do echo $file; Done
7. loops in the script while
VI whiletest.sh
#! /bin/bash
A=5
While [$a-ge 1]; Do and loop condition;
echo $a command
a=$[$a-1]
Done
8.Functions in shell scripts
Function: is to organize a piece of code into a small unit, a name, call the name of the unit.
VI func.sh
#! /bin/bash
function sum () function name ()
{ {
Sum=$1+$2 Command
Echo $sum
} }
Sum $ $ Call function
In a shell script, the function must be written first, not in the back or in the middle, and in the first declaration, before it can be called later.
Shell Footstep Writing