Linux shell script bug and error All In One
Linux shell script bug and error All In One
if [ "$condition1" == "$condition2" ] then echo yes; else echo no; fi
errors
未找到命令 ❌
- shell 变量赋值=等号两边不能有空格
# ❌
var1 = 2023
# ✅
var1=2023
echo $var1
echo "$var1"
echo ${var1}
参数太多 ❌
- 在 if 条件判断语句中, 使用双引号包裹变量
#!/usr/bin/env bash
function is_same_file () {
echo "arg1 = $1"
echo "arg2 = $2"
# 赋值操作=等号两边不能有空格 ✅
test1=$(stat -c "%d %i" $1)
test2=$(stat -c "%d %i" $2)
echo $test1
echo $test2
# "$var", 在 if 条件语句中, 使用双引号包裹变量 ✅
# if 与 [ 之间有空格 ✅
# [ condition ] 的两边有空格 ✅
# if [ $test1 == $test2 ] ; then
if [ "$test1" == "$test2" ]; then
echo yes;
else
echo no;
fi
}
is_same_file ./student.sh ./hard
is_same_file ./student.sh ./soft
variable assignment
with spaces bug
#!/bin/sh
# WEEKLY_TITLE
# WEEKLY_DESC
# CONTENT
WEEKLY_TITLE='';
WEEKLY_DESC='';
CONTENT='';
HOUR=$(date +%H);
echo "现在时间是: ${HOUR}"
# number / string
# if [$HOUR == '14']
# if [$HOUR == 14]
# then
# WEEKLY_TITLE="周报通知";
# WEEKLY_DESC="⏰ 两点了,写周报啦 ✅";
# echo "⏰ 两点了,写周报啦 ✅"
# else
# WEEKLY_TITLE="周会通知";
# WEEKLY_DESC="⏰ 五点了,开周会了";
# echo "⏰ 五点了,开周会了"
# fi
bug ❌
# ❌ spaces bug
CONTENT2 = '
[
{
"tag": "text",
"text": "日期: '$WEEKLY_DESC'\n"
},
{
"tag": "at",
"user_id": "all",
"user_name": "所有人"
}
]'
echo "⏰ $CONTENT2"
solution ✅
# echo "⏰ '${CONTENT1}'"
echo "⏰ $CONTENT1'"
# ✅ no whitespaces ok
CONTENT2='
[
{
"tag": "text",
"text": "日期: '$WEEKLY_DESC'\n"
},
{
"tag": "at",
"user_id": "all",
"user_name": "所有人"
}
]'
echo "⏰ $CONTENT2"
if whitespace
if...then...else..fi
bug ❌
#!/bin/bash
# str1="Learn Bash"
# str2="Learn Bash with tutorialkart"
# if [ "$str1" != "$str2" ]; then
# echo "Both Strings are not Equal. ❌"
# else
# echo "Both Strings are Equal. ✅å"
# fi
HOUR=20;
echo "现在时间是: ${HOUR}"
TWO_O_CLOCK=14
# TWO_O_CLOCK='14'
# number / string
# if [$HOUR == '14'] / if [$HOUR == 14]
# not space bug ❌
# if["$var1" == "$var2"] / if["$var1" != "$var2"]
if ["$HOUR" != "$TWO_O_CLOCK"]; then
WEEKLY_TITLE="周报通知";
WEEKLY_DESC="⏰ 两点了,写周报啦 ✅";
echo "\n⏰ 两点了,写周报啦 ✅"
else
WEEKLY_TITLE="周会通知";
WEEKLY_DESC="⏰ 五点了,开周会了❌";
echo "\n⏰ 五点了,开周会了❌"
fi
# space ok ✅
# if[ "$var1" == "$var2" ] / if[ "$var1" != "$var2" ]
if [ "$HOUR" != "$TWO_O_CLOCK" ]; then
WEEKLY_TITLE="周报通知";
WEEKLY_DESC="⏰ 两点了,写周报啦 ✅";
echo "\n⏰ 两点了,写周报啦 ✅"
else
WEEKLY_TITLE="周会通知";
WEEKLY_DESC="⏰ 五点了,开周会了❌";
echo "\n⏰ 五点了,开周会了❌"
fi
solution ✅
#!/bin/bash
# str1="Learn Bash"
# str2="Learn Bash with tutorialkart"
# if [ "$str1" != "$str2" ]; then
# echo "Both Strings are not Equal. ❌"
# else
# echo "Both Strings are Equal. ✅å"
# fi
HOUR=20;
echo "现在时间是: ${HOUR}"
TWO_O_CLOCK=14
# TWO_O_CLOCK='14'
# number / string
# if [$HOUR == '14'] / if [$HOUR == 14]
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
tutorials
https://www.tutorialkart.com/bash-shell-scripting/bash-strings-equal/
https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
refs
https://github.com/xgqfrms/linux-shell-script-programming/issues/3#issuecomment-1578070809
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15937724.html
未经授权禁止转载,违者必究!