Return value error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return value error
# 1  
Old 03-01-2012
Return value error

Hi All,

I am new to shell sciprting and i have written a small script to get the filename if it exists. I need to capture the file name using the script and i also need the exit status. The problem i am facing is,though my file does not exists, iam getting the exit status as '0'. Please help me out.

My scripts is as follows;

Code:
#!/usr/bin/ksh
folder='/stagep03/cca/data/'
cd $folder
wild='*'
dated=`date +%m%d%y`
filename=`ls -l $1$dated$wild | tail -1 |grep ^-|awk '{print $9}'`
echo $filename
echo $?

The output iam getting is;
Code:
$ sh test.ksh PROCESS1.CAIRP.SSZCKST.A_R.
ls: 0653-341 The file PROCESS1.CAIRP.SSZCKST.A_R.030112* does not exist.
0

Thanks in advance.

Last edited by Scott; 03-01-2012 at 07:48 AM.. Reason: Use code tags, please
# 2  
Old 03-01-2012
Remove the line "echo $filename". If do not want to remove, instead of "echo $0", check for $filename is not null.

Guru.
# 3  
Old 03-01-2012
Thanks for the reply,

I removed $filename and run the script..but still iam facing the same problem. If the output is as "ls: 0653-341 The file PROCESS1.CAIRP.SSZCKST.A_R.030112* does not exist." why the exit status is 0?
# 4  
Old 03-01-2012
exit status 0 means the previous command return is true, which means this was executed correctly.
$? gives the return of the previously executed command.

you are executing
Code:
echo $filename echo $?

This mean echo command ran successfully which is the last command before "echo $?"

your error is coming while executing "ls -l" but with echo.
# 5  
Old 03-01-2012
I have removed echo $filename from my scrip. Now my "echo $?" is next to "filename=`ls -l $1$dated$wild | tail -1 |grep ^-|awk '{print $9}'`"

the exit status should not be '0' if the file does not exist. right?.

but it is '0'.
# 6  
Old 03-01-2012
Quote:
Originally Posted by srinivasayedla
I have removed echo $filename from my scrip. Now my "echo $?" is next to "filename=`ls -l $1$dated$wild | tail -1 |grep ^-|awk '{print $9}'`"

the exit status should not be '0' if the file does not exist. right?.

but it is '0'.
You are having couple of sub shells which causing it to be $?=0.

Try this and check
Code:
#!/usr/bin/ksh
folder='/stagep03/cca/data/'
cd $folder
wild='*'
dated=`date +%m%d%y`
ls -l $1$dated$wild
echo $?

Please note, you are invoking the script with old bourne shell. This simply ignores the shebang of the script (ksh)

To cope with the pipes (subshell), ksh93 having pipefail option.
Code:
$ ls nofile | more
ls: nofile: No such file or directory
$ echo $?
0
$ set -o pipefail
$ ls nofile | more
ls: nofile: No such file or directory
$ echo $?
2 
$

# 7  
Old 03-01-2012
One possible rearrangement of your script to let Shell use filename globbing.
Note that the "ls -1" below is "ls hyphen-one" not "ls hyphen-ell".

Code:
#!/usr/bin/ksh
folder='/stagep03/cca/data/'
cd "${folder}"
dated="`date +%m%d%y`"
prefix="${1}${dated}"

test -f "${prefix}"* ; REPLY=$?
echo "REPLY=${REPLY}"

if [ ${REPLY} -eq 0 ]
then
        ls -1d "${prefix}"*
fi


Last edited by methyl; 03-01-2012 at 10:58 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

Error re-direction and Return code

Hi, I have a shell script which executes some sql. When the shell script executes the sql's logging is shown on the console. I need to grep some data from this output shown on console. So I do the following hive -f load.adj.hql 2>&1 | tee c.txt echo $? A=`grep num_rows c.txt` $? will... (1 Reply)
Discussion started by: wahi80
1 Replies

3. Shell Programming and Scripting

Greping return code error

Hi folks, I am running below code which is giving me output "httpd (no pid file) not running", how can i grep this code in shell script. I have tried below code but it is not giving me error in echoing. ./webserver stop if echo " Everything is OK" else echo "Error code $0... (4 Replies)
Discussion started by: learnbash
4 Replies

4. UNIX for Dummies Questions & Answers

Does SCP return an error code for network issues

Hello everyone, In a script, I am using SCP to copy huge file to another host. scp -qrp hugefile.txt /opt/perf05/tmp However, we have noticed that this file is not being copied. I am suspecting this was because we are losing connection while copying this... (1 Reply)
Discussion started by: qwarentine
1 Replies

5. Shell Programming and Scripting

Return error if - or certain characters are present in a list of strings

I have a list of strings, for example: set strLst = "file1 file2 file3 file4" I want to log an error if some of the fields happen to begin with -, or have characters like ; : ' , ? ] { = Which means for example setting set ierr = 1 (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

SFTP return Error Code 126

Hi, We are getting the following error code while connection remote server using sftp command. sftp user@serrver Warning: child process (/opt/ssh2/bin/ssh2) exited with code 126. pls Advise. (2 Replies)
Discussion started by: koti_rama
2 Replies

7. Shell Programming and Scripting

Getting error return code

I need to try and get the error return code from the tar command when being used as follows: tar tvf tarfile 2>logfile | tee -f outputfile ErrorStat="$?" I would like to save the error return code from the tar command in a variable, howver, the example above it is saving the 'tee' error... (7 Replies)
Discussion started by: nck
7 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

how to get error return code

I have a unix AIX script that ftps some files (mput, mget). How can I check (in the script) to see if the ftp failed? After the ftp I move the files out of the directory but do not want to move files that have not been sent. The script will run as a cron job. (2 Replies)
Discussion started by: rayg50
2 Replies
Login or Register to Ask a Question