Ksh Error Code
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and bash get return code of last command policies of this site About Us Learn more about Stack Overflow the company ksh exit Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users ksh return Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a ksh error handling minute: Sign up Get exit code for command in bash/ksh up vote 34 down vote favorite 5 I want to write code like this: command="some command" safeRunCommand $command safeRunCommand() { cmnd=$1 $($cmnd) if [ $? != 0 ]; then printf "Error when executing command: '$command'" exit $ERROR_CODE fi } But this code does not working the way I want. Where I
Ksh Pipestatus
made mistake? bash unix ksh exit share|improve this question edited Dec 30 '13 at 19:34 javaPlease42 1,1981234 asked Nov 21 '11 at 12:32 Kolesar 3291925 add a comment| 4 Answers 4 active oldest votes up vote 44 down vote accepted Below is the fixed code: #!/bin/ksh safeRunCommand() { typeset cmnd="$*" typeset ret_code echo cmnd=$cmnd eval $cmnd ret_code=$? if [ $ret_code != 0 ]; then printf "Error : [%d] when executing command: '$cmnd'" $ret_code exit $ret_code fi } command="ls -l | grep p" safeRunCommand "$command" Now if you look into this code few things that I changed are: use of typeset is not necessary but a good practice. It make cmnd and ret_code local to safeRunCommand use of ret_code is not necessary but a good practice to store return code in some variable (and store it ASAP) so that you can use it later like I did in printf "Error : [%d] when executing command: '$command'" $ret_code pass the command with quotes surrounding the command like safeRunCommand "$command". If you dont then cmnd will get only the value ls and not ls -l.
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta
Bash Assign Exit Code To Variable
Discuss the workings and policies of this site About Us Learn more unix exit codes about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & bash if exit code Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating http://stackoverflow.com/questions/8211844/get-exit-code-for-command-in-bash-ksh systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How do I capture the return status and use tee at the same time in korn shell? [duplicate] up vote 4 down vote favorite 3 This question http://unix.stackexchange.com/questions/76162/how-do-i-capture-the-return-status-and-use-tee-at-the-same-time-in-korn-shell already has an answer here: Get exit status of process that's piped to another 15 answers Consider Source code: 1. Parent.sh #!/usr/bin/ksh # No tee ksh Child.sh; exit_status=$?; echo "Exit status: ${exit_status}" # Using tee ksh Child.sh | tee -a log.txt; exit_status=$?; echo "Exit status: ${exit_status}" 2. Child.sh #!/usr/bin/ksh ... exit 1; Output: Exit status: 1 Exit status: 0 Variable $exit_status is capturing the exit status of Child.sh and so is 1. In the 2nd case, $exit_status is capturing the exit status of tee, which is 0. So how do I capture the exit status and also use tee? shell-script ksh share|improve this question edited May 17 '13 at 9:06 asked May 17 '13 at 8:10 Kent Pawar 53231025 marked as duplicate by lesmana, Stéphane Chazelas, jasonwryan, slm♦, vonbrand May 17 '13 at 12:39 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Hi @lesmana - Note that this question is asking for a ksh solution. Using $P
Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here. Search Forums Show Threads Show Posts http://www.unix.com/shell-programming-and-scripting/196483-exit-status-ksh-script.html Tag Search Advanced Search Unanswered Threads Find All Thanked Posts Go to Page... learn linux and unix commands - unix shell scripting Exit status of the ksh Script Shell Programming and Scripting Thread Tools Search this Thread Display Modes #1 08-08-2012 saachinsiva Registered User Join Date: Aug 2012 Last Activity: 20 September 2012, 11:02 exit code AM EDT Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts Exit status of the ksh Script Hi Im trying to write a script that will archive some file using java program.Below is the part of the script that I use and my problem is that the script always return with status 0.Below is part of my script(end part) purge.ksh Code: echo "No of ksh error code files before tar : $noOfFilesDir" tar -cf $line/$logDate.tar `find $line -type f ! -name "*.tar"` noOfFilesTar=`tar -tf $line/$logDate.tar | wc -l` echo "No of files in tar : $noOfFilesTar" if [ $noOfFilesDir -ne $noOfFilesTar ] then echo "Some of the files could not be archived under $line.Please investigate" retVal=1 else find $line -type f ! -name "*.tar" -exec rm -f {} \; fi done <"$file" rm -f $TAR_LIST_FILE_NAME fi echo "\nUnix script exited with code : $retVal" exit $retVal ) 2>&1 | tee $APP_DIR/logs/purgeFiles_$logDate.log I call this script from another script and then check the return status and it always return 0 even if the retVal is 1. Calling script: ksh purge.ksh echo $? The status code is always 0. Please help as I need to redirect the ouput of the whole script to screen and also to the file and exit the script based on retVal. Moderator's Comments: Please view this code tag video for how to use code tags when posting code and data. Last edited by Corona688; 08-08-2012 at 11:10 PM.. Remove advertisements Sponsored Links saachinsiva View Public Profile Find all posts by saachinsiva #2 08-