I am trying to get this program to exit if it cannot find the file that has to be deleted. Or if it won't delete for any reason.
When I use this it runs my program it gives me an error "cannot find file" and completes my program successfully.
del `unixpath2win filename`
rc=$?
if [ $rc -ne 0 ] ; then
echo_c $1 "error with deletion of filename"
checkrc.sh $rc
exit 1
fi
Related
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
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
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.
I am working through this tutorial on daemonizing php scripts. When I run the following Unix command:
. /etc/init.d/functions
#startup values
log=/var/log/Daemon.log
#verify that the executable exists
test -x /home/godlikemouse/Daemon.php || exit 0RETVAL=0
prog="Daemon"
proc=/var/lock/subsys/Daemon
bin=/home/godlikemouse/Daemon.php
start() {
# Check if Daemon is already running
if [ ! -f $proc ]; then
echo -n $"Starting $prog: "
daemon $bin --log=$log
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $proc
echo
fi
return $RETVAL
}
I get the following output:
./Daemon: line 12: /etc/init.d/functions: No such file or directory
Starting Daemon: daemon: unrecognized option `--log=/var/log/Daemon.log'
I looked at my file system and there was no /etc/init.d file. Can anyone tell me what this is and where to obtain it? Also is the absence of that file what's causing the other error?
Separate your args within their own " " double-quotes:
args="--node $prog"
daemon "nohup ${exe}" "$args &" </dev/null 2>/dev/null
daemon "exe" "args"
How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc