Name Gender Weight Goog male 65.123 Tech female 50.456
%s : 输出字符串
%d : 输出整型数字
%c : 输出一个字符
%f : 输出实数的小数形式
%-4.2f : 指格式化为小数, 其中 .2 指保留 2 位小数
%-10s : 指一个宽度为 10 个字符( - 表示左对齐, 没有则表示右对齐)
流程控制
if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!\Git\Git\bin\bash
a=1 b=2
if [ $a == $b ] then echo "a = b" elif [ $a -gt $b ] then echo "a > b" elif [ $a -lt $b ] then echo "a < b" else echo "no result" fi
运行结果如下所示 :
1
a < b
for
1 2 3 4 5 6 7 8 9 10 11 12 13
#!\Git\Git\bin\bash # 顺序打印当前列表中的数字 for num in 1 2 3 4 5 6 do echo "The number: $num" done # 顺序打印字符串中的字符 for str in This is a string do echo "The string: $str" done
运行结果如下所示 :
1 2 3 4 5 6 7 8 9 10
The number: 1 The number: 2 The number: 3 The number: 4 The number: 5 The number: 6 The string: This The string: is The string: a The string: string
while
1 2 3 4 5 6 7 8 9
#!\Git\Git\bin\bash
int=1 while(($int<=3)) do echo ${int} let "int++" done
until [ $num -gt 3 ] do echo ${num} ((num++)) done
运行结果如下所示 :
1 2 3
1 2 3
case
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!\Git\Git\bin\bash
echo 'Please input a number: ' read num case $num in 1) echo 'The number you inputed: 1' ;; 2) echo 'The number you inputed: 2' ;; 3) echo 'The number you inputed: 3' ;; *) echo 'You inputed others number.' ;; esac
运行结果如下所示 :
1 2 3 4 5 6 7 8 9
$ bash caseTest.sh Please input a number: 2 The number you inputed: 2
$ bash caseTest.sh Please input a number: 4 You inputed others number.
break
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!\Git\Git\bin\bash
while : do echo -n "please input a number between 1 and 3: " read num case $num in 1|2|3) echo "the number you inputed: ${num}" ;; *) echo "you inputed others number and game over !" break ;; esac done
运行结果如下所示 :
1 2 3 4 5 6 7 8 9
$ bash breakTest.sh please input a number between 1 and 5: 1 the number you inputed: 1 please input a number between 1 and 5: 2 the number you inputed: 2 please input a number between 1 and 5: 3 the number you inputed: 3 please input a number between 1 and 5: 4 you inputed others number and game over !
continue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!\Git\Git\bin\bash
while : do echo -n "please input a number between 1 and 3: " read num case $num in 1|2|3) echo "the number you inputed: ${num}" ;; *) echo "you inputed others number and game over !" continue echo "This Line Never Be Executed !" ;; esac done
运行结果如下所示 :
1 2 3 4 5 6 7 8 9 10
$ bash continueTest.sh please input a number between 1 and 3: 1 the number you inputed: 1 please input a number between 1 and 3: 2 the number you inputed: 2 please input a number between 1 and 3: 3 the number you inputed: 3 please input a number between 1 and 3: 4 you inputed others number and game over ! please input a number between 1 and 3:
函数
语法
1 2 3 4 5
[ function ] funname [()] { action; [return int;] }
function 关键字可加可不加.
无参函数
1 2 3 4 5 6 7 8 9 10 11 12
#!\Git\Git\bin\bash
funcWithReturn() { echo "Please input the first number: " read num1 echo "Please input the second number: " read num2 return $(($num1+$num2)) }
funcWithReturn echo "The sum: $?"
运行结果如下所示 :
1 2 3 4 5
Please input the first number: 1 Please input the second number: 2 The sum: 3
$? : 用来获取调用函数的返回值.
函数参数
1 2 3 4 5 6 7 8 9 10 11 12
#!\Git\Git\bin\bash
funcWithParam() { echo "The first parm: $1" echo "The second parm: $2" echo "The thrid parm: $3" echo "the sixth parm: ${6}" echo "The amount of parm: $#" echo "All parms: $*" }
funcWithParam 10 9 8 7 6 5
运行结果如下所示 :
1 2 3 4 5 6
The first parm: 10 The second parm: 9 The thrid parm: 8 the sixth parm: 5 The amount of parm: 6 All parms: 10 9 8 7 6 5
#!\Git\Git\bin\bash ### # @Author: GoogTech # @Email: googtech@qq.com # @Date: 2021-06-27 # @Site: https://shell.googtech.io # # 1. 编写 shell 脚本, 用户输入整数, 如果大于等于0, # 屏幕打印 "the test value is greater than 0", # 如果小于0, 屏幕打印 "the test value is less than 0". ###
read -r -p "Please input a number : " num if ((num>0)) then echo "The test value is greater than 0." else echo "The test value is less than 0." fi
运行结果如下所示 :
1 2 3 4 5 6 7
$ bash Test.sh Please input a number : 0 The test value is less than 0.
$ bash Test.sh Please input a number : 1 The test value is greater than 0.
while [ $COUNT -lt $NUMBER ] do read -r -p "Please input your pwd: " PWD COUNT=$((COUNT+1)) if [ "$PWD" == $PASSWORD ] then echo "The password is corret." exit else echo "The password is incorret." continue fi done
运行结果如下所示 :
1 2 3 4 5 6 7 8 9 10 11
$ bash Test.sh Please input your pwd: GoogTech.io The password is incorret. Please input your pwd: GoogTech.Io The password is incorret. Please input your pwd: GoogTech.iO The password is incorret.
$ bash Test.sh Please input your pwd: GoogTech.IO The password is corret.
尝试从文件读取数据前, 测试下文件 /etc/shadow 是否可读, 首先判断是否是存在, 且是一个文件, 如果不存在输出 “sorry, the file /etc/shadow does not exist”. 如果存在判断是否可读, 如果可读, 打印文件的最后一行, 如果不可读, 输出 “sorry, I am unable to read the /etc/shadow file”.
#!\Git\Git\bin\bash ### # @Author: GoogTech # @Date: 2021-06-27 # @Email: googtech@qq.com # @Site: https://shell.googtech.io # # 4. 尝试从文件读取数据前, 测试下文件 /etc/shadow 是否可读, # 首先判断是否是存在, 且是一个文件, # 如果不存在输出 "sorry, the file /etc/shadow does not exist". # 如果存在判断是否可读, 如果可读, 打印文件的最后一行, # 如果不可读, 输出 "sorry, I am unable to read the /etc/shadow file". ###
DIRECTORY_PATH="/etc" FILE_PATH="/etc/shadow"
if [ ! -d "$DIRECTORY_PATH" ] then echo "sorry, the file /etc/shadow does not exist." elif [ ! -r "$FILE_PATH" ] then echo "sorry, I am unable to read the /etc/shadow file." else tail -n 1 $FILE_PATH fi
运行结果如下所示 :
1 2 3 4 5 6 7
$ echo "echo string into the /etc/shadow file" >> /etc/shadow
$ cat /etc/shadow echo string into the /etc/shadow file
$ bash Test.sh echo string into the /etc/shadow file
read -r -p "Please input a number: " NUM MIN=${NUM} MAX=${NUM} SUM=$((SUM+NUM))
while [ $COUNT -lt $NUMBER ] do read -r -p "Please input a number: " NUM if((MAX < NUM)) then MAX=${NUM} fi if((MIN > NUM)) then MIN=${NUM} fi SUM=$((SUM+NUM)) COUNT=$((COUNT+1)) done
echo "MIN=$MIN, MAX=$MAX, SUM=$SUM"
运行结果如下所示 :
1 2 3 4 5 6 7 8 9 10 11
Please input a number: 1 Please input a number: 2 Please input a number: 3 Please input a number: 4 Please input a number: 5 Please input a number: 6 Please input a number: 7 Please input a number: 8 Please input a number: 9 Please input a number: 10 MIN=1, MAX=10, SUM=55
len=${#INPUT} count=$((len/2)) # 从左到中遍历字符串 for i in $(seq "$count") do # 从右到中逐个获取字符串中的字符 lasti=$((len-i+1)) # 从左到右, 从右到左依次比较字符串中的字符 first=$(echo "$INPUT"|cut -c "$i") second=$(echo "$INPUT"|cut -c $lasti) # 判断字符是否相等 if [ "$first" != "$second" ] then echo "no, it's not a palindrome." exit fi done
echo "yes, it's a palindrome."
运行结果如下所示 :
1 2 3 4 5
Please input a string: GoogTech no, it's not a palindrome.
Please input a string: GooG yes, it's a palindrome.