[英]How can I check if there are no command line arguments passed in a busybox based Unix system?
我正在尝试创建一个 if else 语句,其中 if 检查是否有命令行参数,else 用于没有命令行参数的情况。 我已经用 if else 和两个 if 试过了。 每当我不带任何参数运行时,我都会收到错误消息:
sh: 0: 未知操作数。
如果我使用命令行参数运行它可以正常工作。 如果我使用if $1
运行脚本,则会收到错误消息:
sh:失踪]
这是我最近尝试的两个案例,
if [ $1 -gt 0 ];
then
ch=$1
stri=$2
thefunction $ch $stri
fi
# User input not working
if [ -z "$1" ];
then
stat="Y"
while [ $stat == "[Yy]" ];
do
printf "\nPlease enter a choice. Enter 1 for manual.\n"
read ch
flag=1
if [ $ch -eq 3];
then
printf "\nPlease enter string to be searched.\n"
read stri
fi
thefunction $ch $stri $flag
printf "Do you wanna continue? \n Press 'Y' to continue..."
read stat
done
fi
和
if [ $1 -gt 0 ];
then
ch=$1
stri=$2
thefunction $ch $stri
else
stat="Y"
while [ $stat == "[Yy]" ];
do
printf "\nPlease enter a choice. Enter 1 for manual.\n"
read ch
flag=1
if [ $ch -eq 3];
then
printf "\nPlease enter string to be searched.\n"
read stri
fi
thefunction $ch $stri $flag
printf "Do you wanna continue? \n Press 'Y' to continue..."
read stat
done
fi
我也尝试过使用多个 [[ ]] 代替单个 [ ], if ! $1
if ! $1
和我在 Google 上能找到的大部分内容。
如何检查基于busybox的Unix系统中是否没有命令行arguments传递?
您检查 arguments 的计数是否等于零。
if [ $# -eq 0 ]; then
echo "No arguments passed"
fi
如果没有 arguments, $1
将扩展为空,因此:
if [ $1 -gt 0 ];
扩展为:
if [ -gt 0 ];
由于-gt 0 ]
对于[
无效 arguments ,因此该工具会抱怨。
请注意,bash 是空间感知和3];
可能是3 ];
.
此外[ $stat == "[Yy]" ]
检查 stat 是否等于字符串"[Yy]"
。 ==
是[
命令的扩展,请使用=
代替。 要检查$stat
是否等于Y
或y
,请使用[ "$stat" = "y" -o "$stat" = "Y" ]
或使用case
构造。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.