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:
The file to be read is as follows:
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
The process_line function seems to read from stdin, i.e. the same stream as the read command reads from.
Redirect stdin
Or fix the process_line function.
This User Gave Thanks to MadeInGermany For This Post:
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:
</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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)