shell basic syntax
Shell pass parameters:
$#: the number of parameters passed to the script or function
$0: the name of the script itself
$n: the nth parameter of the script or function
$@: the list of all parameters passed to the script or function
$*: approximately equal to $@, a list of all parameters
$$: the PID of this script
$!: the PID of the last background command
executed $?: the return value of the last command executed
#!/bin/sh echo "shell script name: $0" echo "arg num: $#" echo "args are: $@" for arg in "$@" do echo $arg done echo $*
1.for statement
#!/bin/sh for file in $(ls /home/sfl/mytest/shell_test |grep sh) do echo $file done for i in 0 1 2 3 4 5 6 7 8 9 # export 10th do echo $i done for i in "0 1 2 3 4 5 6 7 8 9" #Only loop once, "" is a variable do echo $i done
2. until statement
END_CONDITION=end until [ "$var1" = "$END_CONDITION" ] do echo "Input variable #1 " echo "($END_CONDITION to exit)" read var1 echo "variable #1 = $var1" echo done exit 0
3.if statement
#!/bin/bash echo "Input a number #1 " read num #Get a parameter from the command line to num echo "variable #1 = $num" if [ $num -lt 60 ] then echo "you are not pass" elif [ $num -lt 70 ] && [ $num -ge 60 ] #Judgment of multiple conditions then echo "pass" elif [[ $num -lt 85 && $num -ge 70 ]] #If put together, pay attention to the double brackets, do not write it as [ $num -lt 85 && $num -ge 70 ] then echo "good" elif (( $num <= 100 )) && (( $num >= 85 )) #For those who have language foundation, this way of writing makes people feel very comfortable, don't forget the double parentheses then echo "very good" else echo "num is wrong" fi #if must have an end tag, the root XML is very similar, if it is not closed, an error will be reported exit 0
4. case statement
#!/bin/sh #useage:./shell_case.sh start/stop/restart case $1 in start) echo "start ok" ;; #Note that it is a double semicolon stop) echo "stop ok" ;; restart) echo "restart ok" ;; *) echo "no param" ;; esac #watch out for closing tags exit 0