Question regarding shells and subshells when a script is run


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question regarding shells and subshells when a script is run
# 1  
Old 07-06-2010
Question regarding shells and subshells when a script is run

I have the following script running with nohup on one of my servers:

Code:
#!/bin/bash
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#set log number
#i=1
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#Check if log exits, if so incrememnt log number up so we don't clobber
#while [[ -f /tmp/ethtool.$i.dump ]];do let i=i+1;done
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#Location of streamer log
LOGFILE=/opt/VideoServer/logs/streamer_log.txt
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#Before tailing logs, dump current eth information
ethtool -S eth3 > /tmp/ethdump.$(date "+%m.%d.%y.%H.%M").start
cat /proc/net/cxgb3/ofld_dev0/stats >> /tmp/ethdump.$(date "+%m.%d.%y.%H.%M").start
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#Start tailing the log
tail -F $LOGFILE|while read line;do
        line=($line)
        if [[ "${line[2]}" == "E" ]]; then
            echo "${line[*]}" | grep -q "FX_HR_Timer_Sink::restart_timer_"
                if [[ $? -eq 0 ]];then
                        LOGTIME=$(date "+%m.%d.%y.%H.%M")
                        echo "${line[*]}" > /tmp/ethdump.$LOGTIME
                        echo >>  /tmp/ethdump.$LOGTIME
                        echo >>  /tmp/ethdump.$LOGTIME
                        ethtool -S eth3 >>  /tmp/ethdump.$LOGTIME
                        echo >>  /tmp/ethdump.$LOGTIME
                        echo >>  /tmp/ethdump.$LOGTIME
                        cat /proc/net/cxgb3/ofld_dev0/stats >>  /tmp/ethdump.$LOGTIME
                        #let i=i+1
                fi
        fi
    done

All it's doing is watching a log file, and looking for errors (as indicated by E in the third column) and then checking to see if that error is a specific problem we're having, and if so, dumping some information about the interface. The script runs perfectly, and creates the dump files exactly as expected. The one thing I don't understand is why it seems to create two processes when it is run:

Code:
[root@p1b021 ~]# ps -ef|grep logWatch.sh
root      6675     1  0 Jul05 ?        00:00:00 /bin/bash ./logWatch.sh
root      6681  6675  0 Jul05 ?        00:00:01 /bin/bash ./logWatch.sh
root      8221  8195  0 09:44 pts/0    00:00:00 grep logWatch.sh

I assume it has something to do with creating subshells when the script is run, but as I (unfortunately) learned most of my scripting and whatnot from trial and error, and not from real book learning, simple things like this escape me.

