Bash user input while doing other stuff in background [duplicate] - bash

This question already has answers here:
How do you run multiple programs in parallel from a bash script?
(19 answers)
Closed 10 months ago.
I am trying to do a while loop while waiting for user input.
read x
while [ $x == 3 ]
do
echo yay
done
does not do what I want. Of course it does not work, but I type that and make sure no one gets confused

Well, I figured it out.
loop(){ while true; do echo -n .; sleep 2; done; }
loop & read x
kill %1
This defines a function called loop, runs it in background, waits for user input, then stops the background process.

Related

Make variables persist across instances of a script [duplicate]

This question already has answers here:
Variable in Bash Script that keeps it value from the last time running
(3 answers)
bash—Better way to store variable between runs?
(7 answers)
How to use Unix variables to set and retain values across session {bash} [duplicate]
(1 answer)
Closed 9 months ago.
I'm writing a script to toggle a program and need a way to make sh remember a variable after the script has executed and terminated. The only way I can think is by writing a daemon, but there must be a simpler way.
The code works when run in a persistent session, but cannot work as I intend it; the exported variables are deleted when the script finishes running.
I need a toggle as I'm planning to bind the script to a key to toggle japanese and english input, and need to switch between them.
Here's my code:
#!/bin/sh
export toggle=0
if [ $toggle = 0 ];
then
test -z $(pgrep wlanthy) && wlanthy & disown;
export toggle=1;
elif [ $toggle = 1 ];
then
test $(pgrep wlanthy) && killall wlanthy
export toggle=0
else echo error
fi
the problem was solved simply by doing this. I was overthinking it:
#!/bin/sh
if test -z $(pgrep wlanthy);
then
wlanthy & disown;
exit
elif test $(pgrep wlanthy);
then
killall wlanthy
exit
else echo error
fi
This script is bound to a single key that toggles an IME (an input method engine, which turns english keyboard text into japanese.) , meaning the script needs to both start and stop the program, hence its behaviour.
For example, press the key -> type some japanese -> press the key again -> type some english.

how to run two python scripts parallelly using shell script and save their return value in a variable [duplicate]

This question already has answers here:
Get exit code of a background process
(13 answers)
Closed 1 year ago.
I have two python scripts and I have to run them parallelly at the same time and store their retun value in a variable. Could you please help me how to do this?
Example:
script1.py and script2.py
x is variable to store script1.py return value
y is variable to store script2.py return value
both the scripts are returning either 0/1.
You can run the process on the background using & operator. Then you have to wait for all the background processes using wait. This command also provides return code of the awaited process.
Quick and dirty example:
#!/bin/bash
# first command to be executed
sleep 3 &
pid1=$!
# second command to be executed
sleep 5 &
pid2=$!
wait $pid1
x=$?
wait $pid2
y=$?
echo "x: $x, y: $y"

Bash executes comands out of sequance sometimes [duplicate]

This question already has answers here:
Why didn't the shell command execute in order as I expect?
(4 answers)
bash script order of execution
(2 answers)
Closed 3 years ago.
I running multiple commands in bash in parallel and need to get output that is delimited so receiving script could separate the values.
I attempted to do this in few ways but it seems that any echo is executed instantly and anything following after.
So I am trying to find a way to separate input from each output with separator preceding output.
I actually use curl request that may take 50-200ms to respond, but here for simplicity I will give example with time command.
Here is rough example:
echo ">" && time &
echo ">" && time &
echo ">" && time &
wait
This produces >>> time time time
I am looking for a way to make it produce >time>time>time
I had some success trying to call other bash scripts with trailing echo command instead of making actual commands and that works most of the time but inevitably things get mixed up because of timing.
I will post updates as I work on it, thank you for the help
Try this:
echo ">$(time)" &
echo ">$(time) &
echo ">$(time)" &
wait
That tells echo that it needs the output of the time command you have before it can do its thing.

Stop process as soon as a file contains "Success" [duplicate]

This question already has answers here:
How do I use a file grep comparison inside a bash if/else statement?
(5 answers)
How to kill a background process created in a script
(2 answers)
Closed 5 years ago.
I'm trying to stop a long running process as soon as the file /status contains the string Success.
I tried this without success:
cat & while [ `grep -q Success /status` ]; do sleep 1; done; kill %1
cat is the long running process that needs to be stopped when /status contains Success.
Cheers

What is $? var in script below on condition of while loop [duplicate]

This question already has answers here:
What is the $? (dollar question mark) variable in shell scripting? [duplicate]
(9 answers)
Closed 9 years ago.
#!/bin/sh
host google.com>/dev/null
while [ $? -ne 0 ];
do
sleep 3
done
say "You are now connected to internet"
I guess $? is associated with google.com>/dev/null, making the logic work, but i am interested in detail description on $?
Thanks in advance.
I know this will sound pedantic, but $? is not a variable, it is a value. ? is the name of the variable, placing $ at the front gives the value.
Now you can search for ? in man bash:
Expands to the exit status of the most recently executed foreground pipeline.
It is often tested unnecessarily. if and while statements in bash (and most shells) test for success(true) or failure(false). Success is where the value of ? is 0, and failure when it is some other number (which has the range 1-255). ! means "not", as in many languages, and inverts the truth:
while ! host google.com>/dev/null
do
sleep 3
done
echo "You are now connected to internet"
(I had to use echo, not sure where say comes from, Perl?)

Resources