Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用 bash 编写脚本(我开始用 bash 编程),但我有错误,我尝试了一切,但我仍然不知道如何修复它。
#!/bin/bash cap=0 cat|while read line do read a if[$a -gt $cap] then echo "MORE" fi done
这是简单的脚本,但我是 bash 的新手,请有人帮助我;)
语法错误在这里:
if[$a -gt $cap]
您需要空格和引号(对于带有空格或空值的内容是安全的):
if [ "$a" -gt "$cap" ]
使用测试机制会更好一些[[,它是语法(而不是[命令),并且不需要双引号来保证安全:
[[
[
if [[ $a -gt $cap ]]
甚至比使用数学上下文更好,它允许您使用传统的数学运算符并自动取消引用变量:
if (( a > cap )) ; then ... fi