store last command exit status in variable in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting store last command exit status in variable in shell script
# 1  
Old 09-23-2011
store last command exit status in variable in shell script

Hello All

My req is to store the exit status of a command in shell variable

I want to check whether the file has header or not
The header will contain the string
DATA_ACQ_CYC_CNTL_ID

So I am running the command
Code:
head -1 $i | grep DATA_ACQ_CYC_CNTL_ID

Now I have to check if header is there then I will print Header exist if not then header doesn't exist

So the below I put

Code:
 
output=$?
echo $output
if [ $output -eq 0 ]
then 
echo header in flat file exist
else 
Flat File $i has no header
fi

but the code is not giving expected result when header doesn't exist
Its not showing
Code:
 
Flat File $i has no header

# 2  
Old 09-23-2011
Hi you missed a couple of things, try with these corrections:

Code:
#Here the command to execute

output=$?
echo $output
if [ "$output" -eq "0" ]
then 
      echo "header in flat file exist"
else 
      echo "Flat File $i has no header"
fi

# 3  
Old 09-23-2011
Modify the line output=`echo $?`
# 4  
Old 09-23-2011
Thanks for both of your reply but in case if condition fails the o/p is not
echo "Flat File $i has no header"

but Compare.sh[15]: Flat: not found
# 5  
Old 09-23-2011
works fine for me ..
Code:
i="filename"
head -1 $i | grep DATA_ACQ_CYC_CNTL_ID
output=`echo $?`
echo $output
if [ $output -eq 0 ]
then
echo "header in flat file exist"
else
echo "Flat File $i has no header"
fi

# 6  
Old 09-23-2011
Code:
else 
Flat File $i has no header

should be

Code:
 
else 
echo " File $i has no header "

---------- Post updated at 03:04 PM ---------- Previous update was at 03:03 PM ----------

you can check something like this also.

Code:
 
grep "DATA_ACQ_CYC_CNTL_ID" filename > /dev/null && echo "Header is there" || echo "Header is not there"

if your grep supports -q then no need to put > /dev/null
# 7  
Old 09-23-2011
Thanks a lot everyone .its working fine now
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

Exit Status of Command

Hi All, I am doing an export and import (datapump) of 4 schema. I know we can do an export of 4 schema in one command. But just want to know how to check the exit status if i do the export/import of 4 schema in different commands in background. Please suggest. Thanks, Mani (1 Reply)
Discussion started by: pvmanikandan
1 Replies

3. Shell Programming and Scripting

Weird Exit Status of shell script

I have a script named check which will read the content of a file and check wether those files exist in the current directory. If so it will have the exit status of 0, otherwise it will have 1. check script: #!/bin/bash if ; then #Check there is enough command line parameters. exit 1... (2 Replies)
Discussion started by: Ray Sun
2 Replies

4. Shell Programming and Scripting

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

5. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

6. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

7. Shell Programming and Scripting

problem in exit status of the command in a shell script-FTP

Hi All, I have developed below script for FTP a file from unix machine to another machine. ftpToABC () { USER='xyz' PASSWD='abc' echo "open xx.yy.zbx.aaa user $USER $PASSWD binary echo "put $1 abc.txt" >> /home/tmp/ftp.$$ echo "quit" >> /home/tmp/ftp.$$ ftp -ivn <... (3 Replies)
Discussion started by: RSC1985
3 Replies

8. UNIX for Dummies Questions & Answers

$? = Exit status variable

hi, exit status variable $?, returns some digits. 0 ---> succes. 1..126 Failure (the program itself will decide what the numbers mean) 127 Command not found 128..254 The program did not exit normally. (E.g., it crashed, or received a signal) 255 Invalid exit code well, if $?... (4 Replies)
Discussion started by: dummydba
4 Replies

9. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

10. Shell Programming and Scripting

checking exit status of a shell script

Hi, I have tried with the following code; if ;then echo "Failure." else echo "Success." fi to test the exit status of the test.ksh shell script. But whatever is the exit status of the test.ksh shell script Failure. is always printed. Please help. regards, Dipankar. (2 Replies)
Discussion started by: kdipankar
2 Replies
Login or Register to Ask a Question