You are not logged in.

#1 2012-10-27 23:19:22

Waljakov
Member
From: Germany
Registered: 2010-01-13
Posts: 14

[Bash]Access variable out of a loop

Hi @ all,
I'm wondering about the following phenomenon:

a=1
while true
do
    echo in-while $a
    sleep 1
done &
(a=2 && echo out-while $a)

Result:

in-while 1
out-while 2
in-while 1
in-while 1
in-while 1
...

After out-while appears it should print in-while 2 instead of lying the old value...

Is there any way to access the variable out of the loop?
Would be very happy if someone could help me smile


Sorry for bad English

Offline

#2 2012-10-27 23:22:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,094
Website

Re: [Bash]Access variable out of a loop

You can't do this. You're creating a child process by backgrounding the while loop. It will not "see" changes made to variables in its parent. Additionally, even if it did, you're wrapping the a=2 declaration in parenthesis, which creates a subshell of its own.

http://mywiki.wooledge.org/BashFAQ/024

Offline

#3 2012-10-27 23:29:04

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 12,952

Re: [Bash]Access variable out of a loop

do
    echo in-while $a
    sleep 1
done &

Lives in the background and was instantiated with the environmental variables at the the time it was created.  Why would you expect something running in a different process to change the copies that were made when the background process was started?


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Like you, I have no idea what you are doing, but I am pretty sure it is wrong...Jasonwryan
----
How to Ask Questions the Smart Way

Online

#4 2012-10-27 23:51:29

Waljakov
Member
From: Germany
Registered: 2010-01-13
Posts: 14

Re: [Bash]Access variable out of a loop

Is not there any way to communicate with the child proeccess?


Sorry for bad English

Offline

#5 2012-10-28 01:04:41

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,094
Website

Re: [Bash]Access variable out of a loop

You can use a named pipe. Perhaps it would be better if you explained what you were actually trying to do rather than providing a contrived example.

Last edited by falconindy (2012-10-28 01:04:50)

Offline

#6 2012-10-28 13:28:32

Waljakov
Member
From: Germany
Registered: 2010-01-13
Posts: 14

Re: [Bash]Access variable out of a loop

Yeah, that's the only idea I had, too, but I hoped to learn a cleaner way.

All right, I will shortly explain what I'm trying to:
I want to write a music player in bash using mplayer. In background runs a loop where mplayer plays a playlist array.

while [ $currsong -lt $((${#playlist[@]}-1)) ]
do
    currsong_buffer=$currsong
    mplayer -slave -input file=/tmp/mplayerpipe $playlist[$currsong]
    if [ $currsong == $currsong_buffer ]
    then
        currsong=$(($currsong+1))
    fi
done &

While the current song is played the user should be able to select a new one. For this I need to manipulate  $currsong (and $playlist) out of the loop. I have a play() function which detects if mplayer is running and sends a stop-signal to mplayer via named pipe so the current mplayer process shuts down and the loop starts a new turn with the new title. If mplayer is not running the loop above is called again.
The buffer variable is need to check if $currsong was manipluated while playing the current song. If not, mplayer should continue to play the next song in playlist.
But in fact, manipulating $currsong does not work.

P.S. Sorry for my bad english, hoping you are able to unterstand what I mean.

Last edited by Waljakov (2012-10-28 13:49:12)


Sorry for bad English

Offline

Board footer

Powered by FluxBB