linux shell命令之列表


列表由一串命令用與運算&&和或運算||連接而成
與命令的基本格式為:
命令1 && 命令2 && 命令3 && 命令4 && ... && 命令n

或命令的基本格式為:
命令1 || 命令2 || 命令3 || 命令4 || ... || 命令n

vi andlist1.sh
#!/bin/bash

#if條件是一個與列表

if [ -n "$1" ] && echo "The lst argument=$1" && [ -n "$2" ] && echo "The 2nd argument=$2"
then
#只有與列表命令都執行完,才執行下面的命令
echo "At least TWO arguments are passed to this script."
else
echo "Less than TWO argements are passed to this script."
fi
exit 0

./andlist1.sh CAI
The lst argument=CAI
Less than TWO argements are passed to this script.
./andlist1.sh CAI WU
The lst argument=CAI
The 2nd argument=WU
At least TWO arguments are passed to this script.

./andlist2.sh CAI
Usage: andlist2.sh 3 arguments
echo $?
68

./andlist2.sh CAI WU TANG
Correct arguments are passed to this script.
echo $?
0

vi orlist.sh
#!/bin/bash

MAXARGS=3
ERROR=68

#與andlist2.sh腳本相比,僅修改了test語句

test $# -eq $MAXARGS || (echo "Usage: `basename $0` " $MAXARGS arguments && false) || exit $ERROR

echo "Correct arguments are passed to this script."
exit 0
./orlist.sh CAI
Usage: orlist.sh 3 arguments
echo $?
68
./orlist.sh CAI WU TANG
Correct arguments are passed to this script.
echo $?
0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM