Linux命令行与shell脚本(8)--结构化命令

使用if-then语句

  • bash shell的if语句会运行if行定义的那个命令。如果该命令的退出状态码是0,位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会执行。
if date
then
        echo "it worked"
fi

if abc
then
        echo "it worked again"
fi
echo "we are outside of if"

if-then-else 语句

#!/bin/bash
testUser=nouser
if grep $testUser /etc/passwd
then
        echo $testUser are system User;
else
        echo $testUser are not sysatUser;
fi

嵌套if

  • 可以使用 if-then-elif-then 的嵌套形式
#!/bin/bash
testUser=nouser
if grep $testUser /etc/passwd
then
        echo $testUser are system User;
elif grep mysql /etc/passwd
then
        echo $testUser are not system User,but mysql are system User;
else
        echo $testUser and mysql are not system User
fi

test命令

  • test命令可以使if语句测试跟命令的退出状态无关的条件,比如数值比较、字符串比较、文件比较。注意test命令无法处理浮点值,test命令用方括号表示
  • test命令使用标准的数学比较符号来表示字符串比较,用文本代码来表示数值比较。如果你为数值使用了数学运算符号,shell会将它们当成字符串值,可能无法产生正常的结果
  • test对数值和字符串的比较:
#!/bin/bash
val1=10;
val2=11;
testuser=chenhong;
#number test
if [ $val1 -gt 5 ]
then
        echo "The test value $val1 is greater than 5";
fi

if [ $val1 -eq $val2 ]
then
        echo "The values are equal"
else
        echo "The values are different"
fi
#string test
if [ $USER = $testuser ]
then
        echo "Welcom $testuser";
else
        echo "This is not $USER";
fi

val1=baseball
val2=hockey

if [ $val1 \> $val2 ]  #shell 也用 < 和 > 操作符进行重定向,所以必须用 \< 或 \> 加以转义
then
        echo $val1 is greater than $val2;
else
        echo $val1 is less than $val2;
fi

val1=testing;
val2="";

if [ -n "$val1" ] #val1长度是否不为0
then
        echo "The string '$val1' is not empty";
else
        echo "The string '$val1' is empty";
fi

if [ -z "$val2" ] #$val2长度是否为0
then
        echo "The string '$val2' is empty ";
else
        echo "The string '$val2' is not empty";
fi
if [ -z "$val3" ] #$val3长度是否为0
then
        echo "The string '$val3' is empty ";
else
        echo "The string '$val3' is not empty";
fi
  • test对文件的比较:
#!/bin/bash
if [ -d $HOME ] #测试目录是否存在
then
        echo "Your HOME directory is exist";
        cd $HOME;
        ls -al;
else
        echo "Your HOME has some probolem";
fi

if [ -e $HOME ] #测试文件是否存在
then
        echo "now check file"
        if [ -e $HOME/testing ]
        then
                echo "Appending date to existing file";
                date >> $HOME/testing;
        else
                echo "Create new file testing";
                date > $HOME/testing;
        fi
else
        echo "Your HOME has some problems";
fi

if [ -e $HOME ] #测试文件是否存在
then
        echo "The object is existed,it is a file?";
        if [ -f $HOME ] #测试是否为文件
        then
                echo "Yes,it is a file";
        else
                echo "No, it is not a file";
        fi
else
        echo "The object is not existed";
fi

pwfile="/etc/passwd";
if [ -e $pwfile ] 
then
        echo "The $file is existed";
        if [ -r $pwfile ]  #测试文件是否可读
        then
                echo "I can read the file";
                cat $pwfile;
        else
                echo "I can not read the file";
        fi
else
        echo "The $file is now existed";
fi

if [ -s $HOME ]  #检查文件是否存在且非空
then
        echo $HOME is existed and not empty
fi

logfile="$dir/log";
touch $logfile;
chmod a-w $logfile;
now=`date`;
echo $logfile
if [ -w $logfile ]   #判断文件是否可写
then
        echo "First attemp succeeded";
        echo "The program ran at : $now " >> $logfile;
else
        echo "First attemp failed";
fi
chmod a+w $logfile
if [ -w $logfile ]   #判断文件是否可写
then
        echo "Second attemp succeeded";
        echo "The program ran at : $now " >> $logfile;
else
        echo "Second attemp failed";
fi

touch $HOME/test1;
if [ -x $HOME/test1 ] #测试文件是否可执行
then
        echo "You can run $HOME/test1";
        ./test1
else
        echo "You can not run $HOME/test1";
fi

