Linux shell script bug and error All In One
1.Linux shell command ln All In One2.Linux shell command cut All In One3.Linux shell standard input bugs All In One4.Linux shell command base64 All In One5.How to use the shell command to get the version of Linux Distributions All In One6.Linux shell command make & Makefile All In One7.Linux shell command strings All In One8.sudo & su & Rust All In One9.Linux shell command make All In One10.Linux shell script shebang env All In One11.Linux shell script switch...case All In One12.Linux shell command ls sort by date All In One13.Linux shell script read file line by line All In One14.Linux shell regular expression All In One15.How to fix IP filter regular expressions written using grep command in Linux shell script All In One16.Linux shell script programming All In One17.Linux shell script redirection All In One18.How to use Linux shell command filter the IP address All In One19.how to create one interactive mode command line configuration tool with shell language on Linux All In One20.Linux shell script get date and time All In One21.Linux shell command screen All In One22.Linux shell `#!` interpreter All In One23.Linux shell set command All In One24.Linux shell script auto generate batch files All In One25.Linux bash script HereDoc All In One26.Linux shell system environment variables All In One27.Linux shell command curl All In One28.Linux shell command chroot All In One29.Linux shell command chmod All In One30.Linux shell number variables add All In One31.Linux bash shell "${1}" All In One32.Linux xattr shell command All In One33.Multiple Ways to Change Terminal Shell in Linux All In One34.Linux shell stdin & stdout & stderr All In One35.Linux shell man command All In One36.Linux shell command line editor All In One37.Linux shell command uname All In One38.Linux bash shell script block comment All In One39.Linux bash shell script batch download files All In One
40.Linux shell script bug and error All In One
41.Linux bash write long string with multi-lines comments All In One42.Linux set command All In One43.Linux echo command All In One44.Linux echo 换行 All In One45.Linux Bash shell 脚本定时器 All In One46.Linux System Variables All In One47.linux bash shell auto read write template48.Linux bash script read args All In One49.Linux Bash Script conditions syntax All In One50.Linux Bash Script loop syntax All In One51.Linux shell script All In One52.如何通过 Linux terminal 查看一个文件的 meta 信息 All In One53.Linux shell uname -a All In One54.Linux shell command create file methods All In One55. how to write string to file in linux bash All In One56.linux bash shell & lsof & grep & ps57.Linux Bash Shell All In One58.Linux shell command copy file All In One59.Linux shell command & zip & tar All In One60.Linux shell command show project directory tree All In One61.Linux shell commands man chmod All In One62.Linux & bash & shell & PID & PPID63.Linux bash shell All In One64.Linux shell command symbolic link & soft link All In One65.Linux bash shell All In One66.Linux shell (.sh 文件) 基础命令 常用命令 汇总67.How to use a shell script to check whether a command had been installed in the Linux server All In One68.How to print a string with a variable by using the echo command in the shell script All In One69.Linux install vim errors All In OneLinux 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
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-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15937724.html
未经授权禁止转载,违者必究!
合集:
Linux Shell
标签:
bash
, bug
, Linux
, whitespace
, space
, if...then...else..fi
, shell script
, white space
, 空格
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-02-25 Webpack 4.x uglifyjs remove comments
2021-02-25 Ajv: Another JSON schema validator
2021-02-25 iOS simulator change os version
2021-02-25 Kotlin All In One
2021-02-25 从零开始开发一个 Chrome 插件教程 All In One
2021-02-25 pure CSS cube animation
2021-02-25 Emoji 表情符号词典 All In one