linux - 基本的 if else 脚本

标签 linux shell scripting

我希望你们都做得很好。我是 Linux shell 脚本新手。我正在尝试学习基本的 shell 脚本,我从 if else 条件开始,我已经坚持了一周了。我已阅读尽可能多的文章,但我不明白这里的问题是什么。我知道这对你们来说一定是非常基础的,但请帮助我:)

这是我写的脚本

#!/bin/sh

count=102

if [$count -gt 100]

then

echo "$count is greater than 100"

else

echo "$count is less than 100"

fi

当我执行此脚本时,出现以下错误。

./count.sh: line 5: [102: command not found
102 is less than 100

*我还尝试不仅使用整数还使用字符串获取用户输入,但仍然遇到相同的错误。请帮忙:)

提前谢谢

最佳答案

您需要在[之后和]之前提供空格。例如

[ $count -gt 100 ]

示例代码:

count=102
if [ $count -gt 100 ]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi

关于linux - 基本的 if else 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53983782/

相关文章:

android - 脚本运行速度太快了!可能

shell - Ubuntu crontab 编辑

linux - 使用 SNMP 协议(protocol)通过 SNMP 命令运行脚本会给出不同的输出,因为脚本独立执行

linux - 从 bash 脚本执行 lsof 时出错

android - 使用 shell 脚本重启 Android 应用程序

linux - 为什么我不能将 jiffies 与正常运行时间相匹配?

linux - 奇怪的 ELF 二进制文件

scripting - InDesign 将图层复制到另一个文档

linux - 在 heredoc 中使用时如何转义 awk 的内置变量

linux - ./斐波那契.sh : line 11: syntax error near unexpected token `do'