今天我已经开始学习编写bash shell脚本,而且教授要我们使用三个输入创建一个简单的脚本计算器:其中两个是数字输入,一个是操作员输入。
首先,根据我的理解,当从读取输入分配变量时,它被视为字符串。然后通过将变量# Everything before here is the same
print("PROCESSING FILE")
yaml_data = yaml.load(yaml_str)
yaml.dump(yaml_data, yaml_file, default_flow_style=False)
a = yaml.dump(yaml_data, default_flow_style=False)
print(a) #I make a print to debug
yaml_file.close()
与字符串版本op
进行比较,它应该评估+
。但事实并非如此,我得到以下错误:
n1+n2
./q2.txt: line 9: [+: command not found
./q2.txt: line 13: [+: command not found
答案 0 :(得分:1)
if语法必须在条件[ condition ]
所以你的脚本将是
#!/bin/bash
echo "Enter the operator"
read op
echo "Enter the first number"
read n1
echo "Enter second number"
read n2
if [ "$op" == "+" ]
then
n3=$((n1+n2))
elif [ $op = "-" ]
then
n3 = $((n1-n2))
fi
echo "Answer: $n3"
exit 0
然后它会起作用