首页 > 解决方案 > 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"

标签: bash

解决方案


尝试“帮助”。您甚至没有尝试阅读文档;(。我也不确定什么是“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

推荐阅读