BASH shell脚本搜索和显示模式

贾斯汀·西耶夫(Justin Siev)

我试图创建一个BASH shell脚本,在该脚本中,提示用户输入动物,并返回“ $ 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

对于猫或牛,我需要能够呼应“(xyz)(猫或牛)有4条腿”,我正在考虑在某处使用grep,但不知道这是否是最佳选择。有人可以帮忙吗?

谢谢!

赛勒斯

chepner的建议下

#!/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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章