wsx@wsx-ubuntu:~/script_learn$ cat test3.sh #!/bin/bash # testing multiple commands in the then section # testuser=wsx # if grep $testuser /etc/passwd then echo "This is my first command" echo "This is my second command" echo "I can even put in other commands besides echo:" ls -a /home/$testuser/.b* fi
wsx@wsx-ubuntu:~/script_learn$ chmod u+x test3.sh wsx@wsx-ubuntu:~/script_learn$ ./test3.sh wsx:x:1000:1000:wsx,,,:/home/wsx:/bin/bash This is my first command This is my second command I can even put in other commands besides echo: /home/wsx/.bash_history /home/wsx/.bashrc /home/wsx/.bash_logout /home/wsx/.bashrc-anaconda3.bak
# Testing nested ifs # testuser=NoSuchUser # if grep $testuser /etc/passwd then echo "The user $testuser exits on this system." else echo "The user $testuser does not exit on this system." if ls -d /home/$testuser/ then echo "However, $testuser has a directory." fi fi
wsx@wsx-ubuntu:~/script_learn$ chmod u+x test5.sh wsx@wsx-ubuntu:~/script_learn$ ./test5.sh The user NoSuchUser does not exit on this system. ls: 无法访问'/home/NoSuchUser/': 没有那个文件或目录
wsx@wsx-ubuntu:~/script_learn$ cat test23.sh #!/bin/bash # using doble parenthesis # val1=10 # if (( $val1 ** 2 > 90 )) then (( val2 = $val1 ** 2 )) echo "The square of $val1 is $val2" fi
wsx@wsx-ubuntu:~/script_learn$ chmod u+x test23.sh wsx@wsx-ubuntu:~/script_learn$ ./test23.sh The square of 10 is 100
wsx@wsx-ubuntu:~/script_learn$ cat test24.sh #! /bin/bash # using pattern matching # if [[ $USER == r* ]] then echo "Hello $USER" else echo "Sorry, I do not know you" fi
wsx@wsx-ubuntu:~/script_learn$ chmod u+x test24.sh wsx@wsx-ubuntu:~/script_learn$ ./test24.sh Sorry, I do not know you
wsx@wsx-ubuntu:~/script_learn$ cat test25.sh #!/bin/bash # looking for a possible value # if [ $USER = "rich" ] then echo "Welcome $USER" echo "Please enjoy you visit" elif [ $USER = "barbara" ] then echo "Welcome $USER" echo "Please enjoy you visit" elif [ $USER = "testing" ] then echo "Special testing account" elif [ $USER = "jessica" ] then echo "Do not forget to logout when you're done"
case语句:
1 2 3 4 5
case variable in pattern1 | pattern2) commands1;; pattern3) commands2;; *) default commands;; esac
上面的实例可以用case语句表示为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
wsx@wsx-ubuntu:~/script_learn$ cat test26.sh #!/bin/bash # using the casecommand # case $USER in rich | barbara) echo "Welcome, $USER" echo "Please enjoy your visits";; testing) echo "Special testing account";; jessica) echo "Do not forget to log off whe you're done";; *) echo "Sorry, you are not allowed here";; esac
wsx@wsx-ubuntu:~/script_learn$ chmod u+x test26.sh wsx@wsx-ubuntu:~/script_learn$ ./test26.sh Sorry, you are not allowed here
for test in Alabama Alaska Arizona Arkansas California Colorado do echo The next state is $test done
wsx@wsx-ubuntu:~/script_learn$ ./test1 The next state is Alabama The next state is Alaska The next state is Arizona The next state is Arkansas The next state is California The next state is Colorado
这里操作基本和其他语言一致(格式不同),不多讲啦。
在读取列表中的复杂值时,我们可能会遇到问题。比如下面这个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13
wsx@wsx-ubuntu:~/script_learn$ cat badtest1 #!/bin/bash # another example of how not to use the forcommand
for test in I don't know if this'll work do echo "word:$test" done
wsx@wsx-ubuntu:~/script_learn$ ./badtest1 word:I word:dont know if thisll word:work
我们可以看到shell看到了列表值中的单引号尝试使用它们来定义一个单独的数据值。
这里有两种解决办法:
使用转义字符将单引号转义
使用双引号来定义用到单引号的值
我们将这两种解决办法同时用到上个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
wsx@wsx-ubuntu:~/script_learn$ cat test2 #! /bin/bash # another example of how not to use the forcommand
for test in I don\'t know if "this'll" work; do echo "word:$test" done wsx@wsx-ubuntu:~/script_learn$ ./test2 word:I word:don't word:know word:if word:this'll word:work
# using a variable to hold the list list="Alabama Alaska Arizona Arkansas Colorado" list=$list" Connecticut" # 在尾部拼接文本
for state in $list; do echo "Have you ever visited $state?" done
wsx@wsx-ubuntu:~/script_learn$ ./test3 Have you ever visited Alabama? Have you ever visited Alaska? Have you ever visited Arizona? Have you ever visited Arkansas? Have you ever visited Colorado? Have you ever visited Connecticut?
for file in /home/wsx/python_learn/* do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fi done wsx@wsx-ubuntu:~/script_learn$ ./test5 /home/wsx/python_learn/athletelist.py is a file /home/wsx/python_learn/athletemodel.py is a file /home/wsx/python_learn/ch2_data_input.py is a file /home/wsx/python_learn/chapter5_first.py is a file /home/wsx/python_learn/chapter6_first.py is a file /home/wsx/python_learn/chapter6_second.py is a file /home/wsx/python_learn/chapter6_third.py is a file /home/wsx/python_learn/coinFlips.py is a file /home/wsx/python_learn/Dive_into_python is a directory
for (( i=1; i <= 10; i++ )) do echo "The next number is $i" done
wsx@wsx-ubuntu:~/script_learn$ ./test6 The next number is 1 The next number is 2 The next number is 3 The next number is 4 The next number is 5 The next number is 6 The next number is 7 The next number is 8 The next number is 9 The next number is 10
wangsx@SC-201708020022:~/tmp$ cat test #!/bin/bash for file in /home/* do if [ -d "$file" ] then echo "$file is a directory" else echo "$file is a file" fi done > output.txt wangsx@SC-201708020022:~/tmp$ cat output.txt /home/wangsx is a directory