Setting exit codes in Unix shell scripts - shell

I have test.sh which has multiple return condition and test1.sh just echo statement.When i run test2.sh my logic should run test.sh process the file i.e "File successfully" and call test1.sh script .It should not run the test1.sh script when other else condition was executed.i.e "File not successfully", "Input file doesn't exists in directory"
The problem i am facing is when it is executing other condition like "File not successfully", "Input file doesn't exists in directory" it is not retuning "1" as specified exit code but in turn returning 0 i.e from the OS means job was successful. So i am getting "0" from test.sh for all the different condition so test1 .sh is getting called irrespective if the file processed failed etc.Pls advice with the return code
test.sh
FILES=/export/home/input.txt
cat $FILES | nawk -F '|' '{print $1 "|" $2 "|" }' $f > output.unl
if [[ -f $FILES ]];
then if [[ $? -eq 0 ]]; then
echo "File successfully"
else
echo "File not successfully"
exit 1
fi
else
echo "Input file doesn't exists in directory" exit 1 fi
========================================================================
test1.sh
echo "Input file exists in directory"
test2.sh
echo "In test2 script"
./test.sh
echo "return code" $?
if [[ $? -eq 0 ]]; then
echo "inside"
./test1.sh
fi

You're overwriting $? when you use it in echo - after that, it contains the exit code of echo itself. Store it in a variable to avoid this.
echo "In test2 script"
./test.sh
testresult=$?
echo "return code" $testresult
if [[ $testresult -eq 0 ]]; then
echo "inside"
./test1.sh
fi
Edited to add: it's hard to tell what you want from test.sh as the code you pasted is incomplete and doesn't even run. It looks like you meant the cat to be inside the if, because otherwise it errors when the input file is missing, and your $? test does nothing. So I rearranged it like this:
FILES=input.txt
if [[ -f $FILES ]]; then
cat $FILES | awk -F '|' '/bad/{ exit 1 }'
if [[ $? -eq 0 ]]; then
echo "File processed successfully"
else
echo "File processing failed"
exit 1
fi
else
echo "Input file doesn't exist in directory"
exit 1
fi
I've changed the awk script to demonstrate the conditions all work: now, if I put the word bad in input.txt you'll see the "File processing failed" message, otherwise you see success; remove the file and you'll see the input file doesn't exist message.

Related

Checking the existence of files

The following script checks whether FILE is in the directory.
The FILE includes 'name.pdf'
How to verify, that files name*.pdf are also in the directory?
if [ -f "$FILE" ]; then # ----> how to add the condition that name*.pdf is also OK
echo "OK $f"
else
echo "!!! Not OK $f"
fi
You can do this:
if [ -f name*.pdf ]; then
echo "OK $f"
else
echo "!!! Not OK $f"
fi
EDIT: as stated in the comments this method won't work if more than one file match, so I've found another way:
name=name*.pdf;
if compgen -G $name > /dev/null; then
echo "OK $f"
else
echo "!!! Not OK $f"
fi
Does this do what you want?
# List the files, but stream any output to /dev/null
# We are only interested in the exit status
if ls name*.pdf >/dev/null 2>&1; then
echo File matching pattern exist
else
echo Files matching pattern do not exist
fi
This works because if is checking the exit status of the ls command. This is exactly how if [ 'some condition' ] works, which is a shortcut for if test 'some condition': if is checking the exit status of test. ls has an exit status of 0 if it finds files, otherwise it's 1 or 2 both of which if will evaluate as false.
If you want a more general solution, you can define the prefix and extension as variables:
prefix='name' #You can define the prefix and extension as variables
ext='pdf'
if $prefix*.$ext >/dev/null 2>&1; then
#the rest of the code is the same as earlier.
You could follow something like below
#!/bin/bash
if [[ $(find . -name "name-*.pdf") ]]; then
echo "File Exists"
else
echo "File Doesnt exists"
fi

redirection using script parameters in bash

