LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need answers regarding case statements in bash scripts (https://www.linuxquestions.org/questions/linux-newbie-8/need-answers-regarding-case-statements-in-bash-scripts-830379/)

jahobjafwar 09-04-2010 07:40 PM

need answers regarding case statements in bash scripts
 
while learning to write bash scripts, i decided to write some script that, given an integer as input, will tell you the square root of that integer (provided the integer in question is a perfect square). i have already done on using primarily if statements and a while loop. i decided that using a case statement would be a lot simpler and i would be able to make the script more functional. here is what i have so far

#!/bin/bash

echo -n "Enter a perfect square intiger > "
read sqnum
case $sqnum in
# For positive intigers
-ge 0 ) echo "test"
;;
esac

if i try to run this it says

/home/wesley/bin/sq_root1: line 7: syntax error near unexpected token `0'
/home/wesley/bin/sq_root1: line 7: ` -ge 0 ) echo "test" '

i have tried all posible combos of using -ge or >= but i get pretty much the same thing.
the idea is, for now, if the input is greater than or equal to 0 that it will echo test. can you do this sort of thing with case statements? or will it only work if i give it specific values like [1-9] (if this is the case then i dont think the case statement will work for what i want to do)
any input is appreciated. thank you.

slakmagik 09-04-2010 08:08 PM

Right. Case statements make string matching comparisons. You can't use expressions like '-ge' as the pattern.

jahobjafwar 09-04-2010 08:28 PM

thats disappointing. i would think you could do anything you can do with an if statement, for example you can use things in case statements like
-f to test if the variable is a file or
-d for a directory etc. but then again i did try it and it does in fact not work.

slakmagik 09-04-2010 09:25 PM

Well, 'if' and 'case' and other things like that are bash flow control statements and what's actually testing for 'greater than' or 'is a file' is the 'test' command, usually written these days as '['. There, 'if' is checking the exit status of the 'test' command and can check the exit status of any other command, such as 'grep'. So if you had a command or function return '1','0', or '255' based on greater then/equal/less then, you could then do

Code:

command
case $? in
    1) action_based_on_gt ;;
    0) action_based_on_eq ;;
    255) action_based_on_lt ;;
esac

And, since the word 'case' checks is subject to command expansion, you could include a command instead of '$?' but the patterns to be matched would be checked against stdout rather than the exit code. For instance, if you had a command that would emit 'greater than', 'less then', or 'equal' to stdout, you could do something like

Code:

case $(command) in
    greater*) foo ;;
    less*) bar ;;
    equal) baz ;;
esac

But the pattern's pretty much just a pattern.

ghostdog74 09-04-2010 10:46 PM

Quote:

Originally Posted by jahobjafwar (Post 4088235)
while learning to write bash scripts, i decided to write some script that, given an integer as input, will tell you the square root of that integer (provided the integer in question is a perfect square). i have already done on using primarily if statements and a while loop. i decided that using a case statement would be a lot simpler and i would be able to make the script more functional. here is what i have so far

#!/bin/bash

echo -n "Enter a perfect square intiger > "
read sqnum
case $sqnum in
# For positive intigers
-ge 0 ) echo "test"
;;
esac

you can test for 0 as well, since its also a string

Code:

case "$sqnum" in
 "0") echo "0";;
 *) echo "not 0";;
esac



All times are GMT -5. The time now is 06:44 AM.