gpt4 book ai didi

linux - 基本的 if else 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 04:43:18 25 4
gpt4 key购买 nike

我希望你们都做得很好。我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com