Computing.Net > Forums > Unix > IF statements Comparing variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

IF statements Comparing variables

Reply to Message Icon

Name: EDIBrian
Date: December 15, 2004 at 04:09:41 Pacific
OS: AIX 5L 5.3
CPU/Ram: 2@650 4GB
Comment:

I need to create an “if statement” that checks to see if variables have been sent.

My “if statement” must have at least two variables sent to be valid but I also need to be able to check to see if variable 3-12 have been passed. I need to execute specific functions for each additional variable passed. How do I check to see what the last variable passed is?



Sponsored Link
Ads by Google

Response Number 1
Name: cbailey33
Date: December 15, 2004 at 05:09:46 Pacific
Reply:

$# shows the number of command line variables.

if [ $# > 1 ];then
echo $#
else
echo "Not enough params"
exit 0
fi


0

Response Number 2
Name: EDIBrian
Date: December 15, 2004 at 05:28:55 Pacific
Reply:

Thanks...That's what I needed I knew it was simple. Thanks again.


0

Response Number 3
Name: Jim Boothe
Date: December 15, 2004 at 07:14:56 Pacific
Reply:

For the sake of accuracy, I guess we need to point out that the above solution is incorrect. That statement will never echo "Not enough params". Also, it will create an empty file named "1" because the right-angle bracket redirects output to a file. The proper test would be:

if [ $# -gt 1 ]


0

Response Number 4
Name: WilliamRobertson
Date: December 18, 2004 at 12:30:17 Pacific
Reply:

Or in Korn/Bash/Z shell:

if [[ $# > 1 ]]


0

Response Number 5
Name: Jim Boothe
Date: December 20, 2004 at 06:52:40 Pacific
Reply:

Thanks WilliamRobertson. I'm sure that's what Craig intended.


0

Related Posts

See More



Response Number 6
Name: Jim Boothe
Date: December 20, 2004 at 09:26:53 Pacific
Reply:

Note that the [[ x > y ]] construct is not intended for numerical compares, but rather for alphanumerical compares.  For example:

if [[ 5 > 450 ]] ; then
   echo 'Test1 is true'
else
   echo 'Test1 is false'
fi

if [ 5 -gt 450 ] ; then
   echo 'Test2 is true'
else
   echo 'Test2 is false'
fi

./testit.sh
Test1 is true
Test2 is false


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: IF statements Comparing variables

if statement www.computing.net/answers/unix/if-statement/6676.html

While read is ignoring if statement www.computing.net/answers/unix/while-read-is-ignoring-if-statement/7435.html

If statement in unix www.computing.net/answers/unix/if-statement-in-unix/7097.html