If Statement With Blank Variable - bash

I can't get this to work correctly. If the wifissid is blank, it needs to display "Wireless: Not Connected". If the wifissid is not blank, it needs to display "Wireless: Connected To $wifissid"
wifissid=Test
if [ $wifissid = "*" ]; then
WirelessOutput=" Wireless: Connected To $wifissid"
else
WirelessOutput=" Wireless: Not Connected"
exit
fi
echo " $WirelessOutput"

Try this. The ! -z checks to see if $wifissid is not (!) empty/null (-z). Or use -n which basically is the same as ! -z. More details on these operators here:
#!/bin/bash
wifissid=Test
# if [ -n "$wifissid" ]; then
if [ ! -z "$wifissid" ]; then
WirelessOutput=" Wireless: Connected To $wifissid"
else
WirelessOutput=" Wireless: Not Connected"
fi
echo " $WirelessOutput"
exit
Also, I didn’t understand why you had an exit in the else statement since that would prevent the echo from ever happening. So I removed that and added it to the bottom of the script.
Additionally, I added #!/bin/bash to the top of the script because that is just something you should get in the habit of doing. It helps prevent issues such as if this script were running as a cron job, the default sh instead of bash would interpret the script & it would fail.
Might seem like small things, but it’s the little things that will drive you nuts if/when things don’t work out as expected.

Related

Looking for the existence of an ENV Variable

So, I'm guessing this may be a bug, or maybe I've botched something up, I dunno...
I have a script that was always working but I've been tasked to make it work using the new WSL2. I've snipped out the first block of code as it's giving me issues right off the bat. As you'll see below, I'm simply trying to determine if a variable has been set or not. This works in my Linux VM and also in Cygwin, however, it doesn't work in WSL2. Here is the code:
#!/bin/bash
echo $test_var
set -e
if [ ! -d "$test_var" ]; then
echo Please set test_var.
exit
fi
When I run this in any of the working systems I get the output of the variable.
In WSL2 I get the output of the variable followed by Please set test_var. And it stops the script due to the set -e as it's supposed to do thinking the var isn't set.
Any help would be appreciated.
Thanks
If your intention is to check if a directory exists (whose name apparently needs to be set as an environment variable in $test_var), if that directory exists relative to the current directory where the script is executed, you want something like:
#!/bin/bash
echo "$test_var"
set -e
if [ ! -d "$test_var" ]; then
echo "cannot find directory $test_var"
exit
fi
Note that here I have only changed your message, and your problem might possibly be explained by the fact that you do not have such a directory under WSL2.
If, on the other hand, you want to check if some environment variable (whatever it represents) is set, you want the -z option, something like:
#!/bin/bash
echo "$test_var"
set -e
if [ -z "$test_var" ]; then
echo "Please set test_var."
exit
fi
Note the absence of your negation sign (!).

Troubleshooting a script for my RPI to toggle displays

So I have a Quimat LCD attached to my gpio.
included is a script which runs to switch to the display (LCD35-show), and another to switch back to the HDMI port (LCD-hdmi). This causes a reboot when done so any variable changes have to happen prior to this.
as this is for my mother who is afraid of touching command prompt, I am trying to set up a single icon to use to switch between video sources.
I am a novice at coding, most of my experience from dabbling in BASIC, and have spent a couple days searching and trying to set this up, but apparently am failing at how to search properly as I couldn't get it functioning.
What I have done so far is this:
Created text file state.txt to hold a variable stating what mode the device is in (HDMI or LCD)
My attempt was to read the variable, then use if then statement to determine which file to run, change the variable then run the file.
This is the code I ended up with.
!/bin/bash
read var < state.txt
if var == HDMI
then
echo LCD > state.txt
cd LCD-show/
sudo ./LCD35-show
else
echo HDMI > state.txt
cd LCD-show/
sudo ./LCD-hdmi
fi
I am hoping someone can show me what I did wrong, and hopefully explain what I missed in the process.
Be careful with your bash script comparisons.
Wrap strings in quotes (or some other method), so it's not a syntax error when string-vars evaluate to empty. You can use == for string comparisons in bash, but = works in bash and sh.
#! /bin/bash
EXE=`basename "$0"`
LCD_DIR="LCD-show"
STATE_FILE="state.txt"
if [ ! -d "$LCD_DIR" ]; then
echo "$EXE: $LCD_DIR does not exist"
exit 1
fi
read var < state.txt
if [ "$var" = "HDMI" ]; then
echo LCD > "$STATE_FILE"
cd "$LCD_DIR"
sudo ./LCD35-show
else
echo HDMI > "$STATE_FILE"
cd "$LCD_DIR"
sudo ./LCD-hdmi
fi
The difference between a good script and a great script is error handling.
What happens when the dir LCD-show does not exist? (for whatever reason).

