I'm trying to organize a root user check. If the program fails, we get exit 1. Nothing happens when the script is executed. Why?
Maybe fail in the script?
#!/bin/sh
[ "$(id -u)" = 0 ] || exit 1
echo "Running"
Related
I have this code but I don't understand it very well.
Can you explain to me how the finish variable gets the value.
what does this assignment "finish=$?" mean?
or if they have any documentation to help me understand.
Thanks in advance.
#!/usr/bin/bash
/usr/bin/java -jar Report.jar $user $pass $base $dateini $datefin $iphost >> file.log 2>&1
finish=$?
if [ $finish -eq 0 ]; then
echo "Report executed successfully" >> file.log
exit 0
else
echo "There was an error in the report" >> file.log
exit 1
fi
The exit status of the last command you ran (/usr/bin/java -jar ...) is stored in the special parameter $?.
Commands executed by the shell script or user have a exit status being either 0 (meaning the command was successful without errors), or a non-zero (1-255) value indicating the command was a failure.
Check here for further information
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
}
I have a shell script which updates different firmwares with different executables.
I need to know if one of the executable has hung and not returning back to shell.
Can I introduce any kind of timeout ?
Sample shell script below. How to handle if updatefw command hangs and does not return.
#!/bin/sh
updatefw -c config.cfg
if [ $? != 0 ]; then
echo "exec1 failed"
exit 1
fi
exit 0
I suggest with timeout from GNU core utilities:
#!/bin/bash
timeout 30 updatefw -c config.cfg
if [[ $? == 124 ]]; then
echo "update failed"
exit 1
fi
When timeout quits updatefw, the return code is 124.
I assume here that the update will never take longer than 30 seconds.
My script is executed by Cron and every 2 min checks if xxx is running. If it is not in the process then the script will run it. The problem is that sometimes it runs it several times.
My problem is how to detect that the program is running several times?
How does bash detect that the pidof function returns several rather than one pid?
#!/bin/bash
PID=`pidof xxx`
if [ "$PID" = "" ];
then
cd
cd /home/pi
sudo ./xxx
echo "OK"
else
echo "program is running"
fi
You can use this script for doing the same. It will make sure script is executed once.
#!/bin/bash
ID=`ps -ef|grep scriptname|grep -v grep|wc -l`
if [ $ID -eq 0 ];
then
#run the script
else
echo "script is running"
fi
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.