Exit causes root logout if script user executes the script outside of the script directory - bash

Bash Script Bug User Gets Logged Out Of Root On Exit
I have a big Bash script which requires to exit the script and restart it if some user input is invalid. I got a weird bug, where if the user does execute the script outside of the directory in which the script is located, he gets logged out of root. But if the script is executed inside the directory where the script is located, this doesn't occur.
I already tried removing the exit but that only makes things worse.
#!/bin/bash
some_function() {
read -p "Enter something: "
# Some commands
if [[ $? -gt 0 ]]; then
echo "error"
. /whatever/location/script.sh && exit 1
fi
}
The expected result is, that the script just restarts and exits the process the user ran. The actual result is just like that, but the user gets logged out of root if the script is terminated after that.

You did not say so, but it seems you are sourcing the script containing this function that exits. If you are sourcing it, then it is as if each command is typed at the command line... so exit will logout of whatever shell you are running.
For a script that is always sourced, use return instead of exit
If you don't know whether the script will be sourced or not, you'll need to detect it and choose the proper behavior based on how it was called. For example:
some_function() {
read -p "Enter something: "
# Some commands
if [[ $? -gt 0 ]]; then
echo "error"
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
# sourced
. /whatever/location/script.sh && return 1
else
# not sourced
. /whatever/location/script.sh && exit 1
fi
fi
}

Related

Writing a shell script while using "Packages" to make a .pkg file

I really need your help with this:
The thing is: I am trying to build my app into .pkg file, at the same time I want to integrate node.js into my .pkg installation file and it will be installed if the OS doesn't have nodejs.
When I try to write a script to judge whether the user has already installed the node, I was stuck by "return value of the external script". I try my script at the end with 'echo' 'return' 'exit' but still not work.enter image description here
Here is the screenshot of "Packages" when I try to insert the script..
And this is the script I wrote.`#!/bin/bash
OUTPUT="$(node -v)"
echo ${OUTPUT}
if [[ $OUTPUT = "" ]];
then
echo "1"
return 1
#no node
else
echo "0"
return 0
#node found
fi
`
Pls help me
This script will run the "node -v" command and send output (stderr and stdout) to /dev/null; nothing is displayed to user. The if condition checks if the command ran successfully and sets the exit status to 0 or 1 depending on the outcome.
#/bin/bash
main() {
node -v >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
return 0
else
return 1
fi
}
main

Exit if in a script, do not exit if using a terminal/tty

If the user is entering commands in a terminal, I want to echo an error statement, but I do not want the terminal to close, so I have this:
if [[ "$fle" =~ [^a-zA-Z0-9] ]]; then
echo "quicklock: lockname has invalid chars - must be alpha-numeric chars only."
if [[ -t 1 ]]; then
# if we are in a terminal just return, do not exit.
return 1;
else
exit 1;
fi
fi
however the if [[ -t 1 ]]; then does not seem to work, the terminal window I am using just closes immediately, so I think exit 1 is being called.
The -t flag checks if any of the standard file descriptors are open, and specifically [ -t 1 ] will represent if the STDOUT is attached to tty, so when running from the terminal, it will always assert this condition as true.
Also the return keyword is applicable only when running a function to break out of it instead of terminating the shell itself. Your claim of terminal window closing because of hitting exit 1 when running from script, could happen only if you source the script, (i.e. in the same shell) and will not happen if you execute the script in a sub-shell.
You can use a construct for a no-action in scripts by just doing : in the if condition as
if [[ -t 1 ]]; then
# if we are in a terminal just return, do not exit.
:
Also -t is defined by POSIX because of which you can do just [ -t 1 ].
This is actually what ended up working for me:
function on_conditional_exit {
if [[ $- == *i* ]]; then
# if we are in a terminal just return, do not exit.
echo -e "quicklock: since we are in a terminal, not exiting.";
return 0;
fi
echo -e "quicklock: since we are not in a terminal, we are exiting...";
exit 1;
}
the test is to see if we are in terminal or in a script somewhere...if we are interactive, we are in a terminal..

How to stop execution in script which may have been sourced or directly executed

