Shell Process Control (if, else, case, while, for, until), shellcase
1. Select Conditions
1.1.if statement
Simple syntax
#!/bin/bashMATH_SCORES="$1"NAME="$2"if [ -z "${MATH_SCORES}" ]then printf "The command requires that options have a scores.\n" printf "What's ths scores of your math? :" read MATH_SCORESfiif [ -z "${NAME}" ]then printf "The command requires that options have a student's name.\n" printf "What's your name? :" read NAMEfiif [ "${NAME}" = "sunny" ]then printf "No, sunny is a teacher.\nPleae input your name,ok? " read NAME printf "My god,i think, you are bann sunny, excuse me.\n\n"else printf "\n"fiif [ "${MATH_SCORES}" -ge 90 ]then echo "Your scores is very good.Congratulations to you, ${NAME}." echo "I hope that you are't sunny."else if [ "${MATH_SCORES}" -ge 60 ]then echo "Congratulations. ${NAME}"elif [ "${MATH_SCORES}" -ge 50 ]then echo "Come on ${NAME}."else echo "What are you interested in? Please tell me, maybe i can help you, "${NAME}" ?"fi;fiecho[web@h p]$ ./if_then_else.sh 37The command requires that options have a student's name.What's your name? :sunnyNo, sunny is a teacher.Pleae input your name,ok? sunnyMy god,i think, you are bann sunny, excuse me.What are you interested in? Please tell me, maybe i can help you, sunny ?
Statement body, ended with fi.
Use the SELECT statement to determine whether the variable is successfully obtained.
JAVA_PATH=`which java 2>/dev/null`if [ "x$JAVA_PATH" != "x" ]; then JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null` JRE_HOME=`dirname $JAVA_PATH 2>/dev/null`fi
1.2.case statement
Double semicolons must not be less; like if, the statement body also needs an Terminator.
When looking for a job, give the corresponding contact information of different applicants based on the positions they apply.
#!/bin/bashecho -e "\v\tRecruitment Announcement"echo "Are you ready to apply for any job?"echo "1 accounting"echo "2 cashier"echo "3 secretary"echo -e "\vPlease enter a number to select the corresponding positions."read NUMcase $NUM in 1) printf "call mr wang. number is 1124\n" ;; 2) printf "call miss li. number is 1233.\n" ;; 3) printf "call miss ji. number is 1367.\n" ;; *) printf "If you want to make a lot of money, to be a seller. call 1498.\n" ;;esac
The echo command is used to output interaction information. To format the output, debug it back and forth. Here, we just want to familiarize ourselves with the syntax of the case statement and better implement it in the "cat" blog.
Use the case statement to process the result of coordinate movement.
#!/bin/bashecho $(date)X1=0Y1=0echo "L - turn left"echo "R - turn right"echo "U - turn up"echo "D - turn down"read INScase $INS in L) X1=$[${X1}-1] ;; R) X1=$[${X1}+1] ;; U) Y1=$[${Y1}+1] ;; D) Y1=$[${Y1}-1] ;; [[:lower:]]) printf "Uppers, please.\n" ;; *) ;;esacecho "x = ${X1} y = ${Y1}"
Output script help information:
case "$1" in "") run_it ;; -r|--read) read_it ;; -v|--version) display_version ;; --clear) clear_TMPFILE ;; -h|--help) display_help ;; *) echo "findTom -h" display_help ;;esac
An error occurs if "-v | -- version" is expressed in double quotation marks. If you want to use it, write "-v" | "-- version "".
1. 3. implement conditional testing through series judgment
[view@file donatello]$ [ 3 -gt 5 ] && echo "true " || echo "false "false[view@file donatello]$ [ ! 3 -gt 5 ] && echo "true " || echo "false "true[work@file donatello]$ [ ! 3 -gt 5 ] && ( echo -n "true, "; echo "exit 0" ) || ( echo -n "false, "; echo "exit 1" )true, exit 0[work@file donatello]$ [ 3 -gt 5 ] && ( echo -n "true, "; echo "exit 0" ) || ( echo -n "false, "; echo "exit 1" )false, exit 1
2. Loop
2.1.for Loop
Syntax format:
for name[ [in [words…] ] ; ] do commands; donefor ((expr1;expr2;expr3)) ; do commands; done
[web@h p]$ ls >> java.dir[web@h p]$ cat java.sh#!/bin/bash for i in $(cat java.dir)do echo $idone
Add computing 1 to 10
#!/bin/bashdeclare -i sum=0for i in {1..10}do sum=$((sum+i))doneecho sum = $sum#!/bin/bashsum=0for i in $(seq 1 10)do sum=$((sum+i))doneecho sum = $sum
For statementWithout listTo obtain the list information from the command line.
[web@h p]$ cat t1.sh#!/bin/bash for ido echo $idone[web@h p]$ sh t1.sh lsls[web@h p]$ sh t1.sh `ls`
Class C style(It is reflected in the "for" Statement and the loop body. The variable does not require the "$" symbol)
#!/bin/bashSUM=0MAX=100MIN=0for ((i=MIN; i<= MAX; i++))do SUM=$[SUM+i]doneecho "From ${MIN} add to ${MAX} is $SUM."
2. LoopUntil, while
If the return value of a command is involved in condition determination, the value must be compared with numbers to control program running, regardless of whether 0 or 1 is returned.
# Syntax
until test-commands; do consequent-commands; donewhile test-commands; do consequent-commands; done
The while loop starts when the condition is met. The until loop starts when the condition is not met.
For example, a loop body statement can be executed only when the condition is false:
#!/bin/bashuntil falsedo echo -n '-' sleep 1 echo -e -n '\b\' sleep 1 echo -e -n '\b-' sleep 1 echo -e -n '\b/' sleep 1 echo -e -n '\b*'done
If the while statement is executed when the conditions are met, you can select the while statement.
While can also directly read the file, "done </path/to/file" in the done statement ". View the system's default mounted special file system:
#!/bin/bash#while read LINE; do echo $LINE | grep -v dev &> /dev/null if [ $? -eq 0 ]; then echo $LINE | awk '{print $1}' | grep -v boot fidone < /etc/fstab
Bash script debugging
Check script syntax and debug execution script
$ bash -n adduser.sh$ bash -x adduser.sh
Shell script tracking
You can use the set command to track running scripts. Add a line "set-x" to the script; the line starting with "+" is the obtained trace content (the execution process of the program ).
[root@right mag]# cat tes.sh #!/bin/bashset -xread -p "How old are you? " answerif [ $answer == "34" ]; then echo "Yes, very good."else echo "No, i don't want say anyting."fiexit 0[root@right mag]# ./tes.sh + read -p 'How old are you? ' answerHow old are you? 34+ '[' 34 == 34 ']'+ echo 'Yes, very good.'Yes, very good.+ exit 0[root@right mag]# ./tes.sh + read -p 'How old are you? ' answerHow old are you? 33+ '[' 33 == 34 ']'+ echo 'No, i don'\''t want say anyting.'No, i don't want say anyting.+ exit 0
Check the execution process that has not been tracked:
[root@right mag]# ./tes.sh How old are you? 32No, i don't want say anyting.