[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?
# 8  
Old 03-28-2019
Hello,
What I am trying to achieve is the following:
As long as find . process is up and running, I want to show a message to the user and that is why I use
Code:
| 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

This is a yad's window running waiting for find . process to terminate yad.png - Google Drive

I use $tot_files_found to store the result coming from find . and to break from the while loop

That's all. I added wait ${!} to prevent multiple instances of find to be executed.

I am sure there are much better ways to do the same, but I am new in bash Smilie

Right now, everything seems to work but I would like to find a better way to accomplish what I want to do.

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 & 
        wait ${!}
    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


Last edited by soichiro; 03-28-2019 at 09:36 PM..
# 9  
Old 03-28-2019
The entire purpose of running a job asynchronously is to allow you to run other jobs at the same time. If you don't want to run any other jobs at the same time while your find command is running; don't run it asynchronously.

The two commands:
Code:
        find . -type f | wc -l > $tot_files_found & 
        wait ${!}

produce exactly the same output that the single command:
Code:
        find . -type f | wc -l > $tot_files_found

does, but takes fewer keystrokes and makes less busywork for the shell. The first two commands force the shell to create a subshell to run find asynchronously; the last command is run in the current shell execution environment without the need to create a subshell environment. The first two commands slow down your system for you and other users on your system.

Note also that every time this script runs, it creates a new temporary file and (from what you have shown us) never uses anything written into it and never removes it. And the name you are using for that temporary file (with three random numbers included in the name and with two of those random numbers adjacent to each other with no separator) increases the chance that two different invocations of your script will accidentally come up with the same name. If that happens, find will never be run for this invocation of your script.

And, there is no need to create three subshells to include those three random numbers in the name. The command:
Code:
tot_files_found=${RANDOM}_find.$CURRENT_USER.$RANDOM$RANDOM

produces exactly the same temporary filename as your current code does but avoids creating three unneeded subshells to set the name.

A more common way to do this (since there can never be two shells running with the same process ID) is to use:
Code:
tot_files_found=find.$CURRENT_USER.$$

to lessen the chance of a collision with your various multiple adjacent uses of $RANDOM. Of course, we might also comment that as far as we can tell from what you have shown us, $CURRENT_USER will expand to an empty string because it has never been set to anything else. And, since we have no idea how, or if, CURRENT_USER has been set, any expansion of tot_files_found when field splitting might occur should be included in a double-quoted string.

From everything that you have said about what you are trying to do, I do not understand why you have a loop at all? The following would seem to be a better fit for your stated goals (which I still do not understand):
Code:
tot_files_found=find.$CURRENT_USER.$$ 

# recursively gets the total number of files     
find . -type f | wc -l > "$tot_files_found" |
    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

But, as has been said before, having yad as the second process in a pipeline when the first process in that pipeline will NEVER write anything into that pipe is not a "normal" way to do things. Whatever output yad produces has nothing to do with whether or not find is making any progress; all it can tell us is when find has terminated. And that could be done more simply with:
Code:
tot_files_found=find.$CURRENT_USER.$$ 

# recursively gets the total number of files     
echo 'find is running'
find . -type f | wc -l > "$tot_files_found"
echo 'find is done'

We should probably also include a trap to remove the temporary file created by this script, but I haven't yet figured out if there might be some reason why you would want to actually know how many files were found instead of just wasting time figuring out how many there are and then exiting without ever looking at the results.

Last edited by Don Cragun; 03-29-2019 at 01:45 AM.. Reason: Fix mismatched parenthesis.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 03-29-2019
To show the yad picture while find is running, did you consider
Code:
yad ... &
find ...
kill %1 (yad's jobspec, hopefully)

This User Gave Thanks to RudiC For This Post:
# 11  
Old 03-29-2019
To expand on what my colleagues RudiC and Don Cragun has already said:

I might not be (see above) the biggest expert on all things subshell but i know a thing or two about programming. Make no mistake, writing a shell script of 5 lines is essentially a programming task and it is never too early to start on healthy habits.

Programming is mostly about organizing your thoughts and the best way to organize them is to compartmentalize big problems into several smaller problems again and again until there are no problems any more. It is like moving a big heap: you take a shovel and dig one small shovel full after the other until there is no heap any more. If you try to move the whole pile you are likely to fail but with a single shovel full it is easy to succeed. One shovel - success, and another one - success again and before you notice you have the pile moved.

So, let us start with - as exactly as possible - clarify your goals: you want to get a count of (a certain type of) files within a certain subtree. Yes? (I don't mind if you respond "NO!" here, because this is what this stage is about - clarifying your goals. If i haven't understood them correctly just say so.)

You also want - because the operation might take some time - display a window telling the user to wait for completion which you want to update to tell when the counting is finished (probably, but that is my assumption, along with presenting the result). Yes?

So, first for the counting - note that we set aside everything else here! Again, shovel for shovel and we concern ourselves here with only this shovel, not the pile and not another shovel. We can use find for this but we can do that in a single instance because find can traverse the filesystem (or select parts of it) on its own. To start several instances of find and then compile their results is simply not necessary. In addition to what RudiC and Don Cragun already said you find a more thorough introduction to find here. See how far that gets you and ask if you still have questions.

There is another question to this: how is the result of this operation propagated to whereever you will need it. I will postpone that for the moment because the rest of the operation is not clear yet.

Second is the business with the user notification. I told you above that the basic logic of this is to:

- present a user the initial window with a "please wait..."-message
- run the find-operation
- replace the "please wait...."-message with something else (i.e. a "done" or "result is:..."-message)

This is basically the same as what RudiC (and myself) earlier have already suggested. For this you do not need any pipes, background processes or anything else of that sort as Don Cragun has already explained.

So, if you want help for the further commencement of this, you are welcome to more clearly describe your goals and we can take it from there.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 12  
Old 04-02-2019
In the end, thanks to you guys, I did the following:

Code:
tot_files_found=0
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 --width=300 --height=$HEIGHT &
tot_files_found=$(find . -type f -perm -a=r | wc -l)
kill %1

and since I am not using a sub-shell any-longer, I am able to store the value returned by find into a regular variable instead of a file Smilie
On top of that, I changed all the $(echo $RANDOM) with .$CURRENT_USER.$$ in my other scripts too.

One last thing though... Is possible to kill/terminate the find process as soon as I close the yad window?

Bye and thank you so much for all your help and detailed explanations.
# 13  
Old 04-02-2019
Quote:
Originally Posted by soichiro
I changed all the $(echo $RANDOM) with .$CURRENT_USER.$$ in my other scripts too.
Here are two tips: first, if you operate with variables inside larger expressions like .$CURRENT_USER.$$ it is a good idea to use the long form of notation: .${CURRENT_USER}. The difference is that the curly braces make sure where the variables name ends and the rest of the string begins so that this rest of the string is not mistaken as a part of the variables name. For example: you want to add a slash to a variable so $var/ is fine, but add an underscore instead and $var_ would be interpreted as a variable named "var_". ${var}_ will always work, though.

Another tip is: clean up behind you. If you create a temporary file make sure that it is removed when the program creating it exits. You can do that with "traps", which is a theme for another thread but you might want to read up the shells documentation for it.

Quote:
Originally Posted by soichiro
One last thing though... Is possible to kill/terminate the find process as soon as I close the yad window?
Yes - but that would be really advanced stuff. You would need to go back to sending the [icode]find/icode] into background, then the yad-window in another background and check in the main program if either of these subprocesses are finished, then kill the other. The logic is tricky and i suggest to put that on the backburner until you have some experience with shell programming. Get some shell-programming done and you will be able to do it in short time.

I hope this helps.

bakunin
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