If my script gets sourced with
. ./my_script.sh
source ./my_script.sh
then to stop execution in the script, I would use return.
If my script is directly executed with
./my_script.sh
bash ./my_script.sh
then I'd insert an exit.
If I don't know whether the user will source it or directly execute it, how can I cleanly stop the script without killing the terminal it was called from?
Preferably, the code snippet should be able to terminate the script even if it were placed inside one of the script's functions.
Try the following:
ec=0 # determine the desired exit code
return $ec 2>/dev/null || exit $ec
return will succeed if the script is being sourced, otherwise exit will kick in. The 2>/dev/null suppresses the error message in case the script is not sourced.
The net effect is that the script will terminate with the desired exit / return code, regardless of whether it was sourced or not.
Update: The OP wants to be able to exit the script from inside a function within the script.
The only way I can think of is to place all of your script's code in a subshell and call exit from inside the functions inside that subshell; then place the return 2>/dev/null || exit command after the subshell (as the only statement outside the subshell):
#!/usr/bin/env bash
( # subshell to place all code in
foo() {
exit 1 # exit the subshell
}
foo # invoke the function
)
# Terminate script with exit code from subshell.
ec=$?; return $ec 2>/dev/null || exit $ec
Here's one way to find out which method was used to invoke the script.
if [[ "$0" == "bash" ]]; then
echo "source 'file' was used"
else
echo "bash 'file' was used"
fi
Update, In response to comment by #mklement0:
If your script is named my_script.sh, you can use
b=$(basename "$0")
if [[ "$b" == "my_script.sh" ]]; then
echo "bash 'file' was used"
else
echo "source 'file' was used"
fi

Calling script from script

How can I have my shell script echo to me that the script that it calls has failed?
#!/bin/sh
test="/Applications/test.sh"
sh $test
exit 0
exit 1
#!/bin/sh
if sh /Applications/test.sh; then
echo "Well done $USER"
exit 0
else
echo "script failed with code [$?]" >&2
exit 1
fi
The /Applications/test.sh script should be well coded to exit with conventional status. 0 if it's ok and > 0 if it fails.
Like you can see, no need to test the special variable $?, we use boolean expression directly.
I usually take the following approach:
#!/usr/bin/env bash
test="/Applications/test.sh"
sh "${test}"
exit_status=$?
if [[ ${exit_status} ]] ; then
echo "Error: ${test} failed with status ${exit_status}." >&2
else
echo "Success!"
fi
In terms of best practice, you should not. If a script fails, it should emit an error message before it terminates so that its parent doesn't have to. The main reason for this is that the process knows why it is failing, while the parent can only guess. In other words, you should just write:
#!/bin/sh
test="/Applications/test.sh"
sh $test
Although really, it would be more typical to just write:
#!/bin/sh
/Applications/test.sh
test.sh will emit the necessary error message, and your script will return the same value as did test.sh. Also, in its current form your script will always be successful, even if test.sh actually failed because exit 0; exit 1 is pretty pointless: the exit 1 will never be called.

Getting exit code of last shell command in another script

I am trying to beef up my notify script. The way the script works is that I put it behind a long running shell command and then all sorts of notifications get invoked after the long running script finished.
For example:
sleep 100; my_notify
It would be nice to get the exit code of the long running script. The problem is that calling my_notify creates a new process that does not have access to the $? variable.
Compare:
~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID"
ls: nonexisting_file: No such file or directory
exit code: 1
PPID: 6203
vs.
~ $: ls nonexisting_file; my_notify
ls: nonexisting_file: No such file or directory
exit code: 0
PPID: 6205
The my_notify script has the following in it:
#!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"
I am looking for a way to get the exit code of the previous command without changing the structure of the command too much. I am aware of the fact that if I change it to work more like time, e.g. my_notify longrunning_command... my problem would be solved, but I actually like that I can tack it at the end of a command and I fear complications of this second solution.
Can this be done or is it fundamentally incompatible with the way that shells work?
My shell is Z shell (zsh), but I would like it to work with Bash as well.
You'd really need to use a shell function in order to accomplish that. For a simple script like that it should be pretty easy to have it working in both zsh and bash. Just place the following in a file:
my_notify() {
echo "exit code: $?"
echo "PPID: $PPID"
}
Then source that file from your shell startup files. Although since that would be run from within your interactive shell, you may want to use $$ rather than $PPID.
It is incompatible. $? only exists within the current shell; if you want it available in subprocesses then you must copy it to an environment variable.
The alternative is to write a shell function that uses it in some way instead.
One method to implement this could be to use EOF tag and a master script which will create your my_notify script.
#!/bin/bash
if [ -f my_notify ] ; then
rm -rf my_notify
fi
if [ -f my_temp ] ; then
rm -rf my_temp
fi
retval=`ls non_existent_file &> /dev/null ; echo $?`
ppid=$PPID
echo "retval=$retval"
echo "ppid=$ppid"
cat >> my_notify << 'EOF'
#!/bin/bash
echo "exit code: $retval"
echo " PPID =$ppid"
EOF
sh my_notify
You can refine this script for your purpose.

Resources