ksh While Loop - passing variables to functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh While Loop - passing variables to functions
# 1  
Old 11-24-2016
ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read.

I thought it was something to do with the IFS setting but it appears that a function call (when run successfully) forces the loop to end on that iteration. It doesn't exit with an error from the function. It completes it and even prints out lines afterwards but exits the loop.

Here's the code:

Code:
while IFS="," read current_line
do
        printf "\n[INFO] Starting an iteration of testcase file ... \n" >> $LOGFILE
  
        # Check type of cmd line by grabbing the first 8 characters  
        cmd_type=$(printf ${current_line} | awk '{print substr ($0, 0, 8)}')
 
        if [[ ${cmd_type} == "process_" ]]; then
                printf "\n\n\n[INFO] Running line: ${current_line}  \n\n\n" >> $LOGFILE
                IFS=''
                process_line ${current_line}  # pass the current line from the file to the process_line function
                RC=$?
                printf "\nprocess_line status: $RC" >> $LOGFILE
               
        fi
        printf "\n[INFO] Completed an iteration of testcase file ... \n" >> $LOGFILE
 
done < ${FILENAME}

The file to be read is as follows:
Code:
#
Title: Test 1
process_1_test,task_2::GEN,NAME=12345,SIZE=M,DSIZE=M,VER=12
Title:   Test 2
process_2_test,task_1::INNAME,INPUTS=3,CHARSET=AL16UTF16
#
#
Test 3


I tried the following:

If I set IFS="," just before the while statement it loops through ok but doesn't pass the correct string value to the function I call inside the loop. It cuts it on the delimiter.

If I set IFS="," in the while statement it should apply only to the read cmd. This correctly passes the variable string to my function but then does not continue to loop through the file after that same iteration.

If I try setting IFS="," before the while statement again but this time I set the IFS= just before the function call … and IFS="," just after it, it passes the string correctly to the function but again exits the loop that same iteration.


So basically if I set IFS="," outside of the while loop it loops through ok because the function call fails due to the split string I pass to it. If I set IFS inside the loop then it exits on the iteration where I call the function. Note: If the function does nothing other than a printf then it works fully ... but I need it to call another 2 functions.

I print out info after the function call in that same iteration so I know it's not exiting the loop during the function call.

Any ideas? Many Thanks.

Last edited by rbatte1; 11-25-2016 at 07:12 AM.. Reason: Added some ICODE tags
# 2  
Old 11-25-2016
The process_line function seems to read from stdin, i.e. the same stream as the read command reads from.
Redirect stdin
Code:
        </dev/null process_line ${current_line}

Or fix the process_line function.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 11-25-2016
Function calls in ksh (and in other POSIX conforming shells) do not cause a while loop that invokes that function to terminate unless the function does not return when it terminates. If you would show us the code for your process_line function and how the variables that are used by the code you showed us are initialized we would have a much better chance of being able to help you.

There are several places in your code where you expand shell variables without quoting the expansion. Depending on the value of the variables involved, that may send output to a different file than you intended and cause syntax errors.

The awk substr() function's 2nd argument is the starting position within the string named by the 1st argument. You are using 0 for that argument, but the 1st character of a string in awk is number 1; not number 0. (And there are much more efficient ways to grab the 1st 8 characters of a string contained in a shell variable than invoking a subshell, creating a pipeline of two more processes and invoking awk.)

With what you are doing with read in your while loop, there doesn't seem to be any obvious reason to change IFS from its default value for the read. What do you hope to achieve by setting IFS to a comma?

With what you have shown us so far, my 1st guess would be that process_line is exiting your entire shell script instead of returning (or falling through the end of your function) which would return control to your while loop. My 2nd guess would be what RudiC has already suggested. But without a lot more details than you have provided so far, we are all just guessing.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-28-2016
Many thanks guys.

</dev/null process_line ${current_line} worked. Thanks Rudi. IFS did not need to be set. But of course I had to fix the return codes from the function.
Great advice Don. I replaced the awk substr, got rid of the IFS settings, added the quotes. It all works now.

Last edited by rbatte1; 11-29-2016 at 05:31 AM.. Reason: Added ICODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing three variables to loop and error

I have many printer queues to be created in AIX 6.1. I can create printers with this line: /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q 'emg1' -D ps -q 'emg1ps' -h 'emg1' -x '9100' But, I like to feed the printer queue data, called "printfeed", so that I can... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

2. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

3. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

4. Shell Programming and Scripting

Loop to define variables in KSH

Hi, I am trying to use a database to store configurations in an environment definition scripts to make the configurations easily modifiable. (long story short - it is an easier process to make changes in the db than trying to deploy a file). The values will be stored in the database in the... (1 Reply)
Discussion started by: gbala
1 Replies

5. Shell Programming and Scripting

ksh - for loop with variables

Hi, I 'm trying to send an e-mail for every different line in the .txt for i in {1..$variable} do sed -n "/$i$/p" text.txt done I have two problems about this. First one is that for loop doesn't work and the second one is that i cant get the output of sed (4 Replies)
Discussion started by: ozum
4 Replies

6. Shell Programming and Scripting

Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script. function isPresent { typeset member member=$1 dbList=$2 echo '$1:' $1 echo '$2' $dbList The array will be at the second position....something like this isPresent 12 <array> if then echo... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Shell Programming and Scripting

passing variables to sed in ksh

Hi, i need help passing variables to sed using ksh. My goal is to get particular data from log files. first i put a mark to the log files. echo "TEST_"`date + %m_%d_%Y_%T"` >markFile this will produce a 'markFile' which contain text like this TEST_06_01_2009_21:55:09 then i put the mark... (2 Replies)
Discussion started by: d.anggrianto
2 Replies

8. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

9. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question