LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-04-2010, 07:40 PM   #1
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Rep: Reputation: 0
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.
 
Old 09-04-2010, 08:08 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Right. Case statements make string matching comparisons. You can't use expressions like '-ge' as the pattern.
 
Old 09-04-2010, 08:28 PM   #3
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 09-04-2010, 09:25 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 09-04-2010, 10:46 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jahobjafwar View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] if statements in a loop MarkGM Programming 3 04-02-2009 09:12 PM
LXer: Optimizing Switch-Case Statements In C For Speed LXer Syndicated Linux News 0 02-01-2009 06:21 AM
Case statements and vars for bash script geech Programming 1 01-09-2009 05:49 PM
BASH IF statements Part II kinetik Programming 4 05-07-2006 02:18 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM


All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration