exit function gets skipped in Unix Shell script - unix

I have the following as part of my code. But when it reaches this if block, if the condition evaluates to true, the print function is getting executed but the exit function is not. It just gets skipped. Is it because of some mistake in the if condition? Or will I have to share my full code?
if ( grep -i ERROR /tmp/swm_pkg_ros )
then
print "\nFailed. ...EXITING"
print "\n....you will need to fix the problem and rerun\n"
exit
else
print "Successful"
fi

It works fine for me:
if ( grep -i ERROR /tmp/swm_pkg_ros )
then
echo -e "\nFailed. ...EXITING\n....you will need to fix the problem and rerun"
exit 1
else
echo "Successful"
fi
you may print exit code echo $?

Related

My exit commands are not working right

I am not exactly sure if i am using the exits correctly. But when i execute the code with something that prints the usage statement it should stop there.
It should do one or the other. In my case it is doing both.
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
fi
exit=1
path=$(echo $PATH | tr ":" " ")
for dir in $path
do
if [[ -x "$dir/$cmd" && -r "$dir/$cmd" ]]
then
echo "$dir/$cmd"
exit 0
fi
done
echo "$cmd not on $PATH"
exit=0
OUTPUT:
[112] ./findcmd
usage: ./findcmd command
/usr/local/bin/ **this should not be here
[113] ./findcmd ping
/usr/bin/ping
You use exit correctly to stop the script after your dir/cmd gets printed; try using it that way elsewhere.
Keep in mind that your first exit, once correct, will stop the script before the loop whether usage was printed or not.
It should be exit [n].
Within a script, an exit nnn command may be used to deliver an nnn
exit status to the shell (nnn must be an integer in the 0 - 255
range).
And it should be inside the IF/FOR block.
Like this:
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
exit 1
fi
## rest of code to execute if args are correct

How to get exit status of R script run in shell script

Suppose I'm running a Rscript from inside this shell script
#!/bin/bash
RES=$(./abc.R 100)
r_status=echo $?
There is some code in abc.R which stops its execution
#!/usr/bin/env Rscript
...
...
if(nrow(status) == 0)
{ stop("The list id is not present in requests table. Please check.") } else if (status != 'COMPLETED')
{ stop("The list is not in COMPLETED state. Please check.")}
...
...
I am not able to capture the exit status of abc.R in my shell script. It stops R execution and even quits from the shell script to the prompt.
Is there any way I can capture R's exit status.
Just run the script you want.
make sure it returns the correct exit status when finishing its run.
This should work:
#!/bin/bash
./abc.R 100
if [ $? == 0 ]; then
echo "Your script exited with exit status 0"
exit 0
see more here:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

Running custom zsh function for tmux status bar not displaying output

