Scripts are best placed in/usr/local/sbin.
the script executes the Sh-x script. Sh-x can view the execution process1 when using variables in a
script, use the $ symbol:
#!/bin/bash
# #把命令赋值为变量, you need to use anti-quotes
d= ' date + '%h:%m:%s '
echo "The script begin at $d"
echo "Now we'll sleep 2 seconds"
Sleep 2
d1= ' date + '%h:%m:%s '
echo "The script end at $d"
2 use mathematical operations in scripts to be enclosed in [] as follows
#!/bin/bash
a=1
b=2
# #数学运算用 [] surround it.
sum=$[$a + $b]
echo "$a + $b = $sum"
3 using the Read command with the console interactively in the script
#!/bin/bash
read-p "Please input a number:" X
read-p "Please input a number:" Y
sum=$[$x + $y]
echo "The sum of the tow numbers is: $sum"
preset variables in 4shell
#!/bin/bash
echo " $ $ $"
1 2 is a preset variable in the script a preset variable in a script is unlimited, and 0 means the script file book
# SH option.sh 1 2 3The
output of executing this command is as follows:
1 2 3 option.sh
logic judgment in 5shell
if notice without else if the judgment statement after the IF is used (()) or the error will be
#!/bin/bash
read-p "Please input your score:" A
if ((a<60)); Then
echo "You didn ' t pass the exam."
fi
5.1 with the Else instance code is as follows
#!/bin/bash
read-p "Please input your score:" A
if ((a<60)); Then
echo "You didn ' t pass the exam."
Else
echo "good! You passed the exam. "
fi
5.2 with Else if (this is the argument in C) is represented in the shell as Elif
#!/bin/bash
read-p "Please input your score:" A
if ((a<60)); Then
echo "You didn ' t pass the exam."
elif ((a>=60)) && ((a<85)); Then
echo "good! You passed the exam. "
Else
echo "Very good! Your score is very heigh "
fi
5.3case Logic judgment
#!/bin/bash
read-p "Input a number:" N
a=$[$n%2]Case
$a in
1)
echo "The number is odd."
;;
0)
echo "The number is even."
;;
*)
echo "It ' s not a number"
;;
Esac
* indicates other values
6for CycleThe
instance code is as follows:
#!/bin/bashFor
file in ' ls ';d o
Echo $file
Done
7while Cycle
#!/bin/bash
a=5
while [$a-ge 1];
Echo $a
a=$[$a-1]
Done
Linux Learning essay-shell simple writing