Shell 语句
一 test 測(cè)試:
測(cè)試命令 test [ ] [[ ]] (( ))
打開man test 逐一介紹每個(gè)參數(shù)
?
浮點(diǎn)計(jì)算:echo 'scale=2;1/3'|bc -l
?
測(cè)試操作
命令執(zhí)行后會(huì)返回到一個(gè)系統(tǒng)變量中 $?
如果$?值為0 表示命令執(zhí)行成功 否則為失敗
二流程控制: if ? while ? for
#!/bin/bash read -p 'please input username:' usr read -p 'please input passwd:' passwdif [ $usr = 'alex' -a $passwd = 'alex3714' ];thenecho 'login successful' elseecho 'username or password is worng' fi 用戶測(cè)試 !/bin/bash age=57 while : doread -p 'input oldboy age:' agif [ $ag -eq $age ];thenecho "bingo"break elif [ $ag -gt $age ];thenecho "the age is older" elseecho "the age is younger" fiif [ -z $ag ];thencontinuefidone 猜年齡 #!/bin/bash read -p 'please input your score:' scoreif [ $score -ge 90 ];thenecho 'excellent' elif [ $score -ge 70 -a $score -lt 90 ];thenecho 'good' elif [ $score -ge 60 -a $score -lt 70];thenehco 'not bad' elseecho 'bad' fi 成績(jī)查詢 #!/bin/bash read -p 'input your file: ' file if [ -p $file ];thenecho "$file is block file" elif [ -f $file ];thenecho "$file is reuler file" elif [ -d $file ];thenecho "$file is directory file" elseecho "$file is unkown" fi 測(cè)文件類型 #!/bin/bash for ((i =1;i<=9;i++)) dofor ((j=1;j<=i;j++))doecho -n "$i*$j=$[$i*$j] "done echo done 九九乘法表 #!/bin/bash usr='mona' passwd='123' tag=true while $tag doread -p 'please input your name: ' nameread -p 'please input your password: ' pdif [ $name = $usr ] && [ $pd = $passwd ];thenecho 'login successful'while $tagdoread -p 'input your indirction: ' cmdif [ $cmd = 'quit' ];thentag=falseelse$cmdfidonefi done 登錄用戶操作?
轉(zhuǎn)載于:https://www.cnblogs.com/mona524/p/6945425.html
總結(jié)
- 上一篇: hdu-1029 Ignatius an
- 下一篇: 我的R之路:参数假设检验