bash - How do I use conditions in a read -r command bash-script?
问题描述
I need to write a script with a conditional statement at the beginning, to decide what statements user who executes this script will read. This is depending on their "preferred ice-cream flavor" in this case below.
The code I am showing doesn't work, but I believe it give the general idea of how to do this. If you do know how to get a script like this working, tell me in the comments!
ex.
echo -n "What ice cream do you like, vanilla or chocolate: "
read -r icecreamflavor
if $icecreamflavor == vanilla; then gsed -i "7 i/# "
if $icecreamflavor == chocolate; then gsed -i "6 i/# "
echo "I also like vanilla"
echo "I like vanilla more than chocolate"
解决方案
尝试“帮助”。您甚至没有尝试阅读文档;(。我也不确定什么是“gsed”命令,但您的示例应该如下所示:
echo -n "What ice cream do you like, vanilla or chocolate: "
read -r icecreamflavor
if [ "$icecreamflavor" = "vanilla" ]; then
gsed -i "7 i/# "
echo "I also like vanilla"
elif [ "$icecreamflavor" = "chocolate" ]; then
gsed -i "6 i/# "
echo "I like vanilla more than chocolate"
fi
推荐阅读
- javascript - Javascript中嵌套循环的问题
- git - 我们如何缩写参考?
- c++ - Solving knight's tour problem on a huge board?
- javascript - api调用和邮递员之间的区别
- elasticsearch - 获取弹性搜索中全文搜索的每个搜索词的权重
- javascript - 使用正则表达式计算字符串中的特定字母是一个坏主意吗?
- typescript - 我可以基于枚举定义接口吗?
- angular - Angular 7 - 如何在 *ngFor 加载数据之前显示加载器?
- c++ - 如何清除控制台窗口中的特定行
- java - VS Code Extension 卡住安装