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

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?)

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.

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

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.

Different between " ; " and " && " in bash [duplicate]

This question already has answers here:
What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?
(4 answers)
Closed 2 years ago.
I've been doing lots of Linux based stuff with my time and I know that the ; is used to separate commands, and && runs command after the previous one is done.
But if anyone more knowledgeable then me can explain the difference between the two, that would be nice.
Here's a simple example:
whoami ; hostname
whoami && hostname
; will execute the second command whether or not the first returns without error.
&& is the bash AND logical operator, and will execute the second command only if the first returns succesfully without error.
The success of a command is determined by its exit status.

Linux - Capture exit code of a ruby script [duplicate]

This question already has answers here:
Exit Shell Script Based on Process Exit Code [duplicate]
(9 answers)
Is it possible to get the exit code from a subshell?
(3 answers)
Closed 8 years ago.
I have a simple ruby script which uses the abort function to exit with a non-zero exit code
#!/usr/bin/env ruby
puts "I ran"
abort "Exiting"
How can I capture the exit code when I execute this command in bash?
I have tried exit_code=./test or exit_code=ruby test to no avail.
Thanks
Try this:
./test
echo $?
The special shell variable $? contains the exit code of the last terminated program.
It does not matter whether your program is a ruby program. All Unix programs have an exit code which is handled alike in the starting shell.
The exit code of the last program that ran is stored in $?
You find the exit code from the previously executed command in the variable $?.

Exit code of command substitution in bash local variable assignment [duplicate]

This question already has answers here:
Why does "local" sweep the return code of a command?
(2 answers)
Closed 6 years ago.
How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function?
Please see the following examples. The second one is where I want to check the exit code.
Does someone have a good work-around or correct solution for this?
$ function testing { test="$(return 1)"; echo $?; }; testing
1
$ function testing { local test="$(return 1)"; echo $?; }; testing
0
If you look at the man file for local (which is actually just the BASH builtins man page), it is treated as its own command, which gives an exit code of 0 upon successfully creating the local variable. So local is overwriting the last-executed error code.
Try this:
function testing { local test; test="$(return 1)"; echo $?; }; testing
EDIT: I went ahead and tried it for you, and it works.

Resources