edit: thank you I think that syntax wise the script is now running ok.
My script takes 4 parameters:
log_file_name
program_name
input_file_name
output_file_name
I want to redirect the input file into the program (in c) and then redirect the output into the log file.
then check differences with the desired given output file (parameter 4).
what is the most elegant way to do this?
I tried the following code which didn't work-
#!/bin/bash
$2.c < $3 > $1
var = `diff $1 $4`
if [[ var=="" ]]
then echo "Out files match"
exit 0
fi
echo "Out files mismatch"
exit 1
I have another script which compiles the .c file beforehand
#!/bin/bash
gcc -Wall -o $2 $2.c &> $1
var=`grep -e warnings -e errors $1`
if [[ $var == "" ]]
then echo "Compile succeeded"
exit 0
fi
echo "Compile failed"
exit 1
#!/bin/bash
var=`diff $1 $4`
if [[ $var == "" ]];then
echo "Out files match"
exit 0
else
echo "Out files mismatch"
echo " < : Actual output "
echo " > : Excepted output "
diff $1 $4 | grep -v "^---" | grep -v "^[0-9c0-9]"
exit 1
fi
Try above code to difference between output log and actual output

From bash to ksh - script throws errors but still works

I have created a simple BASH script that checks every hour for the presence of a file on a remote server. It worked error-free until I was asked to move it to a server that runs KSH.
The portion of code that errors-out is this one:
connect_string=$UID#$SERVER:$srcdir/$EVENTFILE
result=`sftp -b "$connect_string" 2>&1`
if [ echo "$result" | grep "not found" ]; then
echo "not found"
else
echo "found"
fi
These are the errors it throws:
-ksh: .[51]: [: ']' missing
grep: ]: No such file or directory
found
It still runs though and confirms that the file I am polling for is there but I need to fix this. I changed the if statement like so
if [[ echo "$result" | grep "not found" ]]; then
but it fails right away with this error
-ksh: .: syntax error: `"$result"' unexpected
What am I missing?
Your basic syntax assumptions for if are incorrect. The old [...] syntax, calls the test builtin, and [[...]] is for textual pattern matching.
As #shelter's comment, the correct syntax is:
connect_string="$UID#$SERVER:$srcdir/$EVENTFILE"
result=`sftp -b "$connect_string" 2>&1`
if echo "$result" | grep "not found" ; then
echo "not found"
else
echo "found"
fi
But this is an unnecessary use of the external grep program, you can use shell text comparison:
if [[ $result == *not\ found* ]] ; then
echo "not found"
else
echo "found"
fi
(tested with bash and ksh)
Your solution:
EXIT=`echo $?`
if [ $EXIT != 0 ]
then
...
fi
Can be improved. First, if you are going to do an arithmetic comparison, then use ((...)), not test, and I can't figure out why you have the EXIT variable:
if (( $? != 0 ))
then
...
fi
But to go full circle, you actually only need:
if sftp -b "$connect_string" 2>&1
then
...
fi
echo "$result" | grep "not found"
#capture exit status code from previous command ie grep.
if [[ $? == 0 ]]
than
echo "not found"
else
echo "found"
fi
It appears you're struggling with a basic tenet of bash/ksh control structures.
Between the if and the then keywords, the shell expects one or more commands, with
the last command in the series deciding how the if statement is processed.
The square brackets are only needed if you actually need to perform a comparison. Internally they are equivalent to the test command - if the comparison succeeds, it
results in an exit status of 0.
Example:
$ [ a == a ]
$ echo $?
0
$ [ a == b ]
$ echo $?
1
Which is equivalent to:
$ test a == a
$ echo $?
0
$ test a == b
$ echo $?
1
I changed my approach to this.
connect_string=$UID#$SERVER:$srcdir/$EVENTFILE
result=`sftp "$connect_string" 2>&1`
EXIT=`echo $?`
if [ $EXIT != 0 ]
then
echo "file not found"
exit 1
else
echo "file found"
exit 0
fi
It takes care of my problem. Thanks to all.

Make Bash script exit and print error message if users invoke the script incorrectly

Script needed was
#!/bin/bash
# Check if there are two arguments
if [ $# -eq 2 ]; then
# Check if the input file actually exists.
if ! [[ -f "$1" ]]; then
echo "The input file $1 does not exist."
exit 1
fi
else
echo "Usage: $0 [inputfile] [outputfile]"
exit 1
fi
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2"
Edit, the script has changed to
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "$1" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
invoking the script with no parameters gives no erros and sits blank
Usage:
./Scriptname inputfile > outputfile
I have bit of code
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
This code pulls lines that have a single word on them and pumps the output to a new file, so for example
This is a multi word line
this
the above line is not
now
once again wrong
The output would be
This
now
The code works, users invoke the code using ./scriptname file > newfile
However, I am trying to expand the code to give users an error message if they invoke the script incorrectly.
For the error messange, I'm thinking of echoing something back like scriptname file_to_process > output_file.
I did try
if [incorrectly invoted unsure what to type]
echo $usage
exit 1
Usage="usage [inputfile] [>] [outputfile]
However I have had little luck. The code runs but does nothing if I invoke with just the script name. Also, if I invoke the script with just the scriptname and the input file, it will output the results instead of exiting with the error message.
Other ones I have tried are
if [ ! -n $1 ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
Given replies I have received so far, my code now is
#!/bin/bash
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" $*
if [ ! -f "$1" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
When invoking the script without an input file the script does nothing and has to be aborted with ctrl+c, still trying to get the echo of the invoke message.
When you are invoking the script like ./scriptname file > newfile, the shell interprets file as the only argument to ./scriptname. This is because > is the standard output redirection operator.
I would like to propose 2 possible alternatives:
Alternative 1:
Maybe you're can try passing it as 1 argument like this?
./scriptname 'file > newfile'
In that case one way to check the format would be
#!/bin/bash
# Check if the format is correct
if [[ $1 =~ (.+)' > '(.+) ]]; then
# Check if the input file actually exists.
if ! [[ -f "${BASH_REMATCH[1]}" ]]; then
echo "The input file ${BASH_REMATCH[1]} does not exist!"
exit 1
fi
else
echo "Usage: $0 \"[inputfile] [>] [outputfile]\""
exit 1
fi
# Redirect standard output to the output file
exec > "${BASH_REMATCH[2]}"
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "${BASH_REMATCH[1]}"
Note: If you are checking whether the arguments are valid or not, it's generally better to run commands only after the checking is done.
Alternative 2:
Passing 2 arguments like
./scriptname file newfile
The script looks like this
#!/bin/bash
# Check if there are two arguments
if [ $# -eq 2 ]; then
# Check if the input file actually exists.
if ! [[ -f "$1" ]]; then
echo "The input file $1 does not exist."
exit 1
fi
else
echo "Usage: $0 [inputfile] [outputfile]"
exit 1
fi
# Run the command on the input file
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" "$1" > "$2"
I'd use parameter expansion for this:
inputfile=${1:?Usage: $(basename $0) inputfile > outputfile}
If the script is called without arguments (i.e. $1 is unset) the ${var:?error message} expansion causes the shell to display an error with the given message and exit. Otherwise the first argument is assigned to $inputfile.
Try to add double quotes around $1 and use -f to check for exists and is normal file:
if [ ! -f "$1" ]; then
echo 'Usage: '
echo
echo './Scriptname inputfile > outputfile'
exit 0
fi
Also you can check for the param count with $# and cat an usage message:
if [ ! $# -eq 1 ]; then
cat << EOF
Usage:
$0 'input_file' > output_file
EOF
exit 1
fi

Checking for the correct number of arguments

How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to make sure the command line argument actually exists and is a directory.
#!/bin/sh
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
Translation: If number of arguments is not (numerically) equal to 1 or the first argument is not a directory, output usage to stderr and exit with a failure status code.
More friendly error reporting:
#!/bin/sh
if [ "$#" -ne 1 ]; then
echo "Usage: $0 DIRECTORY" >&2
exit 1
fi
if ! [ -e "$1" ]; then
echo "$1 not found" >&2
exit 1
fi
if ! [ -d "$1" ]; then
echo "$1 not a directory" >&2
exit 1
fi
cat script.sh
var1=$1
var2=$2
if [ "$#" -eq 2 ]
then
if [ -d $var1 ]
then
echo directory ${var1} exist
else
echo Directory ${var1} Does not exists
fi
if [ -d $var2 ]
then
echo directory ${var2} exist
else
echo Directory ${var2} Does not exists
fi
else
echo "Arguments are not equals to 2"
exit 1
fi
execute it like below -
./script.sh directory1 directory2
Output will be like -
directory1 exit
directory2 Does not exists
You can check the total number of arguments which are passed in command line with "$#"
Say for Example my shell script name is hello.sh
sh hello.sh hello-world
# I am passing hello-world as argument in command line which will b considered as 1 argument
if [ $# -eq 1 ]
then
echo $1
else
echo "invalid argument please pass only one argument "
fi
Output will be hello-world

Resources