Bash script not performing a check properly
Hello,
I'm trying to throw together a bash script on Ubuntu Server 12.04. One of the sub-portions of the script is verifying wget exists in /usr/bin. If it doesn't exist, my script offers to install it via apt-get. Here's a potion of my script that is giving me problems. For ease of debugging, I made it its own shell script: Code:
#!/bin/sh Code:
root@Gatekeeper:~# sh wgetcheck.sh |
Try switching it to :
Code:
if [ -z $(ls -al /usr/bin/ | grep wget | cut -f16 -d " ") ] Code:
/usr/bin/which wget |
It is bad practice relying on ls better use if file exist
Code:
if [ -e "/usr/bin/wget" ]; then echo "hurrah"; fi |
Quote:
|
I've tried utilizing both methods (the -z and utilizing exit code checks) and they both work, now I run into a problem where the read statement works, but the portion where the script is looking for y or yes to indicate yes is not working. For readability, let me post version 2:
Code:
if the word yes is entered: I get an infinite loop where the text "= [yes]" is returned repeatedly. if I enter y, I get this: Code:
root@Gatekeeper:~# sh wgetcheckv2.sh Code:
root@Gatekeeper:~# sh wgetcheckv2.sh |
You can't do tests that way.
Break it out of the script and just run it from the command line. Does this work? Code:
if y = ["yes"] || ["y"]; then echo 1; fi If not, modify it until it does, then put the corrected version back into the script. The nice thing about scripting languages is you don't need the entire code to test one line. You can test that if statement by itself from the command line until it works, then insert the working version into the script. |
Alternatively, use case:
Code:
#!/bin/sh |
Quote:
I didn't even think to use a case statement. thank you very much for this. I ended up getting the or statement to evaluate, then I ran into a snag where the script would not take no for an answer. This solves the problem very well. Thanks again! |
Just an informational follow up, this could have been done pretty easily with the "select" function as well:
http://tldp.org/LDP/Bash-Beginners-G...ect_09_06.html |
Quote:
thanks again! |
FYI - a working if could have looked like:
Code:
if [[ "$wget_install" == "y" || "$wget_install" == "yes" ]] |
Quote:
Code:
|
Quote:
-eq is used for integer comparison, == is used for string comparison. This page gives a nice rundown of the differences between "[" and "[[". http://mywiki.wooledge.org/BashFAQ/031 Essentially it boils down to "[" is older and more compatible, "[[" is newer and more powerful. |
Thanks for the clarification suicidaleggroll.
|
nevermind
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS The [[ ]] construct is the more versatile Bash version of [ ]. This is the extended test command, adopted from ksh88. Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct. Arithmetic evaluation of octal / hexadecimal constants takes place automatically within a [[ ... ]] construct. |
It's the beauty of bash. There are many ways to skin a cat, but you still finish up with a skinned cat. :)
|
All times are GMT -5. The time now is 04:20 AM. |