bash脚本的奇怪行为
我有两个脚本。bash脚本的奇怪行为,bash,shell,Bash,Shell,我有两个脚本。 script.conf #!/bin/bash set -o errexit set -o nounset set -o pipefail # set -o xtrace [ -f $HOME/Templates/bash_colors.sh ] && source $HOME/Templates/bash_colors.sh INFO=${INFO:-"1"} DEBUG=${DEBUG:-"0"} ERROR=${E
script.conf
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace
[ -f $HOME/Templates/bash_colors.sh ] && source $HOME/Templates/bash_colors.sh
INFO=${INFO:-"1"}
DEBUG=${DEBUG:-"0"}
ERROR=${ERROR:-"1"}
Red=${Red:-}
Yellow=${Yellow:-}
Cyan=${Cyan:-}
Color_Off=${Color_Off:-}
__msg_error() {
[[ "${ERROR}" == "1" ]] && echo -e "${Red}[ERROR]: $*${Color_Off}";
}
__msg_debug() {
[[ "${DEBUG}" == "1" ]] && echo -e "${Yellow}[DEBUG]: $*${Color_Off}";
}
__msg_info() {
[[ "${INFO}" == "1" ]] && echo -e "${Cyan}[INFO]: $*${Color_Off}";
}
和testConf.sh
#!/bin/bash
# INFO="0"
scriptDir="${BASH_SOURCE%/*}"
[ -f ${scriptDir}/script.conf ] && source ${scriptDir}/script.conf
INFO="0"
__msg_error "Test error1"
__msg_info "Test Info"
__msg_error "Test error2"
__msg_debug "Test debug"
#!/bin/bash
# src: https://stackoverflow.com/a/28938235/1959528
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
# High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White
奇怪的是,如果将INFO
设置为0,我希望两条错误消息都会得到响应,但事实并非如此。只打印第一个。我不明白为什么。也许你有个主意
当INFO
为0时,脚本似乎停止
查看跟踪:
++ INFO=1
++ DEBUG=0
++ ERROR=1
++ Red='\033[0;31m'
++ Yellow='\033[0;33m'
++ Cyan='\033[0;36m'
++ Color_Off='\033[0m'
+ INFO=0
+ __msg_error 'Test error1'
+ [[ 1 == \1 ]]
+ echo -e '\033[0;31m[ERROR]: Test error1\033[0m'
[ERROR]: Test error1
+ __msg_info 'Test Info'
+ [[ 0 == \1 ]]
但是为什么呢
bash_colors.sh
#!/bin/bash
# INFO="0"
scriptDir="${BASH_SOURCE%/*}"
[ -f ${scriptDir}/script.conf ] && source ${scriptDir}/script.conf
INFO="0"
__msg_error "Test error1"
__msg_info "Test Info"
__msg_error "Test error2"
__msg_debug "Test debug"
#!/bin/bash
# src: https://stackoverflow.com/a/28938235/1959528
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Underline
UBlack='\033[4;30m' # Black
URed='\033[4;31m' # Red
UGreen='\033[4;32m' # Green
UYellow='\033[4;33m' # Yellow
UBlue='\033[4;34m' # Blue
UPurple='\033[4;35m' # Purple
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
# High Intensity
IBlack='\033[0;90m' # Black
IRed='\033[0;91m' # Red
IGreen='\033[0;92m' # Green
IYellow='\033[0;93m' # Yellow
IBlue='\033[0;94m' # Blue
IPurple='\033[0;95m' # Purple
ICyan='\033[0;96m' # Cyan
IWhite='\033[0;97m' # White
# Bold High Intensity
BIBlack='\033[1;90m' # Black
BIRed='\033[1;91m' # Red
BIGreen='\033[1;92m' # Green
BIYellow='\033[1;93m' # Yellow
BIBlue='\033[1;94m' # Blue
BIPurple='\033[1;95m' # Purple
BICyan='\033[1;96m' # Cyan
BIWhite='\033[1;97m' # White
# High Intensity backgrounds
On_IBlack='\033[0;100m' # Black
On_IRed='\033[0;101m' # Red
On_IGreen='\033[0;102m' # Green
On_IYellow='\033[0;103m' # Yellow
On_IBlue='\033[0;104m' # Blue
On_IPurple='\033[0;105m' # Purple
On_ICyan='\033[0;106m' # Cyan
On_IWhite='\033[0;107m' # White
如果将INFO设置为
0
,则与\uuu msg\u error
相关的变量具有以下赋值
信息:0
调试:0
错误:1
因此,只打印错误。您的代码正在执行您告诉它的操作。在
scipt.conf
中设置了许多标志,特别是errexit
标志,并初始化错误、调试和信息标志:
INFO=${INFO:-"1"}
DEBUG=${DEBUG:-"0"}
ERROR=${ERROR:-"1"}
您可以创建使用以下标志的函数:
__msg_error() {
[[ "${ERROR}" == "1" ]] && echo -e "${Red}[ERROR]: $*${Color_Off}";
}
__msg_debug() {
[[ "${DEBUG}" == "1" ]] && echo -e "${Yellow}[DEBUG]: $*${Color_Off}";
}
__msg_info() {
[[ "${INFO}" == "1" ]] && echo -e "${Cyan}[INFO]: $*${Color_Off}";
}
(注意:您的错误
和信息
标志设置为1
)
然后在testConf.sh
中将INFO
设置为zero
,因此将输出的唯一消息是ERROR=1
INFO="0"
__msg_error "Test error1"
__msg_info "Test Info"
__msg_error "Test error2"
__msg_debug "Test debug"
运行脚本会给出预期的输出,“Test error 1”
和“Test error 2”
显示良好且非常红色,例如
$ bash ./testConf.sh
[ERROR]: Test error1
[ERROR]: Test error2
注意:但是必须在文件名之前提供“/”
,否则会出现我在第一条注释中讨论的情况,(您连接两个文件名,因为从$PWD运行时,如果没有”/“
,将没有路径信息,从而导致测试和源失败
当您抱怨只显示了一个输出时,set-o errexit
会在第一个错误时导致整个脚本退出。如果您想遵循所做的操作,只需将set-x
添加为脚本中!/bin/bash
下的第一行。您会发现退出是由于比较“测试信息”而触发的e> 失败[[0==\1]]
,然后退出跳过测试错误2
输出是正常行为
在conf.script
中,我建议在尝试源代码之前验证要源代码的文件的存在性和非空性质,并检查源代码是否正确完成,例如
colorfile="$HOME/tmpd/bash_colors.sh"
if [ ! -s "$colorfile" ]; then
printf "error: file not found or empty '%s'\n" "$colorfile" >&2
else
. "$colorfile" || {
printf "error: failed to source '%s'\n" "$colorfile" >&2
}
fi
(注意:您可以使用”。
将文件源代码转换为当前的。。
是原始源代码操作符,但后来还添加了源代码的别名
如果您在其他地方遇到问题,请告诉我。它可能是一个游离字符,例如卡在文件中的回车符,当我复制代码段时,它没有被复制。Guess-您正在使用bash scriptname
在$PWD中运行脚本,因此scriptDir=“${bash_SOURCE%/*}”将产生脚本名称(没有要从右侧修剪的路径信息)因此[-f${scriptDir}/script.conf]
失败--解释您的当信息为0时,脚本似乎停止了。
这不是猜测-ANSI颜色很好-但是如果您需要任何类型的可移植性,通常比它们更麻烦。(如果大量使用,更不用说它们对脚本可读性的影响——即使转义序列的名称很好)@DavidC.Rankin我尝试了不使用颜色的方法,但仍然无法使用。另外,script.conf
的源代码也正确。否则,即使是第一条错误消息也不会正确打印?如果您查看输出++Color\u Off='\033[0m'
来自script.conf
中的sourcingsource$HOME/Templates/bash_colors.sh
。您的下一个输出是+INFO=0
意味着sourcing${scriptDir}/script.conf
在testConf.sh
中失败了。Mh…我不确定我是否理解正确。我执行testConf.sh
。如果script.conf
的源代码失败了,我不会做任何与颜色相关的事情。它只会将INFO
设置为0,并尝试调用所有将失败的\uu msg.
en跟踪不起作用,因为set-o xtrace
是在script.conf
中设置的。好的,我同意你在那里说的。继续发布你的bash_colors.sh
这样我就可以把它们都拉下来,看看是怎么回事。删除set-o errexit
实际上解决了这个问题。现在我看到了两条错误消息。Thx。但是我我不知道[[“${INFO}”==“1”]]
在bash退出时被认为是一个错误。很高兴你把它解决了。一般来说,除非你在设置所有选项之前仔细阅读了man bash
,最好还是坚持使用set-x
进行初始调试。祝你的脚本编写好运!