IMHO that's because you are passing output of cat file1 command to while loop which is considered as a new shell(or in other language creates a sub shell to execute while loop). So once while loop is done with execution its new value of i gets lost and when you try to print its value; it prints OLD value for i.
Now lets do this with another way(Without using cat file | while.....
When you run script file.ksh we will see following.
I believe that is proved why in your attempt i's value is getting lost.
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
Can someone help me understand why it is back to 0 ( why not 5?)
As Ravinder has already said it is because of the pipeline. Basically in classical Bourne Shell (and its descendants, bash - Bourne Again Shell - among them) code like this:
is executed by running the process1 in the main shell and process2 in its own subshell. Therefore the while-loop you use runs in its own subshell, with its own variables and inside this shell your variables work - which is why your counter is increased - but when the subshell is left (at the end of the loop) all these variables are deleted. This is why your counter variable is zero at the end.
Notice that the other descendant of the Bourne shell - the Korn Shell, ksh - does things differently. In ksh your code would work as you obviously expect because it executes the last element of a pipeline in the main shell. You can force bash (since, IIRC version 4) to do the same but you must set this option explicitly:
will set the behavior of bash to exactly the same as ksh always had. So your options are: switch to ksh (most of the code is the same but there are subtle differences between bash and ksh) or set the lastpipe option.
By the way: notice that pipelines and redirections word differently in bash, regardless of the lastpipe-setting! This simple line-counter:
will not work (for the same reasons as your code) but:
I hope this helps.
/PS: only now saw that peasant has also addressed the ksh<->bash difference. He is absolutely right.
Last edited by bakunin; 07-30-2019 at 10:45 AM..
Reason: corrected typo
These 4 Users Gave Thanks to bakunin For This Post:
As is often the case with a little bit of jiggery pokery there is a pseudo-back-door.
The storage device can be your friend although it will slow things down with the disk thrashing and huge numbers.
NOTE: This uses 'dash' which means it is fully POSIX compliant.
Results OSX 10.14.3, default terminal.
This User Gave Thanks to wisecracker For This Post:
I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest:
I'm... (6 Replies)
Dear mentors, I just need little explanation regarding for loop to give input to awk script
for file in `ls *.txt |sort -t"_" -k2n,2`; do
awk script $file
which sorts file in order, and will input one after another file in order to awk script
suppose if I have to input 2 or... (4 Replies)
Hello! Before you "bash" me with
- Not another post of this kind
Please read on and you will understand my problem...
I am using the below to extract a sum of the diskIO on a Solaris server.
PATH=/usr/bin:/usr/sbin:/sbin; export PATH
TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i.
I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
I am having issues with a script I'm working on developing on a Solaris machine.
The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today.
Here is my while loop:
last $user | grep -v 'sshd'... (7 Replies)
Hi Friends ,
Sorry if this is a repeated question ,
The input file contains 5 lines , so the the values of the variables i and count should b
but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad:
can any1 help me please. (11 Replies)
I am writing a shell script using the korn shell. It seems that I am only
able to use local variables within a while loop that is reading a file.
(I can't access a variable outside a previously used while loop.) It's been
a while since I wrote shell scripts. Here is a sample
cat file.txt... (4 Replies)
I have a file (details.txt) with 3 rows of variables ie...
name postcode age
john D fr25dd 25
mark W ab122aa 22
phil C cd343bb 33
What I want to do is read down the list with a loop and add each field into a one line piece of text...
So I have a file (test1) which reads;... (3 Replies)
I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh).
The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)