Returning exit code from a while read $inputline


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Returning exit code from a while read $inputline
# 1  
Old 05-30-2012
Returning exit code from a while read $inputline

Hi searched hi and wide for this with no luck.

Maintaining a bash script that
Code:
 
#!/usr/bin/bash
#does some stuff like setting env etc.
f_do_dbwork
 
...
..
 
#Now I want to exit with the value of $err however $err is re-initialised to 0 on exiting the function
Always 0
....
 
f_do_dbwork() {
echo "$err" #- Always 0
#does a datbase call to set file that feeds the inputline command
cat $TEMP_FILE | while read inputline
do
#set variables
#call another shell script which returns an exit code of 0 or -6
 
$err=$?
if [ $err -ne 0 ] ; then 
   exit $err
fi
#more stuff
done
}

Don't have time to rewrite the cat $TEMP_FILE | while read inputline bit.

How do I get a status from the function back to the calling part of main shell script?
# 2  
Old 05-30-2012
It has to do with Bash running the while loop in a sub-shell. It's a known "problem".

You can search for "Bash while loop variable", for more info.

https://www.unix.com/shell-programmin...ll-script.html
This User Gave Thanks to Scott For This Post:
# 3  
Old 05-30-2012
Quote:
Originally Posted by Scott
It has to do with Bash running the while loop in a sub-shell. It's a known "problem".

You can search for "Bash while loop variable", for more info.

https://www.unix.com/shell-programmin...ll-script.html
Thanks for the quick response I'll go do some research.
# 4  
Old 06-13-2012
Quick fix:
Exit the function with return $err (instead of exit $err) and test $? in the main script immediately after calling f_do_dbwork
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Pipe -Exit when there are no bytes to read

Hi All, I'm creating a program which reads millions of bytes from the PIPE and do some processing. As the data is more, the idea is to read the pipe parallely. Sun Solaris 8 See the code below: #!/bin/sh MAXTHREAD=30 awk '{print $1}' metadata.csv > nvpipe & while do ... (3 Replies)
Discussion started by: mr_manii
3 Replies

2. Shell Programming and Scripting

Exit to while read

Hi expert, How do i exit to while read, below is the script. I need to exit after execute echo or command. or any scripts that can search two patterns and if they found any patterns execute the command and exit. Thanks a lot.. tail -fn0 /tmp/test.log | \ while read line ; do ... (12 Replies)
Discussion started by: lxdorney
12 Replies

3. Shell Programming and Scripting

Read exit value from Shell script

Hi all, I have a situation to read exit value of a command (not exit code) and process further. bash-4.2$ returnvalue=`ssh DOMAIN\\\\user1@10.7.7.68 'cmd /c "del C:\Users\user1\db_test.bak"'` Could Not Find C:\Users\user1\db_test.bak bash-4.2$ echo $? 0 bash-4.2$ echo $returnvalue... (3 Replies)
Discussion started by: baluchen
3 Replies

4. Shell Programming and Scripting

Returning an exit code from a bash function

How to return a exit code from a function and use it in conditional? I tried the following but it does not seem to work. tests.sh: if test ./load.sh ; then echo "0" else echo "1" fi load.sh: return 1; from command line: $ ./tests.sh 0 I was expecting it to output "1"... (17 Replies)
Discussion started by: wolfv
17 Replies

5. UNIX for Advanced & Expert Users

Timing READ command - any key to exit

Hello everyone, I would like some help on an issue I have related to the read command with ksh93 (Unix AIX). I want to implement a 'press any key to exit' scenario. I have the following code: STTY=$(stty -g) if ;then stty -echo -icanon time 0 min 0 fi k="" while ];do read k #... (5 Replies)
Discussion started by: gio001
5 Replies

6. Shell Programming and Scripting

shell script returning error code 2 from AIX to Mainframe

Hi, I have a shell script which is residing on AIX which is triggered by Mainframe through Connect Direct. The shell script creates several files and sends those files to mainframe using Connect Direct. The shell script is working fine, still it is returning exit code 2 to mainframe. What... (0 Replies)
Discussion started by: Yogesh Aggarwal
0 Replies

7. Shell Programming and Scripting

urgent -read exit status

i have written a shell script that invokes main class of a java prg. the java prg does a System.exit(0) or (1) based on condition. how can i read or check the status in unix.... (4 Replies)
Discussion started by: iamcool
4 Replies

8. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question