04-shell变量测试语句流程控制for循环

    xiaoxiao2021-11-29  20

    变量测试语句

    1.小例子(条件控制语句if...elseif  else  fi)

    =======================================================

    #!/bin/bash #test  a  program started, if no  started,started. echo  "we are test  java  started......" # JAVA is  a  variable record java  starting JAVA="" if [ "$JAVA" = "" ]         then                 echo  "JAVA is null"         elif [ "$JAVA" = "aaa" ]         then                 echo "JAVA =aaa,-----$JAVA"         else                 echo "JAVA is not  aaa,JAVA is $JAVA" fi

    =======================================================

    运行结果:

    xukaiqiangdeMacBook-Pro:shell xukaiqiang$ sh start.bat 

    we are test  java  started......

    JAVA is null

    =======================================================

    查看进程是否启动

    JAVA=`/usr/bin/grep ssh`

    将以上命令替换,来判断程序是否启动。

    2.变量测试语句

    字符串测试

    test str1=str2测试字符串是否相等

    test str1!=str2测试字符串是否不相等

    test str1  测试字符串是否不为空

    test -n str1测试字符串是否不为空

    test -z str1测试字符串是否为空

    整数测试

    test int1 -eq int2测试int1是否=int2

    test int1 -ge int2测试int1是否>=int2

    test int1 -gt int 测试int1是否>int2

    test int1 -le int2测试int1是否<=int2

    test int1 -lt int2测试int1是否<int2

    test int1 -ne int2测试整数是否相等

    文件测试

    test -d file测试文件是否为目录

    test -f file测试文件是否常规文件

    test -r file指定文件是否可读

    test -x file指定文件是否可执行

    test -w file指定文件是否可写

    test -a file指定文件是否存在

    test -s file文件大小是否非0

    使用方法:

    if[ -x /usr/bin/ssh] 测试是否可执行

    3.-a 在if中表示逻辑与

      -o 表示逻辑或

    4.exit 0表示退出

    5.小例子,输入参数,判断参数的大小。

    shell脚本

    #############################################################

    #!/bin/sh if [ $# -ne 2 ];    then "only allowed 2 parameters!"    exit 0 fi if [ $1 -eq $2 ];    then echo "$1=$2" elif [ $1 -lt $2 ] ;    then echo "$1<$2" elif [ $1 -gt $2 ];    then echo "$1>$2" fi

    ###########################################################

    执行结果:

    6.for循环

    小例子:

    ###########################################################

    #!/bin/bash #test for for  DAY in MONDAY TUESDAY WENSDAY THUHTHDAY FRIDAY SATARDAY SUNDAY do         echo "the day is $DAY" done

    ###########################################################

    运行结果:

    7.文件提取命令awk -F

    小例子:输入用户名,提取相关信息

    ###########################################################

    #!/bin/sh # display user's info /bin/echo "please input username" read username /bin/grep $username /etc/passwd > /dev/null 2> /dev/null if [ $? -eq 0 ] then       echo "username is $username" else       echo "$username is not exist!"       exit 1 fi /bin/echo #list passwd info userinfo=`/bin/grep ^$username:x /etc/passwd` userid=`/bin/echo $userinfo | awk -F : '{print $3}'` groupid=`/bin/echo $userinfo | awk -F : '{print $4}'` homedir=`/bin/echo $userinfo | awk -F : '{print $6}'` shell=`/bin/echo $userinfo  | awk -F : '{print $7}'` #get group name from GID grouptmpname=`cat /etc/group | /bin/grep :x:$groupid` groupname=`/bin/echo $grouptmpname | awk -F : '{print $1}'` echo "user id is :$userid" echo "default group is $groupname" echo "home directory is $homedir" echo "shell is $shell" echo "group members info:" # get grop members groups=`/usr/bin/groups $username` /bin/echo $groups /bin/echo #get login info userlogin=`/usr/bin/who | /bin/grep $username` if [ "$userlogin" != "" ] then     echo "$username is online!" else         echo "$username is not logged in" fi

    ###########################################################

    其中:

    ^$username:x表示精确匹配

    /dev/null 2> /dev/null不管正确还是错误结果都不看

    8.输入用户名,踢掉已经登录的用户

    #######################################################

    #!/bin/sh # test for  to kill process username="$1" ps aux | grep $username | awk '{ print $2 }' > /tmp/temp.pid killid=`cat /tmp/temp.pid` for PID in $killid do    /bin/kill -9 $PID 2> /dev/null done

    ############################################################

    运行结果:

    转载请注明原文地址: https://ju.6miu.com/read-678594.html

    最新回复(0)