if [ -o /etc/passwd ]  #判断是否为文件的属主
then
        echo "You are the owner of /etc/passwd file";
else
        echo "You are not the owner of /etc/passwd file";
fi
touch test1
touch test2
touch test3
if [ ./test2 -nt ./test1 ] #判断test2是否比test1新
then
        echo "test2 is newer than test1";
else
        echo "test2 is older than test1";
fi
if [ ./test2 -ot ./test3 ] #判断test2是否比test3旧
then
        echo "test2 is older than test3";
else
        echo "test2 is newer than test3";
fi

复合条件测试

  • 可以使用 &&|| 组合复合条件
#!/bin/bash
if [ -e $HOME/testing ] && [ -w $HOME/testing ] #文件存在并且可写
then
        echo "You can write to the file";
fi

if-then的高级特性

  • 双圆括号,允许将高级数学表达式放入比较重,也可以在脚本中的普通命令里使用来赋值
#!/bin/bash

val1=10
if (( $val1**2 > 90 ))
then
        (( val2 = $val1 ** 2 ));
        echo "The square of $val1 is $val2";
fi
  • 双方括号命令提供了针对字符串比较的高级特性,可以使用正则表达式
#!/bin/bash

if [[ $USER == c* ]]
then
        echo "Hello $USER";
else
        echo "Sorry, i do not know you ";
fi

case命令

#!/bin/bash

case $USER in
rich | chenhong)   #值为rich或 chenhong
        echo "Welcome, dear $USER";;
testing)           #值为testing
        echo "Welcome, test";;
*)                      #不满足上面两个的其他值
        echo "Welcome ,everyone";;
esac

for命令

#!/bin/bash

for test in Alabama Alaks Alibaba Arizona
do
        echo The next state is $test;
done
echo The last state is $test; #The last state is Arizona

for test in I don\'t know if this\'ll work #转义
do
        echo The word is $test;
done

for test in Nevada "New Hampshire" "New York"
do
        echo going to $test
done

list="Alabama Alaks Alibaba Arizona";
for state in $list
do
        echo "The next state $state";
done
  • 默认情况下,bash shell会将下列字符当作字段分隔符: 空格、制表符、换行符。 如果bash shell在数据中看到这些字符中的任意一个,它就会假定你在列表中开始了一个新的数据段。可以修改ISF的值来修改字段分割符,如

ISF=$'\n' #修改字段分割符为换行符

C语言风格的for命令

#!/bin/bash

for (( i=1; i <= 10; i++ ))
do
    echo "The next number is $i";
done

while命令

#!/bin/bash

var1=10
while [ $var1 -gt 0 ]
do
        echo $var1;
        var1=$[ $var1 - 1 ];
done
  • 在含有多个命令的while语句中,在每次迭代中所有的测试命令都会被执行

until命令

#!/bin/bash

var1=100
until [ $var1 -eq 0 ]
do
    echo $var1;
    var1=$[ $var1 - 25 ];
done

嵌套循环

#!/bin/bash

for (( a=1; a <= 3 ; a++ ))
do
    echo "a : $a";
    for (( b=1; b <= 3 ; b++ ))
    do
        echo "  b : $b";
    done
done

var1=3 
until [ $var1 -eq 0 ]
do
    echo "Outer loop:$var1"
    var2=1
    while [ $var2 -lt 5 ]
    do
        var3=`echo "scale=4; $var1 / $var2 " | bc`
        echo "  Inner loop: $var1 / $var2 = $var3"
        var2=$[ $var2 + 1 ] 
    done
    var1=$[ $var1 - 1 ];
done

循环处理文件数据

#!/bin/bash

IFS.OLD=$IFS;
IFS=$'\n';
for entry in `cat /etc/passwd`
do
        echo "the entry: - ";
        IFS=":";
        for item in $entry
        do
                echo "  $item";
        done
done
IFS=IFS.OLD;

控制循环

  • break 命令
#!/bin/bash

for (( a=1 ; a < 4 ; a++ ))
do
        echo "Outer loop $a";
        for (( b=1; b < 100 ; b++ ))
        do
                echo "  Inner loop $b";
                if (( $b >4 ))
                then
                        break 2 #跳出两层循环
                fi
        done
done
  • continue 命令

处理循环的输出

  • for循环处理结果重定向到 output.txt

“`

!/bin/bash

for file in HOME/doif[d file” ]
then
echo “’ fileisadirectory;elif[f file” ]
then
echo “’ fileisafile;fidone> HOME/output.txt

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值