51.条件选择语句:if else
if command #注意if与command之间的空格
then
command
command
else
command
fi
语法格式可如下:
if [ string/numeric expression]
then
command
fi
if [[ string expression ]]
then
command
fi
if (( numeric expression ))
then
command
fi
x=10
y=20
(( x<y ))
echo $?
0
#!/bin/bash
a=100
if [ $a -eq 100 ]
then
echo "a is equal to $a"
else
echo "a is not equal"
fi
52.exit:退出命令。
#!/bin/bash
if (( $1 < 0 || $1 > 30 )) #另一种 写法:[ $1 -lt 0 -o $1 -gt 30 ]
then
echo "mdays is out of range"
exit 2
fi
53.stty -echo:禁止回显。
stty -echo
read -p "please enter a password :" password
if test "$password" == "ab"
then
echo "password is matching"
fi
stty echo
~
53.字符串NULL检测。
if [ "$string" = "" ] # [ ! "$string" ] or [ -z "$string" ]
then
echo "the string is null"
fi
上一条命令的执行结果检查。
read -p "enter a user name : " user_name
grep "^$user_name" /etc/passwd > /dev/null
status=$?
if test $status -eq 0
then
echo "user '$user_name' is found id /etc/passwd."
else
echo "user '$user_name' is not found in /etc/passwd."
fi
54.if关键字进行文件操作。
#!/bin/bash
echo "$1 is: "
if ! [ -e $1 ]
then
echo ".. do not exists"
exit
else
echo "file is present"
fi
if [ -x $1 ]
then
echo "..executable"
fi
if [ -r $1 ]
then
echo "..readable"
fi
if [ -w $1 ]
then
echo "..wriable"
fi
#!/bin/bash
file1="File1"
file2="File2"
if cp $file1 $file2 #判断命令是否执行成功
then
echo "copy command executed successfully"
echo "content of file named fill copied in another file named file2"
else
echo "some problem in command execution"
fi
55.逻辑运算符。|| &&
#!/bin/bash
echo "enter the first number"
read val_a
echo "enter the second number"
read val_b
#if [ $val_a ==1 ] && [ $val_b == 10 ] #有2种写法
if [[ $val_a ==1 && $val_b == 10 ]]
then
echo "testing is successful"
else
echo "testing is not successful"
fi
这2个运算符有个巧妙的使用方法
command1 || command2 # 如果command1的执行结果为true,那么command2不执行;如果为false,那么command2执行
command1 && command2 # 如果command1执行结果为true,command2执行;如果为false,command2不执行。
56.if/elif/else/fi
if exp1
then
command
elif exp2
then
command
else
command
fi
57.空指令: :
#!/usr/bin/env bash
city=London
if grep "$city" city_database_file >& /dev/null #if后面可以跟bool表达式,也可以跟一个执行命令。当跟bool表达式时,需要用[[]] 或者[]包起来,但是如果跟的是执行命令,那么是不需要用[]包起来的。
then
: #这里必须放指令,if的执行语句不能为空,所以得放置一个null命令。
else
echo "citu is not found in city_database_file"
exit 1
fi
58.case关键字
#!/usr/bin/env bash
echo "please enter any number from 1 to 9"
read number
case $number in
1 ) echo "One"
;;
2) echo "two"
;;
3) echo three
;;
4) echo four
;;
5) echo five
;;
6) echo "six"
;;
*) echo "some onther number"
;;
esac
case $1 in
*@*.com) echo "valid email address"
;;
*) echo "invalid string"
;;
esac
case语法与交互
匹配还能够利用pattern去匹配。
#!/bin/bash
echo "Enter Day Of The Week"
read day
case $day in
[mM][oO][nN][dD][aA][yY])
echo "First Day is Monday"
;;
[tT][uU][eE][sS][dD][aA][yY])
echo "Second Day Tuesday"
;;
[wW][eE][dD][nN][eE][sS][dD][aA][yY])
echo "Third Day Wednesday"
;;
[tT][hH][uU][rR][sS][dD][aA][yY])
echo " Fourth Day Thursday"
;;
[fF][rR][iI][dD][aA][yY])
echo "Fifth Day Friday"
;;
[sS][aA][tT][uU][rR][dD][aA][yY])
echo "Sixth Day Saturday"
;;
[sS][uU][nN][dD][aA][yY])
echo "Seventh Day Sunday"
;; *)
echo "Invalid Day of Week"
;; esac
shell获取当前月份
mth=$(data +%m)
#当前时间
#$ date +"%F %H:%M:%S"
case $mth in
02)
echo "february usually has 28 days."
echo "if it is a leap year,it has 29 days."
;;
04|06|09|11)
echo "the current month has 30 days."
;;
*)
echo "the current month has 31 days."
;;
esac
59.select:选择交互功能
注意:这个是无限循环的,如果想退出的话,需要手动调用exit;
用户输入的值是存储在内置变量REPLY,而非自己给定的变量中。
PS3="please select any one : " #提示内容
select var1 in a bc def ghi jkl
do
echo "present value of var1 is $REPLY"
done
结合case命令
PS3="please select any one:"
select var in a b quit
do
case $REPLY in
a ) echo option is a
;;
b) echo option is b
;;
quit) exit
;;
*) echo option is default
;;
esac
done
break也可以跳出循环。
PS3="please select one of the above:"
select COMPONENT in comp1 comp2 comp3 all none
do
case $REPLY in
comp1 | comp2 | comp3 ) echo "comp1 or comp2 or comp3 selected"
;;
all) echo "selected all"
;;
none) break
;;
*) echo "error: invalid selection,$REPLY."
;;
esac
done
60.循环语句:for
for var in element1 element2 element3 element4 element5
do
commands
done
无限循环:
for ((;;))
do
command
done
例子:
for var in {1..10}
do
echo $var
done
显示输入参数:
for var in $*
do
echo "command line contains: $var"
done
从文件读取输入:
for var in `cat file`
do
echo "file contains $var"
done
从其他命令读取输入:
for var in $(ls /bin/*)
do
echo -n -e "$var \t"
done
for filename in *.c
do
echo "copying $filename to $filename.bak"
cp $filename $filename.bak
done
61.循环命令的continue关键字
for x in 1 2 3
do
echo before $x
continue 1 #跟编程中的continue关键字一样,不会执行continue后面的语句。
echo after $x
done
exit 0
rm -fr sample*
echo > sample_1
echo > sample_2
mkdir sample_3
echo > sample_4
for file in sample*
do
if [[ -d "$file" ]]; then
echo "skipping directory $file"
continue
fi
echo file is $file
done
rm -fr sample*
exit 0
若文件不存在,则拷贝到指定目录;存在,则跳过。
for file in `ls *.mp3`
do
if test -e /MP3/$file
then
echo "the file $file exists
continue
fi
cp $file /MP3
done
62.循环命令的关键字break
set -o nounset
typeset -i num=0
while true
do
echo -n "enter any number (0 to exit):" #statements
read num
if [[ $num == 0 ]]; then #使用[[]]时,变量取值加$,如果使用(()),变量取值不用加$
break
else
echo "square of $num is $(( num * num ))."
fi
done
echo "script has ended"
declare -i x
x=0
while [[ $x -le 10 ]]; do
echo $x
x=$((x+1))
done
63.until:循环条件控制语句,语义和while一致。
until command
do
command(s)
done
例子:
x=0
until [[ $x -eq 10 ]]; do
echo $x
x=`expr $x + 1` #注意 $x + 1 之间要加空格
done
64.循环结果通过管道输出到shell命令。
for value in 10 5 27 33 14 25
do
echo $value
done | sort -n