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
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?
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
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
Summary: I have a file that consists of a blank line, then either Y or N and then another blank line. I want to take this into an if statement saying if the file contains a Y then do this else do that. I've tr...
Summary: I have written a scipt that will report when a drive is down, but i finding if the status of the drive is UP, then "while read LINE" ignores the IF statement. I want the script to print the echo "Al...