What am I doing wrong here?

Forum rules
Before you post please read this

What am I doing wrong here?

Postby Cmiller21 on Sat Jan 11, 2014 1:05 pm

Code: Select all
#!/bin/bash

VALUE1=8
VALUE2=10
VALUE3=12
if [ "`expr $VALUE1 + $VALUE2`" -eq "11" ]; then
   echo "Can you say the number $VALUE1"
else if [ "$VALUE2" -lt "$VALUE3" ]; then
   echo "What's sup."

   echo "Not Valid"
fi


Okay, so I am learning Bash scripting, so every now and then I make small scripts so that eventually I can use them proficiently and one day aplly them to real life. This script is me playing around with loops, as it is what I am trying to learn at the moment. There is no actual purpose of the script other than to demonstrate a loop (not finished yet) What is suppose to happen is the first condition is not suppose to be met (this is intended), so it should automatically go to the second condition which is true. Also, if none of the above condition would be met, then the prompt will echo "not valid" However, it won't go through all the way. It keeps producing an error. Obviously I made a mistake somewhere along the way, and I can't figure it out. Could someone help me identify my mistake, and give me a solution? Thanks

Anyway, I'm suppose to be playing around with this script and experimenting with different options. I'd like to get if - then statements down patch.

~Cmiller
Cmiller21
Level 1
Level 1
 
Posts: 31
Joined: Wed Nov 27, 2013 5:34 pm

Linux Mint is funded by ads and donations.
 

Re: What am I doing wrong here?

Postby xenopeek on Sat Jan 11, 2014 1:18 pm

It's not "else if", it's "elif". So line 8 isn't correct. It should read:
Code: Select all
elif [ "$VALUE2" -lt "$VALUE3" ]; then

And what you have written in a sh script, not a bash script. In bash you would do the above as:
Code: Select all
#!/bin/bash

VALUE1=8
VALUE2=10
VALUE3=12
if [[ $((VALUE1 + VALUE2)) -eq 11 ]]; then
   echo "Can you say the number $VALUE1"
elif [[ $VALUE2 -lt $VALUE3 ]]; then
   echo "What's sup."

   echo "Not Valid"
fi

The double-bracket conditional has less surprises than the single-bracket conditional and is preferred if you are writing only for bash. See more info on the bash hackers wiki: http://wiki.bash-hackers.org/syntax/ccm ... expression. Or the bash faq: http://mywiki.wooledge.org/BashFAQ/031. Or the bash manpage :wink:

And as you can see you can do math inside $(( )), and need not prefix variable names with $.
Forum Rules | IRC Channel Rules
Image
Arch Linux / 64-bit / Gnome Shell
User avatar
xenopeek
Level 21
Level 21
 
Posts: 13700
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: What am I doing wrong here?

Postby Cmiller21 on Sat Jan 11, 2014 2:38 pm

Thanks Xenopeek, very much apperciated. Not only does my script work, but I also know how to properly do if, then, and else statements. :)

~Cmiller
Cmiller21
Level 1
Level 1
 
Posts: 31
Joined: Wed Nov 27, 2013 5:34 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests