[bash] why my variable is not updated?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] why my variable is not updated?
# 1  
Old 03-27-2019
[bash] why my variable is not updated?

Does anyone know why the below script is not working? Why is not the variable tot_files updated?

Code:
location=$1
cd "$location"

tot_files=0

(
    echo ""
    # recursively gets the total number of files
    tot_files=$(for t in files ; do echo `find . -type ${t:0:1} | wc -l` $t | cut -f1 -d" "; done 2> /dev/null) & 
) | wait ${!} | yad --progress --pulsate --progress-text="" --center --title="title" --text="Please wait... Reading files..." --no-buttons --no-escape --width=300 --height=200

echo "tot_files: " $tot_files

I would like to show a yad (zenity alternative) window that says to the user to wait until all the files in $1 have been counted.
That's why I used wait ${!} but I am not sure I have used in the right way.

Last edited by soichiro; 03-27-2019 at 01:02 AM..
# 2  
Old 03-27-2019
Quote:
Originally Posted by soichiro
Does anyone know why the below script is not working? Why is not the variable tot_files updated?

Code:
location=$1
cd "$location"

tot_files=0

(
    echo ""
    # recursively gets the total number of files
    tot_files=$(for t in files ; do echo `find . -type ${t:0:1} | wc -l` $t | cut -f1 -d" "; done 2> /dev/null) & 
) | wait ${!} | yad --progress --pulsate --progress-text="" --center --title="title" --text="Please wait... Reading files..." --no-buttons --no-escape --width=300 --height=200

echo "tot_files: " $tot_files