Creating a "First run" notification in bash?

What is the best way to make a "First run" notification in a bash script?
I tried putting export FIRSTRUN="no" first,
and later on put
`if [ -z "$FIRSTRUN" ]; then
echo "It seems that this may be your first time running this script.";
echo "Please ${GREEN}test whether the needed components are installed${RESET}.";
echo "";
fi`
but it simply does it on every time the script is run. If i put the export FIRSTRUN="no" after the if part, it never runs the if part of the code. So that doesn't work.
I am new at this so please help :D
You can't modify the parent shell state from within your script, so attempting to export a variable will not work.
You could use a config file to store a variable which determines whether the script has been run already:
#!/bin/bash
# load vars from config at the start of the script
if [[ -f ~/.your_script.conf ]]; then
. ~/.your_script.conf
fi
# check whether var has been set to determine first run
if [[ -z $has_run ]]; then
echo 'first run'
# set variable in config file for next time
echo 'has_run=1' >> ~/.your_script.conf
fi
Alternatively, if this is the only "config" you have, you could just create a file after the first run and check for its existence to determine whether the script has been run.
The first run of the script should leave behind some sort of permanent evidence that it has run; it can then look for that file on startup.
do_init () {
# initialize whatever needs initializing
touch ~/.alreadyran
}
if ! [ -f ~/.alreadyran ]; then
echo "First time running."
do_init
fi
If the initialization isn't per-user, it will need to leave the file in a more public place, such as somewhere under /var.

test for process return status returns unexpected results on ubuntu

Here's a piece of shell script code that I am executing on an ubuntu machine:
myProcess
ret="$?"
if [ "${ret}" == "0" ]
then
echo good
else
echo bad "${ret}"
fi
So the logic is pretty simple: if myProcess returns a non-zero exit status, it's bad, otherwise good. I tested this piece of code by isolating it into a separate script and running it from command line. When myProcess returned 0, I got good as expected.
However, when I run it in production, I get bad 0. So even though the return code seems to be 0, the if test seems to return false. What's going on here?
It appears that the problem was in the use of too many quotes possibly in combination with ==. The following modified code seems to be working fine:
myProcess
ret=$?
if [ ${ret} -eq 0 ]
then
echo good
else
echo bad "${ret}"
fi
There's a simpler way to do it (no need to treat $? and creating a copy of the variable):
if myProcess; then
echo "good"
else
echo "bad" >&2
fi
If myProcess is coded properly, that should works. If not, paste the exact output/errors in your original post.
This short and concise way is named boolean logic

Check if file exists and continue else exit in Bash

I have a script that is one script in a chain of others that sends an email.
At the start of the script I want to check if a file exists and continue only if it exists, otherwise just quit.
Here is the start of my script:
if [ ! -f /scripts/alert ];
then
echo "File not found!" && exit 0
else
continue
fi
However I keep getting a message saying:
line 10: continue: only meaningful in a `for', `while', or `until' loop
Any pointers?
Change it to this:
{
if [ ! -f /scripts/alert ]; then
echo "File not found!"
exit 0
fi
}
A conditional isn't a loop, and there's no place you need to jump to. Execution simply continues after the conditional anyway.
(I also removed the needless &&. Not that it should happen, but just in case the echo fails there's no reason not to exit.)
Your problem is with the continue line which is normally used to skip to the next iteration of a for or while loop.
Therefore just removing the else part of your script should allow it to work.
Yes. Drop the else continue. It's entirely unneeded.

Resources