Bash Unix如何检查是否输入了特定单词作为参数
我正在用Unix编写脚本,但我需要一种方法来检查在命令行中输入的参数是否为特定单词 因此,如果在使用脚本时用户键入:Bash Unix如何检查是否输入了特定单词作为参数,bash,shell,unix,scripting,Bash,Shell,Unix,Scripting,我正在用Unix编写脚本,但我需要一种方法来检查在命令行中输入的参数是否为特定单词 因此,如果在使用脚本时用户键入: $ ./script hello 我的脚本可以判断hello是作为参数输入的,并且可以适当地显示消息 如果用户键入hello以外的内容作为参数,那么我的脚本可以显示另一条消息 谢谢。这应该可以: #!/bin/bash if [[ $1 == hello ]];then echo "hello was entered" else echo "hello wasn't ente
$ ./script hello
我的脚本可以判断hello是作为参数输入的,并且可以适当地显示消息
如果用户键入hello以外的内容作为参数,那么我的脚本可以显示另一条消息
谢谢。这应该可以:
#!/bin/bash
if [[ $1 == hello ]];then
echo "hello was entered"
else
echo "hello wasn't entered"
fi
可以使用$number检索命令行参数 例如,第一个参数存在于$1,第二个参数存在于$2,以此类推 您可以在BASH中使用条件句,我假设您使用BASH就像使用其他语言一样;但是,语法有点不可靠:。这里有一个链接给你
在Bash中,传递给shell脚本的参数存储在如下命名的变量中:
$0 = name of the script.
$1~$n = arguments.
$# = number of arguments.
$* = single string of all arguments: "arg1,arg2,..."
#!/bin/bash
case "$1" in
"hello" )
printf "you entered hello\n"
;;
"goodbye" )
printf "well goodbye to you too\n"
;;
* )
printf "you entered something I don't understand.\n"
;;
esac
exit 0
您可以简单地使用if[$1==某个字符串];然后…有许多方法可以对照列表检查位置参数。当列表中有许多项时,可以使用case语句而不是if。。。埃利夫。。。埃利夫。。。金融机构比较。语法如下:
$0 = name of the script.
$1~$n = arguments.
$# = number of arguments.
$* = single string of all arguments: "arg1,arg2,..."
#!/bin/bash
case "$1" in
"hello" )
printf "you entered hello\n"
;;
"goodbye" )
printf "well goodbye to you too\n"
;;
* )
printf "you entered something I don't understand.\n"
;;
esac
exit 0
输出/使用
如果您确定参数的位置,您可以: !/bin/bash 如果[[$1==SearchWord]];然后 已输入echo SearchWord 其他的 未输入echo SearchWord fi 如果您不确定: 您可以使用$* 如果[$1==你好];然后回声有你好;否则回声没有你好;fi