:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量

链接:

须要定义一个名为failed的bash变量,并将值设置为False。当从cron作业调用我们的脚本时,特定的任务可能会失败,之后我须要将failed转换为True。基于$failed,我须要发送一封电子短信警告我的cron作业失败了。这么,怎样在Linux服务器上运行的shell脚本中申明和使用布尔变量呢?Bash中没有布尔值。并且shell脚本 变量,我们可以按照须要将shell变量的值定义为0(“False”)或1(“True”)。不过,Bash也支持布尔表达式条件。让我们瞧瞧怎样在Bash中组合这两个概念来申明布尔变量,并在运行在Linux、macOS、FreeBSD或类unix系统上的shell脚本中使用它们。在bash中申明布尔变量句型如下,定义如下内容failed=0#False

jobdone=1#True

##更具可读性的句型##

failed=false

jobdone=true如今,当$failed是数字(比如0或1)时,我们可以按以下方法检测它:

if [ $failed -eq 1 ]
then
echo "Job failed"
else
echo "Job done"
fi

就这样。

图片[1]-:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量-唐朝资源网

怎样在Shell脚本中申明和使用布尔变量(比如“true”和“false”)

其实shell脚本 变量,我们可以将它们定义为字符串,并使我们的代码更具可读性:

#!/bin/bash
# Declare it as string
failed="false"

if [ "$failed" == "true" ]
then
echo "Job failed"
else
echo "Job done"
fi

或则

# set it to true
email_sent=true
# ...do your stuff and flip email_sent to 'false' if needed ...
if [ "$email_sent" = true ]
then
echo 'Data logged and email sent too.'
else
echo 'ALERT: Operation failed.'
logger 'ALERT: Operation failed.'
fi

在bash下定义布尔变量的取代句型如下:

图片[2]-:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量-唐朝资源网

#LetusDeclareTwoBooleanVariables

#Setthisonetotrue

jobstatus=true

#Checkit

if[“$jobstatus”=true];then

echo’Okay:)’

图片[3]-:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量-唐朝资源网

else

echo’Noop:(‘

fi

#DoublebracketformatsyntaxtotestBooleanvariablesinbash

bool=false

if[[“$bool”=true]];then

echo’Done.’

else

echo’Failed.’

fi

让我们对其进行测试:

图片[4]-:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量-唐朝资源网

图片[5]-:如何在Linux服务器上运行的shell脚本中声明和使用布尔变量-唐朝资源网

shell脚本示例中的Bash布尔变量下边是一个示例脚本:

#!/bin/bash
# Purpose: Backup stuff from /data/apps
# Tested on : AWS EC2 with EFS and Ubuntu 20.04 Pro servers
# ---------------------------------------------------------
source "/apps/.keychain/$HOSTNAME-sh"
source "/apps/scripts/cli_app.sh"

# Set failed to 'False'
failed=0

D="/nfsefs/ec2mum/prodwwwroot"
log="/tmp/server.log.$$.txt"

# Log everything to our file
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>"${log}" 2>&1

# Backup all web servers
for s in www-0{1..8}
do
/usr/bin/rsync -az --delete backupt@${s}:/data/apps/ ${D}/${s}/
# set to 'true' when rsync failed and continue loop
[ $? -ne 0 ] && failed=1
done

# See if rsync failed in loop
if [ $failed -eq 1 ]
then
echo "$0 script error: rsync backup failed. See attached log file." | mail -A ${log} -s "$HOSTNAME - LXD backup failed" -r sysuser@my-corp-tld alert@somewhere-tld
push_to_mobile "$0" "Dear sysadmin,

Backup failed at $HOSTNAME at $(date).

---- log:start ---

$(<${log})

--- log:end --

--
Yours faithfully,
$0"

fi
[ -f "${log}" ] && rm -f "${log}"

Bash布尔测试首先,定义一个日志文件:log=”/tmp/rsnapshot.$$.txt”让我们运行rsnapshot命令:/usr/bin/rsnapshot”$1″2>&1|$HOME/bin/error-scanner.pl>”${log}”在$status中获取bash命令的退出状态,如下所示:status=$?接出来在我们的$log文件中搜索ERROR:alogs=”$(egrep-w’^ERROR:|ERROR’$log)”假如$status不为零(命令失败)或$alogs不为空(由$HOME/bin/error-scanner.pl报告错误),这么通过电子短信/文本消息通知系统管理员或开发人员:

if [ $status -ne 0 ] ||  [ "$alogs" != "" ]; then
sub="Backup job failed at $HOSTNAME"
mail -A "$log" -s "$sub" -r sys@somewhere-tld sysadmin@gmail-tld <<< "$0 script ended with errors when we ran /usr/bin/rsnapshot "$1" $alogs"
push_to_mobile "$0" "$sub

$0 script ended with errors when we ran /usr/bin/rsnapshot "$1"

$alogs

See email for detailed log."


else
sub="Backup successful at $HOSTNAME"
#push_to_mobile "$0" "$sub. See email for detailed backup log." >/dev/null
#mail -A "$log" -s "$sub" -r sys@somewhere-tld sysadmin@gmail-tld <<< "$0 /usr/bin/rsnapshot ran successfully"$1" $alogs"
fi

最后,删掉$log文件:[-f”$log”]&&rm-f”$log”总结本文我们讲解了怎样在Linux或类Unix系统的shell脚本/bash中申明和使用布尔变量。使用man命令查看以下指南页:$manbash

$helptest

$helpif

…END…

© 版权声明
THE END
喜欢就支持一下吧
点赞259赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容