When Comment Out 'exit', Code Perform Well
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am following an instruction and confuse with the usage of exit
FILE=~/.bashrc
if [ -e "$FILE" ] ; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ] ; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ] ; then
echo "$FILE is writabe."
fi
if [ -x "$FILE" ]; then
echo '$FILE is executable/searchable.'
fi
else
echo '$FILE does not exist'
exit 1
fi
exit
Run and come by
$ bash test_file.sh
/Users/me/.bashrc is a regular file.
/Users/me/.bashrc is readable.
/Users/me/.bashrc is writabe.
If comment out commands of exit
, the output stay unchanged.
What's its function?
Could I leave out the exits when familiar with the language.
bash
add a comment |Â
up vote
1
down vote
favorite
I am following an instruction and confuse with the usage of exit
FILE=~/.bashrc
if [ -e "$FILE" ] ; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ] ; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ] ; then
echo "$FILE is writabe."
fi
if [ -x "$FILE" ]; then
echo '$FILE is executable/searchable.'
fi
else
echo '$FILE does not exist'
exit 1
fi
exit
Run and come by
$ bash test_file.sh
/Users/me/.bashrc is a regular file.
/Users/me/.bashrc is readable.
/Users/me/.bashrc is writabe.
If comment out commands of exit
, the output stay unchanged.
What's its function?
Could I leave out the exits when familiar with the language.
bash
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am following an instruction and confuse with the usage of exit
FILE=~/.bashrc
if [ -e "$FILE" ] ; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ] ; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ] ; then
echo "$FILE is writabe."
fi
if [ -x "$FILE" ]; then
echo '$FILE is executable/searchable.'
fi
else
echo '$FILE does not exist'
exit 1
fi
exit
Run and come by
$ bash test_file.sh
/Users/me/.bashrc is a regular file.
/Users/me/.bashrc is readable.
/Users/me/.bashrc is writabe.
If comment out commands of exit
, the output stay unchanged.
What's its function?
Could I leave out the exits when familiar with the language.
bash
I am following an instruction and confuse with the usage of exit
FILE=~/.bashrc
if [ -e "$FILE" ] ; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ] ; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ] ; then
echo "$FILE is writabe."
fi
if [ -x "$FILE" ]; then
echo '$FILE is executable/searchable.'
fi
else
echo '$FILE does not exist'
exit 1
fi
exit
Run and come by
$ bash test_file.sh
/Users/me/.bashrc is a regular file.
/Users/me/.bashrc is readable.
/Users/me/.bashrc is writabe.
If comment out commands of exit
, the output stay unchanged.
What's its function?
Could I leave out the exits when familiar with the language.
bash
edited 2 days ago
Rui F Ribeiro
34.7k1269113
34.7k1269113
asked Apr 3 at 2:01
JawSaw
29410
29410
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The exit
command not only exits a script, but also sets an exit code
, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash
and similar shells, one can view or interrogate the exit code by examining shell variable $?
.
Also, BTW, you have an indentation problem. The else
clause should be out-dented to the same level as the initial if
statement...
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Usingexit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
â user1404316
Apr 3 at 3:15
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The exit
command not only exits a script, but also sets an exit code
, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash
and similar shells, one can view or interrogate the exit code by examining shell variable $?
.
Also, BTW, you have an indentation problem. The else
clause should be out-dented to the same level as the initial if
statement...
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Usingexit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
â user1404316
Apr 3 at 3:15
add a comment |Â
up vote
2
down vote
accepted
The exit
command not only exits a script, but also sets an exit code
, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash
and similar shells, one can view or interrogate the exit code by examining shell variable $?
.
Also, BTW, you have an indentation problem. The else
clause should be out-dented to the same level as the initial if
statement...
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Usingexit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
â user1404316
Apr 3 at 3:15
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The exit
command not only exits a script, but also sets an exit code
, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash
and similar shells, one can view or interrogate the exit code by examining shell variable $?
.
Also, BTW, you have an indentation problem. The else
clause should be out-dented to the same level as the initial if
statement...
The exit
command not only exits a script, but also sets an exit code
, which by convention is zero for a successful exit, and some other integer for an error, so in your script, its purpose is to indicate to the caller (either another script / program, or a user) that the program exited with an error. In bash
and similar shells, one can view or interrogate the exit code by examining shell variable $?
.
Also, BTW, you have an indentation problem. The else
clause should be out-dented to the same level as the initial if
statement...
answered Apr 3 at 2:19
user1404316
2,314520
2,314520
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Usingexit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
â user1404316
Apr 3 at 3:15
add a comment |Â
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Usingexit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.
â user1404316
Apr 3 at 3:15
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
so, it's not a compulsive requirement?
â JawSaw
Apr 3 at 2:38
@Tool - Using
exit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.â user1404316
Apr 3 at 3:15
@Tool - Using
exit
commands with exit codes is not required, but it is good practice, so it's worthwhile to get into the habit, and to assign unique and consistent exit codes for uniqe error conditions. See here for a list of the error codes used by linux itself: unix.stackexchange.com/a/326811/153769 , but do be aware that other operating systems may have other standards.â user1404316
Apr 3 at 3:15
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435176%2fwhen-comment-out-exit-code-perform-well%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password