if 语句for循环

9.27

shell
在shell脚本中,如果用户不输入东西,系统不自动退出,this is a bug!
文件测试语句:-d -f -r -w -x -e
逻辑测试语句:“&&”与(同时满足) “||”或(前为假,后面才实施) “!”非(看中间是与还是或来决定)
整数值比较语句:eq(是否等于) gt(是否大于) lt(是否小于) ge(是否大于或等于) le(是否小于或等于) ne(是否不等于)
字符串比较语句:=(等于) !=(不等于) -z(是否为空)

if条件测试语句
if条件语句的单分支结构有if\then\fi 关键词组成,相当于口语的“如果...那么...”
#vi if.sh
#!/bin/bash
yt="/1/2/3/4"
if[ ! -e $yt] if [ -w $yt]
then
mkdir -p $yt chmod g+w $yt
fi #bash if.sh
#ll if.sh #ll -d /1/2/3/4
#bash if.sh

if条件语句的双分支结构由if、then、else、fi类似于口语“如果...那么...或者...那么...”
#vi if.sh
('DD'删除)

#!/bin/bash
ping -c 3 -i 0.2 -w 3 $1 &>/dev/null
if[$? -eq 0]
then
echo"Host $1 is On-line."
else
echo"Host $1 is off-line."
fi
#chmod 777 if.sh
#./if.sh 192.168.26. ...

#! /bin/bash
read -p "please set directory: " n
if [ ! -e $n ]
then
mkdir -p $n
else
echo "this $n directory exist"
fi

#. /if.sh
xx
#ls
. /if.sh

#vi if.sh

#. /if.sh
70
80
90

#vi if.sh
#!/bin/bash
read -p "Enter your parameter : " n
if [ -e $n ] && [ -d $n ]
then
echo "this $n is directory"
elif [ -e $n ] && [ -f $n ]
then
echo "this $n is file "
else
echo "you enter is emputy"
fi

#./if.sh

for条件循环语句

语序一次性读取多个信息,然后逐一处理
#vi forlist.txt
a
b
c

#vi passwd.sh

#!/bin/bash
read -p "Enter The Users Password:" n
for u in ‘cat forlist’
..............

#ll forlist
#chmod 777 forlist
#chmod 777 passwd.sh
#ls
#./passwd.sh
#chmod 777 passwd.sh
#ll passwd.sh
#./passwd.sh

#vi ipaddr
3个ip地址
#vi foriplist.sh
#bash foriplist.sh
#chmod 777 forcheck.sh

猜你喜欢

转载自blog.51cto.com/13956389/2286805