系统相关
首页 > 系统相关> > linux shell命令之列表

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

 

标签:shell,passed,script,列表,命令,sh,&&,linux,echo
来源: https://www.cnblogs.com/zhudaheng123/p/14647129.html