Can I reload variable in a watch command?
Solution 1
Contrast:
$ watch -n 1 "echo $(date)"
Every 1.0s: echo Sat Apr 27 03:10:50 CEST 2013
$ watch -n 1 'echo $(date)'
Every 1.0s: echo $(date)
What you've done is to run echo "($ls DirFlat |wc -l)*100/$FileNum"|bc
and date
, substitute the output of each command into the shell command watch -n 100 "echo $(…) % $(…)"
, and run that. You need to prevent the expansion of the command in the parent shell, and instead pass it as a string to watch
for it to run. This is a simple matter of using single quotes around the command instead of double quotes.
Solution 2
Alternatively you can wrap it into a shell loop:
while sleep 100; do
(... your stuff ...)
if [ (... process has finished ...) ]; then
(... beep or get operator's attention otherwise ...)
break
fi
done
Solution 3
You have to put this into a shell script and let watch call this script then.
Related videos on Youtube
AncientSwordRage
I'm just this guy, y'know? (he/him) Secret ninja-muslim programmer, roleplayer and all-round nuisance. Desh by marriage. I have approximate knowledge of many things Cat thumb-servant. I moderate Sci-fi StackExchange, but I'm active on RPG.se, Puzzling.se and I dabble on the writing and worldbuilding StackExchanges. Sci-Fi I used to be the secretary of my university Sci-fi and fantasy club and I love everything from Horror to 'Saturday morning cartoons'. I help moderate this stack (with a diamond), and I'm active/interested in the marvel, back-to-the-future, star-wars and ghostbusters tags. Happy to meet and greet new users, as well as point people in the right direction for story-id questions. RPG TO BE FILLED StackOverflow TO BE FILLED
Updated on September 18, 2022Comments
-
AncientSwordRage almost 2 years
Part of my job involves some data handling. One of the tasks is to 'flatten' a set of directories (which we'll call Dir for now), and copy them to a new location called DirFlat.
This can take a long time (30 minutes ->2-3 hours)! I'd like to be able to watch the progress, so I use
find Dir -type f|wc -l
to get the number of files (lets call this$Filenum
, and then I run a very short command that I wrote (retyping from my notebook, may have copied it wrong, I hope you get the gist):echo $(echo "($ls DirFlat |wc -l)*100/$FileNum"|bc) "%" $(date)
However, if I run
watch -n 100 "!!"
it takes the output of the echo, and keeps printing that (even the date doesn't change).Can I get this to refresh the variable/re-run the assignment of the internal variables in BASH? Hopefully this will help me in automating some of my tasks.
-
AncientSwordRage about 11 yearsCould you give me an example?
-
Hauke Laging about 11 years@Pureferret Example?
watch -n 100 /my/script.sh
? -
AncientSwordRage about 11 yearsThis is probably the behaviour I'm after! If I push this to the background, will it still output to the terminal?
-
peterph about 11 yearsYes, but remember not to close the terminal. Or better run it in a terminal multiplexer like
tmux
orscreen
. -
Alireza about 4 yearsI know SO comments are not for endorsements or personal resolutions, but man, I think now's a good time for me to sit down and re-learn shell from the ground-up.