Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bashshell脚本搜索和显示模式_Bash_Shell_Unix_Grep - Fatal编程技术网

bashshell脚本搜索和显示模式

bashshell脚本搜索和显示模式,bash,shell,unix,grep,Bash,Shell,Unix,Grep,我试图创建一个bashshell脚本,在该脚本中,我提示用户输入动物,并返回“the$animal有(我在case语句中设置的数字)腿” 我使用的是一个案例陈述。我目前的发言如下: #!/bin/bash echo -n "Enter an animal: " read animal case $animal in spider|octopus) echo "The $animal has 8 legs";; horse|dog) echo "The $animal has 4 legs";

我试图创建一个bashshell脚本,在该脚本中,我提示用户输入动物,并返回“the$animal有(我在case语句中设置的数字)腿”

我使用的是一个案例陈述。我目前的发言如下:

#!/bin/bash

echo -n "Enter an animal: "
read animal

case $animal in
spider|octopus) echo "The $animal has 8 legs";;
horse|dog) echo "The $animal has 4 legs";;
kangaroo|person) echo "The $animal has 2 legs";;
cat|cow) echo "The $ animal has 4 legs";;
*) echo "The $animal has an unknown number of legs"
esac
对于猫或牛,我需要能够回应“The(xyz)(猫或牛)有4条腿”,我正在考虑在某个地方使用grep,但不知道这是否是最好的选择。有人能帮忙吗

谢谢

带有:


似乎在不需要grep的情况下,添加*cat |*cow)而不是cat | cow)是有效的。谢谢

$animal
替换
$animal
。实际上,
case
语句只需要为每个类别设置
n=8
n=“未知数量”
等。之后,你可以有一个单独的语句‘echo“The$animal has$n legs”。@Cyrus对此表示感谢——我没有意识到,如果在提示输入动物时输入“tom cat”,目标是能够显示“tom cat has 4 legs”。或者如果我输入brown cow,我应该能够显示“brown cow有4条腿”
#!/bin/bash

echo -n "Enter an animal: "
read -r animal

case $animal in
  spider|octopus)       n=8;;
  horse|dog)            n=4;;
  kangaroo|person)      n=2;;
  cat|cow)              n=4;;
  *)                    n="an unknown number of"
esac

echo "The $animal has $n legs"