I have been working on a script for my father that manages several tasks through a script with many functions that chain together (organization purposes). This script relies entirely on zenity
for all user interaction (my father is really tech illiterate {Age 60+}).
The issue I'm having is that once I call the function transprog
it won't exit the loop even after the variable that the while
loop relies on is changed outside of the function.
The loop is called here:
copystat="1"
transprog & cp -rf "$netlocdirFIN/" "$transdir2/" # "transprog" is called here
copystat="0"
sleep 1
ret="$?"
echo "$ret"
transnew #calls next step/function
And this is the transprog
function:
transprog ()
{
i2="Im Working On It!"
(
while [ $copystat = 1 ]; do
# =================================================================
echo "25"
echo "# ^_^ $i2" ; sleep 1
# =================================================================
echo "50"
echo "# ^-^ $i2" ; sleep 1
# =================================================================
echo "75"
echo "# ^_^ $i2" ; sleep 1
# =================================================================
echo "99"
echo "# ^-^ $i2" ; sleep 1
done
echo "# Your files are DONE TRANSFERING!" ; sleep 1
echo "100"
) |
zenity --progress \
--title="File Transfer In Progress" \
--text="# Im Getting Warmed Up" \
--percentage=0 \
--auto-close \
--auto-kill
}
Once the copy is done the script continues on: the zenity progress bar is in the background ticking away and it really confuses him.
How can I tell the backgrounded function to exit?
(zenity uses the echo
values of 25,50,75 and 99 to change the "complete-ness" of the progress bar. and the echo "# (whatever)"
to change the text.)
all it is is something to tell him "Hey Duffus im bussy copying stuff dont shut me off"
i have tried Guss's solution: (seperated script for test purposes)
#!/bin/bash
transtest ()
{
i2="Im Working On It!"
var=0
trap 'var=1' 2
(
while [ "$var" = 0 ]; do
# =================================================================
echo "25"
echo "# ^_^ $i2" ; sleep 1
# =================================================================
echo "50"
echo "# ^-^ $i2" ; sleep 1
# =================================================================
echo "75"
echo "# ^_^ $i2" ; sleep 1
# =================================================================
echo "99"
echo "# ^-^ $i2" ; sleep 1
done
echo "# Your files are DONE TRANSFERING!" ; sleep 1
echo "100"
) |
zenity --progress \
--title="File Transfer In Progress" \
--text="# Im Getting Warmed Up" \
--percentage=0 \
--auto-close \
--auto-kill
}
transtest &
pid=$!
echo "$pid"
sleep 20 #code to copy? or just for this test?
echo "Killing Child: $pid"
kill -2 "$pid"
echo "wait"
wait
exit
i see result from echo "wait"
and it still keeps looping.