I've tried to do some reading, and while I understand shells and subshells to a point in theory, I'm having a hard time understanding what's going on in practice. Could anyone give me the idiots explanation of why I'm spawning two processes to do one job? (I know the script isn't running twice at the same time...only one dump file is being created).
# 2  
Old 07-06-2010
Code:
echo "${line[*]}" | grep -q "FX_HR_Timer_Sink::restart_timer_"

The "other side" of a pipe is a child process
# 3  
Old 07-06-2010
You are seeing that second shell because bash is spawning it to run the while loop:
Code:
tail -F $LOGFILE|while read line;do

This is also the reason why the effects of modifying variables within the while loop are not visible to the rest of the script.

Some shell implementations run the while loop within the current shell, but not bash.

Regards,
Alister
# 4  
Old 07-06-2010
Quote:
Originally Posted by jim mcnamara
Code:
echo "${line[*]}" | grep -q "FX_HR_Timer_Sink::restart_timer_"

The "other side" of a pipe is a child process
So there's no way of knowing what process is doing what? If one pid were killed, would the other keep running but not do anything, or would it just spawn the process again once the loop went through again.

I had originally wanted to do something more like

Code:
if [[ "${line[2]}" == "E" ]] && [[ "${line[7]}" == "FX_HR_Timer_Sink::restart_timer_" ]];then

But I couldn't get it to work. I think it has something to do with the colour coding used in the log files. Had I done it like this, would it have not spawned the second shell?

Is there a better way I could have accomplished this goal? I've only recently started trying to parse logs in real time like this, and aside from learning perl (which I hope to do eventually) I can't think of who better to have accomplished this.

Quote:
Originally Posted by alister
You are seeing that second shell because bash is spawning it to run the while loop:
Code:
tail -F $LOGFILE|while read line;do

This is also the reason why the effects of modifying variables within the while loop are not visible to the rest of the script.
I'm sorry, could you expand on this a bit? I'm not sure what you mean by modifying variables inside the loop...where else could I have done it?
# 5  
Old 07-06-2010
Alister - counterexample:

I believe this contradicts what you said about bash. What you said is true for some other shells, esp. the Bourne shell.

Code:
a=1
while read rec
do
  a=$(( $a + 1 ))
done < t.lis
echo $a

output:
Code:
3

For variables are processed inside a bash while loop, changes are visible outside the loop.
At least in this example.
# 6  
Old 07-06-2010
isn't it due to external programs usage?
# 7  
Old 07-06-2010
Quote:
Originally Posted by jim mcnamara
Alister - counterexample:

I believe this contradicts what you said about bash. What you said is true for some other shells, esp. the Bourne shell.

Code:
a=1
while read rec
do
  a=$(( $a + 1 ))
done < t.lis
echo $a

output:
Code:
3

For variables are processed inside a bash while loop, changes are visible outside the loop.
At least in this example.


I guess, there is no sub-shell involved in this or I am wrong?

Alister's statement probably meant for sub-shells.

Code:
a=1
cat t.lis | while read rec
do
  a=$(( $a + 1 ))
done 
echo $a


output: 1

Also possible, I misunderstood the scenario.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run commands in a script in different shells

Hi to all, i have the following problem... i want to run three commands in a script in different shells... the first command is running always and is needed for the second on to run properly... example # Procedure 1 xterm -e exec1 arg1 arg2 # Procedure 2 xterm -e exec2 arg1 arg2 #... (6 Replies)
Discussion started by: paladinaeon
6 Replies

2. UNIX for Dummies Questions & Answers

How to run additional shells

Hi, unix newbi here. Im currently on the bash shell, what are the commands to run additional shells? for example i want to run csh and then ksh? Thanks. BT. (2 Replies)
Discussion started by: BillThompson
2 Replies

3. UNIX Desktop Questions & Answers

How to run addtional Shells on top of the default shell

Hi What is the command to run additional available shells on top of default shell. I am using bash also how can i tell that the additional shells are actually running? Also what is the advantage of running additional shells on top of your default login shell of bash? Lastly... (1 Reply)
Discussion started by: Bill Thompson
1 Replies

4. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

5. Shell Programming and Scripting

Question on tweaking the PATH variable to allow the world to run my executable script

All, I am pretty new to Unix and still in the learning curve :) I have a simple requirement for which I did not get an answer yet (Atleast I do not know how to keyword the search for my requirement!!!). I have an executable script my.script1 in a folder /data/misc/scripts/dev, which when... (5 Replies)
Discussion started by: bharath.gct
5 Replies

6. Shell Programming and Scripting

Book and Links about Shells; and zsh question

HI, I would like to ask You about some good books or links where I can find information about shells, theoretical information. I will be grateful if You can help me And I have question about zsh loop trivial script: #!/bin/zsh for i in {1..100000} do echo $i; doneexec time is 10... (9 Replies)
Discussion started by: Physix
9 Replies

7. Shell Programming and Scripting

Using different shells in one script

Hi, Is it possible to use multiple shells in one script. There are sometimes we need to club shell specific commands in single script. for example in bash mode we use -e with echo to use Escape sequence but in ksh it is not required. How to tell a UNIX command to run in a specific shell. ... (2 Replies)
Discussion started by: sanjay1979
2 Replies

8. Shell Programming and Scripting

Killing parent shells from subshells (KSH)

Hi all, I have a shell script which calls other shell scripts, depending on the input. Within a.sh, I have a command which calls b.sh (ie. ksh b.sh) Normally, we use the exit function to terminate a shell. However, if I choose to call exit from b.sh, I will return to the parent shell who... (4 Replies)
Discussion started by: rockysfr
4 Replies

9. UNIX for Advanced & Expert Users

script to run different shells which run different processes

Hi, Would like to ask the experts if anyone knows how to run a script like this: dtterm -title shell1 run process1 on shell1 dtterm -title shell2 run process2 on shell2 cheers! p/s: sorry if i used the wrong forum, quite concussed after watching world cup for several nights; but I... (2 Replies)
Discussion started by: mochi
2 Replies

10. UNIX for Dummies Questions & Answers

basic question about shells?

I have come across the topic of changing shells, does that mean that all Unix operating systems comes with a variety of shells built in and its up to the user to select a shell of his/her choice? (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question