700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【Shell】shell编程条件测试if for case select while

【Shell】shell编程条件测试if for case select while

时间:2018-10-14 16:55:58

相关推荐

【Shell】shell编程条件测试if for case select while

shell编程条件测试

Shell条件判断初始

Shell分支if语句

Shell循环for语句

Shell分支case语句

Select列表select循环

Shell 循环控制continue,break,exit

Shell循环while语句

Shell条件判断初始

[ ] 表示条件测试,扩展为test命令[ ] 注意这里的空格很重要。要注意在'['后面和']'前面都必须要有空格例如:STRING1 = STRING2 字符串相等STRING1 != STRING2 字符串不等[root@linux ~]# test "root" = "root"[root@linux ~]# echo $?0[root@linux ~]# test "root" = "Root"[root@linux ~]# echo $?1[root@linux ~]# [ "root" != "ROOT" ][root@linux ~]# echo $?0$RANDOM%10随机数取余数0-9guess=$(($RANDOM%10+1))已非交互的方式自动正规填入密码123(避免交互)echo 123 | passwd --stdin user$i &> /dev/null整数判断INTEGER1 -eq INTEGER2 相等 INTEGER1 -ge INTEGER2 大于等于INTEGER1 -gt INTEGER2 大于于INTEGER1 -le INTEGER2 小于等于INTEGER1 -lt INTEGER2 小于INTEGER1 -ne INTEGER2 不等于例如:[root@linux ~]# [ 100 -gt 90 ][root@linux ~]# echo $?0文件类型判断-d FILE 文件存在并是一个目录-e FILE 文件存在-f FILE 文件存在并是一个普通文件(ls -l 第一位是 -)-s FILE 文件存在并不是空文件文件权限判断-r FILE 文件存在并具有读权限-w FILE 文件存在并具有写权限-x FILE 文件存在并具有执行(或搜索)权限

Shell分支if语句
单分支结构

if [ 条件1 ] then 动作1 fi [root@linux ~]# cat test.sh#!/bin/bashif [ -f /etc/passwd ]thenecho "This is a file"fi

双分支结构

if [ 条件1 ] then 动作1 条件真时做动作else 动作2 条件假时做动作fi [root@linux ~]# cat test.sh#!/bin/bashif [ -f /etc/vsftpd/vsftpd.conf ]thenservice vsftpd startelseecho "vsftpd is not install"fi提示输入一个用户名字,判断该用户是否存在?存在显示其信息(uid gid 家目录 shell),不存在添加该帐号#!/bin/bashread -p "输入用户名: " usernameif id $username &> /dev/nullthenecho "用户存在,显示信息"grep $username /etc/passwd | cut -d':' -f 1,3,4,6,7elseecho "用户$username不存在,添加用户"useradd $usernamefi

多分支结构

判断条件1是否为真,如为真,执行语句1,如果为假,判断条件2,若条件2为真,执行语句2… ,若所有条件都为假,执行语句n。

if [ 条件1 ] then 动作1 elif [ 条件2 ] then 动作2 ... ... else 动作n fi [root@linux ~]# cat test.sh#!/bin/bashguess=$(($RANDOM%100+1))read -p "请输入1-100 整数: " NUMif [ $NUM -eq $guess ]thenecho "congratulation"elif [ $NUM -gt $guess ]thenecho "big!!! try again !!!"elseecho "small!!! try again !!!"fi

Shell循环for语句

for 变量 in 值1 值2 值3 ... ... for ((i=1;i<=5;i++))do 动作1 动作2 ... ... done [root@linux ~]# for ((i=1;i<=5;i++)); do echo $i ; done

Shell分支case语句

case 变量 in 模式1) 动作1 ;; 模式2) 动作2 ;; ... ... 模式N) 动作N ;; *) 动作 ;; esac [root@linux ~]# cat test.sh#!/bin/bashread -p "请出示身份:" namecase name incaptain)echo "队长别开枪!是我~";;hulk)echo "你喝三鹿奶粉的么?";;ironman)echo "你的铁甲哪儿买的?"*)echo ”输入信息的提醒“;;esac

select列表循环

select 变量 in 命令1 命令2 命令3 ... ... do 动作 done[root@linux ~]# cat est.sh#!/bin/bashselect i in date cal pwddo$idone[root@linux ~]# ./test.sh1) date2) cal3) pwd#? 1Sun Nov 16 18k49k45 CST

Shell 循环控制continue,break,exit

break 跳出整个循环continue 跳出当前循环,不再执行continue后面的语句,进入下一个循环。exit 直接退出[root@linux ~]# cat test.sh#!/bin/bash# continuefor i in {1..10}doif [ $(($i%3)) -eq 0 ]then# continue 跳出本次循环回到上面的for循环# break 跳出整个循环elseecho ”$i“done

Shell循环while语句

当 while后的条件为真时,就执行do和done之间的语句,直到条件为假结束。

while 条件 do 动作1 动作2 ... ... done死循环[root@linux ~]# while true ; do echo 1 ; done[root@linux ~]# cat tset.sh#!/bin/bashx=0while [ $x -le 10 ]doecho $xdone

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。