//语法一:
if[ 条件 ]; then
程序
fi
//语法二:
if[ 条件 ]
then
程序
fi
# whoami 可以查看当前用户,但是在使用su切换用户的时候 不适用- whoami结果不准确
eg1:
#shell 标准头部 ,没有可能会在调用其他语言的时候出现一些问题或报错#加上它 编译器可以知道我们当前的文件执行的是标准shell脚本#!/bin/bash#从env中获取当前的登录用户信息 ,#1.从env中获取当前配置信息#2.通过管道从配置信息中获取当前用户信息#3.再次通过管道返回的信息截取 由等号分割的两部分 取第二部分
user=$( env | grep USER | cut -d "="-f 2)#打印当前用户echo"user :"$user#判断当前用户是否是rootif["$user"=="root"]
then
echo"The user is root!"
fi
eg2:
#!/bin/bash#获取磁盘分区中 磁盘使用率 (awk '{print $5}' 获取第5列 的数据 )
memberUse=$( df -h | grep 'VolGroup00-LogVol00'| awk '{print $5}'| cut -d "%"-f 1)echo"内存使用情况:"$memberUseif["$memberUse"-ge 8 ]
then
echo"请注意内存使用情况"
fi
2.双分支if条件语句
if[ 条件 ]
then
条件成立时,执行
else
条件不成立时,执行
fi
eg1:
#!/bin/bash#等待30秒 获取用户从键盘上输入的内容
read -t 30 -p "please input a dir :"dir#判断用户输入的内容是不是一个文件夹if[-d "$dir"]
then
echo$dir"This is a dir "elseecho$dir"This is not a dir"
fi
eg2:
#!/bin/bash#获取tomcat服务进程的内容,#grep -v grep 表示去掉本次查询使用grep 产生的进程数据
tomcat=$(ps-ef | grep 'org.apache.catalina.startup.Bootstrap start'| grep -v grep | awk '{print $18}')echo"tomcat : "$tomcatif[-n '$tomcat']
then
echo"tomcat service is begining !"elseecho"tomcat service is dowm, warning ...."
fi
echo"service is process end"
3.多分支if条件语句
语法:
if[ 条件1 ]
then
条件成立时,执行
elif [ 条件2 ]
then
条件2成立,执行
…………
else
当所有的条件都不成立时,执行
fi
eg1:
#!/bin/bash
read -t 30 -p 'please input first num :' firstnum
read -t 30 -p 'please input second num :' secondnum
read -t 30 -p 'please input operator :' ope
# 当三个字符中有一个是空的时候给予提示,并结束当前进程if[-z "$firstnum"-o -z "$secondnum"-o -z "$ope"]
then
echo'please input the full contents !'exit-1
fi
#判断num是否为数字 sed 's/[0-9]//g' 表示把管道输出的结果中的0-9数字全部替换成null # eg : 显示中5到10行里匹配root,把行内所有的root替换为FTL,并打印到屏幕上# sed '5,10 s/root/FTL/g' yum.log
num1=$(echo$firstnum| sed 's/[0-9]//g')
num2=$(echo$secondnum| sed 's/[0-9]//g')if[-n "$num1"-o -n "$num2"]
then
echo"please input number !"exit 0
fi
#判断运算符,并进行相应的计算if["$ope" == "+"]
then
sum=$(($firstnum+$secondnum))
elif ["$ope" == "-"]
then
sum=$(($firstnum-$secondnum))
elif ["$ope" == "*"]
then
sum=$(($firstnum*$secondnum))
elif ["$ope" == "/"]
then
sum=$(($firstnum/$secondnum))elseecho"operator is wrong !"exit 5
fi
#输出结果echo"$firstnum$ope$secondnum$sum"
eg2:
#!/bin/bash# 判断输入的内容是否是文件或文件夹
read -t 30 -p "please input contents :" file
if[!-n "$file"]
then
echo"$file is null !"exit 1
elif [!-e "$file"]
then
echo"$file is not file "exit 2
elif [-d "$file"]
then
echo"$file is directory "exit 3
elif [-e "$file"]
then
echo"$file is file"exit 4
elseecho"$file is other file"
fi
4.多分支case语句
语法:
case $变量名 in
"值1")
变量值等于值1,执行
;;"值2")
变量值等于值2,执行
;;
…………
*)
如果变量的值都不是以上的值,执行
;;
esac
eg:
#!/bin/bash#判断输入的是yes or no
read -t 30 -p "please input yes or no :" con
case "$con" in
"yes")echo"您输入的内容是:yes"exit 1
;;"no")echo"您输入的内容是:no"exit 2
;;*)echo"您输入的内容不是yes/no,而是:"$con;;
esac
三. 循环控制
1. for循环一
语法:
for 变量 in 值1 值2 值3 ……
do
程序
done
eg:
#!/bin/bash# 使用循环解压缩文件ls*.tgz > lg.log
ls*.tar.gz >> lg.log
#循环解压缩 把解压输出信息放进linux的垃圾回收站for i in $(cat lg.log )do
tar -zxf $i &>/dev/null
done
参考链接 第一个脚本: # #键代表注释,但是 下面一句不是注释, 它表示当前脚本是一个标准脚本 # 不加在调用其他语言的时候回报错 #!/bin/bash # The first program echo -e “\e[1;31m this is my first shell demo \e[0m”