我有一个shell脚本,有时用户有时会忘记所需的arg.所以我需要在某种程度上,如果用户已经提供了特定的参数,那么不应该提示用户提供该特定的arg.有人可以帮忙吗?
示例脚本:
#!/bin/sh
if [ $GAME =="" ]
then
echo Enter path to GAME:
read GAME
else
echo $GAME
fi
Run Code Online (Sandbox Code Playgroud)
例O/P 1:
$ ./sd.sh -GAME asdasd
输入GAME的路径:
?asd
例O/P 2:
$ ./sd.sh
输入GAME的路径:
asd
你可以使用检查第一个参数 $#
if [ $# -ne 1 ];then
read -p "Enter game" GAME
else
GAME="$1"
fi
Run Code Online (Sandbox Code Playgroud)
$#是指参数的数量.此脚本检查参数的数量是否为1.如果要检查第五个参数,则使用$5.如果你真的想要更可靠的东西,你可以选择使用getopts.或GNU getopt支持长选项