I would like to show a yad (zenity alternative) window that says to the user to wait until all the files in $1 have been counted.
That's why I used wait ${!} but I am not sure I have used in the right way.
Everything between the parentheses marked in red above is performed in a subshell. Nothing set inside that subshell will be visible in the shell that invoked that subshell (including changes to variables tot_files and !. And, since there is no wait inside the subshell, the subshell will return to the main shell before tot_files is updated in the subshell. And, the main shell has no idea that there is anything left running in the background. And, when it completes, the updated value assigned to tot_files will silently be ignored.

Furthermore, wait should never be in the middle of a pipeline; it doesn't read anything from its standard input; and even if it did, nothing from the first stage of your pipeline writes anything to its standard output.

I'm afraid I have no experience with zenity nor yad so I can only tell you what is wrong with your script; not how to fix it to make yad work.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-27-2019
Quote:
Originally Posted by soichiro
Does anyone know why the below script is not working? Why is not the variable tot_files updated?
Because you updated it in a subshell. In fact it is updated - but only in the subshell and with leaving the subshell the value is lost too.

These two are identical and they:

Code:
( command )

(
    command
)

tell the shell to open a new shell and execute everything within the braces in this new shell and then close it.

The next problem is:

Code:
tot_files=$(<...>) &

This is not working at several levels.

First: what do you want to assign to the variable tot_files? Obviously the stdout of whatever is inside the subshell ( "$(...) ). It is like this:

Code:
var=$( echo "blabla" )

The shell opens a subshell and executes echo "blabla" in this subshell. So you get some output at <stdout>, in this case a string. This output is then assigned to the variable.

The next thing is you put that assignment into background:
Code:
tot_files=<something> &

But an assignment is not a separate process, it is done within the shell internally. x=5 does not generate a process you could put into background (which is in fact just another way of saying it runs asynchronously to the main process). I am not sure about that (perhaps Don Cragun knows more about these fringe situations) but i suppose that the "&" is simply ignored in this case.

The next problem is:
Code:
tot_files=<...> & | wait ${!} | <...>

A pipeline takes the <stdout> (the "normal" output) of a process and feeds it into the input (<stdin>) of another process. The output of

Code:
var=value

is <nothing>. Try it out on the commandline. When you pour that nothing into another command, what will happen, hmm? Furthermore, the wait command does not even process any input, and neither does it have any output. So, even if there would be any output from the assignment operation it would be lost by now - for the same reasons as above. So, whatever you pipe the output of wait into, there will be nothing to process because there will be no output.

Now, after this lengthy explanation of what will not work, let us finish on some positive note:

In principle you do that the following way: first display the message for the user that he should wait, then you start the lengthy process. Finally you update (or maybe redraw, depending on which possibilities are offered by your environment) the screen with a finishing message and the result of the operation:

Code:
echo "Starting calculations, hold on...."
result=<...lengthy calculation here...>
echo "Finished. Result is: $result"

As an addendum i suggest to go over the counting command itself too (to me that looks not ideal, whatever it is supposed to do), but since i do not know exactly what you want to achieve i will not suggest an alternative yet. You are welcome to post your goals and we can do that too.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 03-28-2019
Quote:
Originally Posted by bakunin
... ... ...

The next problem is:

Code:
tot_files=$(<...>) &

This is not working at several levels.

First: what do you want to assign to the variable tot_files? Obviously the stdout of whatever is inside the subshell ( "$(...) ). It is like this:

Code:
var=$( echo "blabla" )

The shell opens a subshell and executes echo "blabla" in this subshell. So you get some output at <stdout>, in this case a string. This output is then assigned to the variable.

The next thing is you put that assignment into background:
Code:
tot_files=<something> &

But an assignment is not a separate process, it is done within the shell internally. x=5 does not generate a process you could put into background (which is in fact just another way of saying it runs asynchronously to the main process). I am not sure about that (perhaps Don Cragun knows more about these fringe situations) but i suppose that the "&" is simply ignored in this case.

... ... ...

bakunin
Actually, this is pretty straight forward. But, an "&" is never simply ignored in the shell command language. The "&" causes the previous list (where "list" in this case is a simple command that is an assignment statement) to be run asynchronously in a subshell. So, we not only have a subshell due to the parentheses, we have a sub-subshell to run the assignment statement in the background. It is not clear to me that the standards mandate whether the command substitution that is part of this command is run in the background or whether the shell can run the command substitution in the foreground and then run the assignment of the results of that command substitution to the specified variable in the background.

In either case result of the variable assignment is visible only in the sub-subshell; it is not visible in the environment of the shell that started the subshell and it is not visible in the subshell that started the asynchronous list.

I hope this helps.
- Don
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-28-2019
Quote:
Originally Posted by Don Cragun
Actually, this is pretty straight forward. But, an "&" is never simply ignored in the shell command language. The "&" causes the previous list (where "list" in this case is a simple command that is an assignment statement) to be run asynchronously in a subshell. So, we not only have a subshell due to the parentheses, we have a sub-subshell to run the assignment statement in the background. It is not clear to me that the standards mandate whether the command substitution that is part of this command is run in the background or whether the shell can run the command substitution in the foreground and then run the assignment of the results of that command substitution to the specified variable in the background.

In either case result of the variable assignment is visible only in the sub-subshell; it is not visible in the environment of the shell that started the subshell and it is not visible in the subshell that started the asynchronous list.
First off, thank you for clearing that up. It actually never occurred to me to put an assignment (or anything else not being an external executable, for that matter) in background so i couldn't draw on experience. One thing is still not completely clear to me and i hope you can clarify. You say:

Quote:
Originally Posted by Don Cragun
In either case result of the variable assignment is visible only in the sub-subshell; it is not visible in the environment of the shell that started the subshell and it is not visible in the subshell that started the asynchronous list.
I take it, the mechanism of

Code:
$( list; ... )

is to open a subshell, execute list there and then close the subshell. Now, any command (and, as i have just learned, even an assignment) put into background is executed in its own subprocess (and as the assignment is a shell command, a subshell, yes?). Therefore:

Code:
var=$( list; ... ) &

should, as i understood it, open a subshell (running asynchronously), execute
Code:
var=<something>

there and open another sub-subshell in there to calculate this <something> because <something> is actually $( ... ). So the assignment happens at the first subshell and the list inside $( list; ... ) happens at the sub-subshell. Have i gotten that right or am i a hopeless case?

Anyway, i have at least tested a bit and i found:

Code:
# x=5
# echo $x
5
# y=5 &
[1]     6887
[1] +  Done                    y=5 &
# echo $y
#

So, putting an assignment into background does not only make no sense it even (for practical purposes) nullifies its effect because the variable gets assigned in a subshell. At least this much i take away from your explanation and this is true regardless of in which way the subshells and sub-subshells are arranged. Thank you again.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 03-28-2019
[solved]

Thank you so much for the info provided... After your explanation I was able to make it work Smilie

Code:
tot_files_found="$(echo $RANDOM)_find.$CURRENT_USER.$(echo $RANDOM)$(echo $RANDOM)" 
while true; do
    if [ -f "$tot_files_found" ]; then      # File exists
        if [ -s "$tot_files_found" ]; then  # File exists and not empty; (find . has written the file; operation ended)
            break
        fi     
    else
        # recursively gets the total number of files     
        find . -type f | wc -l > $tot_files_found &    
    fi
done | yad --image="$MD5SUM_ICONS/binary.png" --progress --pulsate --progress-text="" --center --title="Getting things ready..." --text="Reading files..." --auto-kill --auto-close --no-buttons --no-escape --fixed

Of course if anyone has a better way to do this, I am all hears.
Thanks again.
# 7  
Old 03-28-2019
Still not clear why you (a) pipe (b) nothing into yad. Does it need meaningful data on its stdin?
a) piping is (not illegal but) not an apt way to put execution of commands into a certain order. If not reading from stdin, the second command may be finished before the first does anything meaningful.

b) No info will be supplied by your script; anything found will be output to $tot_files_found.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable gets auto updated after function execution

Hi Team In the below code, irrespective of the if statement that gets executed, retcd is being assigned a standard value(1) instead of changing as per code. Could you please help to see where is it going wrong. rval=0 CONF_FILE=/apps/wmroot/scripts/props/UMPath.properties NOHUP="nohup"... (3 Replies)
Discussion started by: harishshankar
3 Replies

2. Shell Programming and Scripting

Bash variable within variable

Hello, Can I ask how to expand variable that contains another in bash? I need to loop variable within another one like this: RD1=testgrp RD2=testgroup RD3=testgroupfile RD4=tstgroup ... RD40=try2013 DEST=/home/king/finaldir for i in {1..40}; do mv ${RD${i}} ${DEST} done I do not... (8 Replies)
Discussion started by: yifangt
8 Replies

3. Shell Programming and Scripting

Variable in bash help

#aa=xxxx #zz="cc $aa" #aa=gggg #echo $zz out put is cc xxxx if I want to get cc gggg how should I do, I don't want to write zz="c $aa " after aa=gggg (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Use variable in bash

$ p="1 2 5 8" $ set -- $p $ echo $3 5 $ k=3 $ echo \$${k} $3 I want the "echo \$${k}" to get the output 5 , how to modify ? (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. Programming

pthread question : global variable not updated

Hi, I wrote the following program to understand mutexes. If I run the program , number of threads is shown as zero, even after creating one thread. When running with gdb, it works fine. The function process is used to update global variable (used to keep track of threads). It looks like the... (2 Replies)
Discussion started by: sanjayc
2 Replies

6. Shell Programming and Scripting

bash - Variable made of variable

Hello, I am struggling with using variable made using "eval". a=4 eval b$a=20 echo $b$a ??? As shown above, I am trying to call back the variable "bX" assuming I do not know the value of "a". How can I do that? I tried several combinations but nothing worked. Thanks (10 Replies)
Discussion started by: jolecanard
10 Replies

7. Shell Programming and Scripting

[Bash]variable does not keep its value

Hello all, I have this shell script, but do not understand why the variables inside the if block does not keep its value outside. Is it because of the pipe ? How can i fix this problem ? Thank you for helping. local alarm="" local num_alarm=0 local -a alarms ... (3 Replies)
Discussion started by: trickstar
3 Replies

8. Shell Programming and Scripting

Using the value of one variable to name another variable (in bash)

Hello all, I'm working on a script, and as part of it, I'm trying to create a loop that will run through a stored piece of information a certain number of times pulling out information, and each time create a variable with a unique name that will store that information. I'm sure it's a simple... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, 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)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question