Linux Tutorials

Linux bash exit standing and find out how to set exit standing in bash

SEOClerks

Can you clarify bash exit standing code? How do I set bash exit standing in my Linux shell scripts?

Every Linux or Unix command returns a standing when it terminates usually or abnormally. You need to use worth of exit standing within the shell script to show an error message or run instructions. For instance, if tar command is unsuccessful, it returns a code which tells the shell script to ship an e-mail to sysadmins.

Adblock detected 😱

My web site is made doable by displaying on-line ads to my guests. I get it! Advertisements are annoying however they assist maintain this web site operating. It’s onerous to maintain the positioning operating and producing new content material when so many individuals block adverts. Please take into account donating cash to the nixCraft by way of PayPal/Bitcoin, or develop into a supporter utilizing Patreon.

Extra on Linux bash shell exit standing codes

Each Linux or Unix command executed by the shell script or consumer, has an exit standing.The exit standing is an integer quantity.For the bash shell’s functions, a command which exits with a zero (zero) exit standing has succeeded.A non-zero (1-255) exit standing signifies failure.If a command just isn’t discovered, the kid course of created to execute it returns a standing of 127. If a command is discovered however just isn’t executable, the return standing is 126.The entire Bash builtins return exit standing of zero in the event that they succeed and a non-zero standing on failure.

How do I show the exit standing of shell command?

You need to use particular shell variable known as $? to get the exit standing of the beforehand executed command. To print $? variable use the echo command/printf command. The syntax is:
command
echo $?
OR
/path/to/script.sh
command
date
echo $?
printf “%dn” $?
## run non-existence command ##
foobar13535
## show standing code ##
echo $?
Display bash exit status in Linux

Learn how to retailer the exit standing of the command in a shell variable

Assign $? to a shell variable. The syntax is:

date
standing=$?
echo “The date command exit standing : $

date
standing=$?
echo “The date command exit standing : $”

Linux exit standing and the conditional/record constructs

A easy shell script to find host identify (findhost.sh)

#!/bin/bash
# set var
FILE=“/and so on/hosts”
 
# get host identify
learn -p “Enter a hostname : “ hostname
 
# attempt to find hostname/pc identify in $FILE
grep -q -w $ $
 
# retailer exit standing of grep
# if discovered grep will return zero exit standing
# if not discovered, grep will return a nonzero exit standing
standing=$?
 
if check $standing -eq zero
then
echo “Host ‘$hostname‘ present in $FILE file.”
else
echo “Host ‘$hostname‘ not present in $FILE file.”
fi

#!/bin/bash
# set var
FILE=”/and so on/hosts” # get host identify
learn -p “Enter a hostname : ” hostname # attempt to find hostname/pc identify in $FILE
grep -q -w “$” “$” # retailer exit standing of grep
# if discovered grep will return zero exit standing
# if not discovered, grep will return a nonzero exit standing
standing=$? if check $standing -eq zero
then
echo “Host ‘$hostname’ present in $FILE file.”
else
echo “Host ‘$hostname’ not present in $FILE file.”
fi

Linux exit status in shell script with if command

Learn how to use the && and || operators with exit codes

The syntax is:

command && echo “success”
command || echo “failed”
command && echo “success” || echo “failed”

command && echo “success”
command || echo “failed”
command && echo “success” || echo “failed”

If a dir named “/tmp/foo” not discovered create it:
[ ! -d “/tmp/foo” ] && mkdir -p “/tmp/foo”
For instance, present utilization syntax when filename not handed because the command line arg:

#!/bin/Bash
_files=“$@”
 
## fail secure ##
[[ $_files == “” ]] &&
 
## proceed under ##

#!/bin/Bash
_files=”$@” ## fail secure ##
[[ “$_files” == “” ]] && echo “Utilization: $zero file1.png file2.png”; exit 1; ## proceed under ##

Right here is one other shell script that reveals utilization:

#!/bin/bash
set -e
I=~/bin/tags.deleted.410
O=“/tmp/https.www.cyberciti.biz.410.url.conf”
t=“$1”
[ ! -f $I ] &&
[ $t == “” ] && echo “Utilization: $zero number-of-urls-to-purge-from-$I; exit 11;
 
>$O
 
cat $I | kind | uniq | whereas learn -r u
do
uu=$
echo “~^$uu 1;” >>$O
completed
echo “* Config file created at $O …”
echo “* Putting in $O file on utls-wp-mg-www …”
~/bin/set up.py $O
echo “* Ship config to remainder of cluster nodes … “
~/bin/set up.py –sync –cluster –reload cbz-www
echo “* Constructing record purge urls for Cloudflare CDN …”
sleep 1
url=“”
whereas IFS= learn -r u
do
url=$url $u
completed <<<$(tail -$ $)
[ $url != “” ] && ~/bin/cloudflare.purge.urls.sh $url
[ -f $O ] && rm -f $O

#!/bin/bash
set -e
I=~/bin/tags.deleted.410
O=”/tmp/https.www.cyberciti.biz.410.url.conf”
t=”$1″
[ ! -f “$I” ] &&
[ “$t” == “” ] && >$O cat “$I” | kind | uniq | whereas learn -r u
do
uu=”$”
echo “~^$uu 1;” >>”$O”
completed
echo “* Config file created at $O …”
echo “* Putting in $O file on utls-wp-mg-www …”
~/bin/set up.py “$O”
echo “* Ship config to remainder of cluster nodes … ”
~/bin/set up.py –sync –cluster –reload cbz-www
echo “* Constructing record purge urls for Cloudflare CDN …”
sleep 1
url=””
whereas IFS= learn -r u
do
url=”$url $u”
completed <<<"$(tail -$ $)"
[ "$url" != "" ] && ~/bin/cloudflare.purge.urls.sh "$url"
[ -f "$O" ] && rm -f "$O"

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects by way of RSS/XML feed or weekly e-mail e-newsletter.

Source link

Related Articles

Leave a Reply

Back to top button