Using pipe in bash - is this the expected behaviour?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using pipe in bash - is this the expected behaviour?
# 1  
Old 05-23-2012
Using pipe in bash - is this the expected behaviour?

Hi all,

Am trying to convert a script from ksh to bash Smilie. One of the sub is something like below:

Code:
#!/bin/bash

declare -a array01

step_01_test()
{
   local count=0
   ps -ef | grep watch | grep -v grep | awk '{ print $8 }' | while read line
   do
      let count=${count}+1
      array01[${count}]="${line}"
      echo "---> ${array01[*]}"
   done
}

step_02_test()
{
   local count=0
   ps -ef | grep watch | grep -v grep | awk '{ print $8 }' > x.txt
   while read line
   do
      let count=${count}+1
      array01[${count}]="${line}"
      echo "---> ${array01[*]}"
   done < x.txt
   rm x.txt
}

step_01_test
echo "===> ${array01[*]}"
echo
echo "......"
echo
step_02_test
echo "===> ${array01[*]}"

exit 0

The output of running the above script is as below:

Code:
./array_01.bash
---> [watchdog/0]
---> [watchdog/0] [watchdog/1]
---> [watchdog/0] [watchdog/1] [watchdog/2]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
===>

......

---> [watchdog/0]
---> [watchdog/0] [watchdog/1]
---> [watchdog/0] [watchdog/1] [watchdog/2]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
===> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]

As you can see, am not getting the contents of the arrays if I use a pipe with the while loop.

Is this the expected behavior for bash Smilie ... Am kinda hoping I can avoid having to create a temporary file just like how am doing it on step_02_test.

What am wanting to accomplish is to be able to put into the array the output of the ps command having to redirect to a file and read from it. Maybe I don't need to use a while loop?

BTW also tried the following:

Code:
step_03_test()
{
   local count=0
   STDOUT=$( ps -ef | grep watch | grep -v grep | awk '{ print $8 }' )
   while read line
   do
      let count=${count}+1
      array01[${count}]="${line}"
      echo "---> ${array01[*]}"
   done <<< "$STDOUT"
}

Unfortunately, the output from step_03_test gives as below:

Code:
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]
---> [watchdog/0] [watchdog/1] [watchdog/2] [watchdog/3] [watchdog/4] [watchdog/5] [watchdog/6] [watchdog/7]

Feedback/Advise much appreciated. Thanks.

Last edited by newbie_01; 05-23-2012 at 02:22 AM.. Reason: Added more information
# 2  
Old 05-23-2012
Code:
ps -ef | grep watch | grep -v grep | awk '{ print $8 }' | while read line

This is a KSH-only construct. No ordinary Bourne shell lets you put 'while read' on the end of a pipe. You set variables, but they don't stick around, because they're being set in a subshell, not in your shell.

Cramming it all into backticks will do strange things since it will split wherever it pleases, not on lines.

I'd redirect it into a temp file (/tmp/$$ perhaps) then read from that in the loop.

Code:
while read LINE
do
...
done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies

2. UNIX for Beginners Questions & Answers

Bash script does not work as expected

Repeat this text in a file named notes.txt and run the script Before bash is a good language a blank line appears Also, the following notes are displayed incorrectly What is bad? ================================== Title : Note 1 ================================== Category: Computer Date... (3 Replies)
Discussion started by: cesar60
3 Replies

3. Shell Programming and Scripting

Bash script unable to disable expected output

I'm trying to understand why a script behaves different when run through a pipe. My OS: Linux myip 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux This script (myscript.sh): #!/bin/bash echo whoami: whoami echo who: who echo who... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

5. Red Hat

Device-mapper behaviour booting on init=bin/bash

Good morning Recently we needed to change the password from a redhat 6.5 system that no one knew the root password. Starting the system with the init=/bin/bash method took us to the following scenario: system_vg active with only root_lv and tmpfs mounted. our entries at fstab are like... (1 Reply)
Discussion started by: Ikaro0
1 Replies

6. Shell Programming and Scripting

Bash syntax behaviour : [[ vs [

Hello. In the following : RESTORE_FF contain a file name : a_file.tar.gz I am testing in a directory if "a_file.tar.gz" exists and or if any file like "a_file.tar.gz" exists. So "a_file.tar.gz" will give me file exists So "a_file.tar.gz." will give me file exists So... (5 Replies)
Discussion started by: jcdole
5 Replies

7. Shell Programming and Scripting

Bash won't exit as expected

Hi there, following code snippet should output nothing, IMHO. But the result is "THE END". #!/bin/bash if true ; thenexit fi | grep "somesearchstring" echo "THE END"using bash 4.1.9(1) Bug or feature? Hagen (5 Replies)
Discussion started by: montour
5 Replies

8. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

9. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

10. Shell Programming and Scripting

Bash script on startup does not respond as expected

Hi, I have a bash script which I have referenced in the rc.local of my fedora linux OS. However it doesnt respond the same as when run in terminal from fedora. The bash script has a series of interactive questions that require user input as shown: #!/bin/bash echo "Do you want to use... (1 Reply)
Discussion started by: Crigamorfin
1 Replies
Login or Register to Ask a Question