I'm wrote a function called test_status that I am trying to incorporate in my tmux status bar. To give some background, my tests will output to a file called .guard_result with either success or failure and the test_status function reads from that file and echoes a 💚 if my tests are passing and a ❤️ if they are failing.
The good news is running test_status works just fine, I'm just having trouble getting it to work with tmux. What am I missing here?
# ~/.oh-my-zsh/custom/aliases.zsh
function test_status {
if [ ! -f "./.guard_result" ]; then
echo "?"
return 1
fi
result="$(cat ./.guard_result)"
if [[ $result == *"success"* ]]
then
echo "💚";
elif [[ $result == *"fail"* ]]
then
echo "❤️";
fi
}
This function works... Here is Tmux configuration (which doesn't show result):
# ~/.tmux.conf
set -g status-right "#(test_status) #[fg=colour245]%d %b %Y #[fg=white]:: #[fg=colour245]%l:%M %p"
I know I must be missing something simple... Thanks for your help!
tmux passes shell commands to /bin/sh not zsh. And even if tmux would use zsh, the function would not be available in that context as ~/.zshrc, which loads oh-my-zsh, is only read for interactive shells.
In order to get the the output of test_status into tmux, I would suggest to put the function into a zsh script and call that.
You can either source ~/.oh-my-zsh/custom/aliases.zsh from within the script and then call test_status:
#!/usr/bin/zsh
# ^ make sure this reflects the path to zsh (`type zsh`)
source ~/.oh-my-zsh/custom/aliases.zsh
test_status
Or you can just put the entire function into the script, so as to not clutter alias.zsh:
#!/usr/bin/zsh
function test_status {
if [ ! -f "./.guard_result" ]; then
echo "?"
return 1
fi
result="$(cat ./.guard_result)"
if [[ $result == *"success"* ]]
then
echo "💚";
elif [[ $result == *"fail"* ]]
then
echo "❤️";
fi
}
Safe the script somewhere (e.g. /path/to/test_status.zsh), make it executable (chmod a+x /path/to/test_status.zsh) and call it by path in the tmux configuration.

Error Handling while running sqlplus from shell scripts

I need to validate whether DB connection is success/failure.
This is my code
report=`sqlplus -S /nolog << EOF
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
connect <<username>>/<<Password>>#hostname:port
set linesize 1500
set trimspool on
set verify off
set termout off
set echo off
set feedback off
set heading on
set pagesize 0
spool extract.csv
<<My SQL Query>>
spool off;
exit;
EOF`
I have tried the below option based on the thread Managing error handling while running sqlplus from shell scripts but its picking the first cell value rather than the connection status.
if [ $report != 0 ]
then
echo "Connection Issue"
echo "Error code $sql_return_code"
exit 0;`enter code here`
fi
Please advise.
I needed something similar but executed it a bit differently.
First, I have list.txt which contains the databases that I would like to test. I am using wallet connections but this could be edited to hold username/password.
list.txt:
DB01 INSTANCE1.SCHEMA1
DB02 INSTANCE2.SCHEMA2
DB03 INSTANCE3.SCHEMA3
DB04 INSTANCE4.SCHEMA4
I have OK.sql which contains the query that I want to run on each database.
OK.sql:
select 'OK' from dual;
exit
Last, I user test.sh to read list.txt, attempt to connect and run OK.sql on each line, and record the result in (drumroll) result.txt.
test.sh:
. /etc/profile
rm result.txt
while read -r name wallet; do
echo "BEGIN-"$name
if (sqlplus -S /#$wallet #OK.sql < /dev/null | grep -e 'OK'); then
echo $name "GOOD" >> result.txt
else
echo $name "BAD" >> result.txt
fi
echo "END-"$name
done < list.txt
After the run check your result.txt.
result.txt:
DB01 BAD
DB02 GOOD
DB03 GOOD
DB04 GOOD
I hope this helps.

Error handling in unix shell ksh

Can anyone guide to a document or explain on the below
how to use error handling in ksh.
How does Unix work on unhandled errors(like error happened in the
subscript etc..).
From ksh man page.
Unhandled errors
Errors detected by the shell, such as syntax errors, cause the shell to return a non-zero exit status. If the shell is being used
non-interactively, then execution of the shell
file is abandoned UNLESS the error occurs inside a subshell in which case the subshell is abandoned.
Error handling
Basically check exit/return codes to handle errors:
if [ $exit_code != 0 ]; then
# Your error handler
fi
Example
test_handler() {
ls file_not_present
if [ $? -eq 2 ]; then
echo "Handler for No such file or directory"
elif [ $? -ne 0]; then
echo "Handler for any other exception"
else
echo "Succesful execution"
fi
}
Will throw:
ls: cannot access non_file: No such file or directory
Handler for No such file or directory
But if the command does not exit:
test_handler() {
l file_not_present
if [ $? -eq 2 ]; then
echo "Handler for No such file or directory"
elif [ $? -ne 0 ]; then
echo "Handler for any other exception"
else
echo "Succesful execution"
fi
}
The output will be:
l: not found [No such file or directory]
Handler for any other exception
The shell returns
the exit status of the last command executed
(see also the exit command above). Run time errors detected by the shell are reported by printing the command or function name and
the error condition. If the line number that
the error occurred on is greater than one, then the line number is also printed in square brackets ([]) after the command or function
name.

Resources