If语句每次都换成else-bash
我是新来的,所以我为我的错误道歉,也为我的知识不足道歉(我只是初学者)。 这里就是,我正在用bash和li编写一个小脚本,我有if语句,这里就是If语句每次都换成else-bash,bash,whiptail,Bash,Whiptail,我是新来的,所以我为我的错误道歉,也为我的知识不足道歉(我只是初学者)。 这里就是,我正在用bash和li编写一个小脚本,我有if语句,这里就是 #!/bin/bash something=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3) if [ $something ?? 'you' ]; then echo "$something" else echo "nope
#!/bin/bash
something=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3)
if [ $something ?? 'you' ];
then
echo "$something"
else
echo "nope"
fi
具体地说,我想从中得到什么-我输入一些单词/句子/任何东西来whiptail,如果其中包含一些字符串,则打印它,但每次都会打印其他字符串;。请帮忙
编辑现在可以了,谢谢,但我需要检查字符串是否包含单词
if [[ $string =~ .*My.* ]]
似乎不起作用了我一点也不明白,失去了希望,搜索了我所关注的网络
#!/bin/bash
OPTION=$(whiptail –title “Menu Dialog” –menu “Choose your option” 15 60 4 \ “1” “Grilled ham” \ “2” “Swiss Cheese” \ “3” “Charcoal cooked Chicken thighs” \ “4” “Baked potatos” 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ];
then echo “Your chosen option:” $OPTION
else echo “You chose Cancel.”
fi
我刚刚粘贴了这个脚本来检查它是如何工作的,并对它进行了修改,它不是我的脚本,应该可以工作,但是它说“您选择了取消”。您可能要查找的是字符串比较运算符,如
==
或=代码>。比如说,
if [ "$something" == "you" ]; then
echo "$something"
else
echo "nope"
fi
if [ -z "$something"] ;then
如果$something
等于您
,则回显$something
;else echonope
或者,正如David C.Rankin在他的评论中提到的,您可以检查字符串变量来证明字符串是否为空。比如说,
if [ "$something" == "you" ]; then
echo "$something"
else
echo "nope"
fi
if [ -z "$something"] ;then
字符串为空
if [ -n "$something" ]; then
字符串不是空的
有关此测试的更多信息,请查看测试
手册页面 ??
语法对我来说是新的。你可以简单地做如果whiptail--inputbox…
,或者你可以测试如果[-z$something],然后回显“nope”,否则…
我怀疑这可能是个家庭作业,但是一个bash if语句的google可能会出现一些关于字符串includes.rereading,我怀疑你是对的,??
实际上是“我使用哪个操作员?”的问题。可能是重复的-这确实是在30分钟前提出的。