LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2012, 09:36 AM   #1
towardstheedge
LQ Newbie
 
Registered: Nov 2012
Posts: 6

Rep: Reputation: Disabled
Bash: using tests in a case structure


I am trying to simplify a series of loops by using a case structure.

This is for an optional exercise in my Linux class. I am new to Linux, bash, and programming in general. The case structure is not a requirement for the project, just something I wanted to try.

Code:
var1=0

while [ -f .tlight.run ]
do

case "$var1" in

[ "$var1" -lt 4 ] )

echo "Green"
;;

[ "$var1" -lt 6 ] )

echo "Yellow"
;;

[ "$var1" -lt 10 ] )

echo "Red"
;;

[ "$var1" -gt 9 ] )

var1=0
esac

((var1++))

sleep 1

done

Is something like this possible in case structure? I have tried several variations and keep getting errors.

Last edited by towardstheedge; 11-10-2012 at 09:46 AM.
 
Old 11-10-2012, 09:59 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It can be done, using the fact that bash does arithmetic evaluation of the pattern expressions and taking care to order the cases:
Code:
#!/bin/bash

var=$1
case 1 in
    $(( var < 4 )) )
       echo less than 4
       ;;  
    $(( var < 6 )) )
       echo less than 6
       ;;  
    $(( var < 10 )) )
       echo less than 10
       ;;  
    $(( var > 10 )) )
       echo greater than 10
       ;;  
    * ) 
       echo does not match specific cases
esac
 
1 members found this post helpful.
Old 11-10-2012, 02:59 PM   #3
towardstheedge
LQ Newbie
 
Registered: Nov 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the help. I got my script to work. I think it looks a lot cleaner then the bunch of nested ifs and loops.

This is the full script that works.

Code:
#!/bin/bash

#tlight
#towardstheedge
#nov 2012

clear

var1=0

while [ -f .tlight.run ]
do

case 1 in

$(( var1 < 4 )) )

echo "Green"
;;

$(( var1 < 6 )) )

echo "Yellow"
;;

$(( var1 < 10 )) )

echo "Red"
;;

$(( var1 > 9 )) )

echo "Green"
var1=0
;;
*) echo I got here somehow
#used for debugging
esac

((var1++))

sleep 1

done
There is one piece that still puzzles me:
Code:
case 1 in
I don't understand how bash is reading that line. I tried substituting different things for the '1' to see what it would do. I don't get how the 1 relates to any of the rest of the script.

Thanks again for the help.

Last edited by towardstheedge; 11-10-2012 at 03:01 PM. Reason: Edit to add: This script is called by another simple "switch" script.
 
Old 11-10-2012, 07:08 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
1 is arithmetic true.

To see how it relates to the $(( ... )) case "patterns", run this command at the command prompt: var=3; echo $(( var1 < 4 ))
 
  


Reply



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
LXer: Nested Statements for Additional Tests in Bash LXer Syndicated Linux News 0 04-17-2011 08:10 PM
[SOLVED] bash compound tests and parenthesis Jerry Mcguire Programming 4 03-24-2011 08:37 PM
Bash: change case graemef Programming 9 04-06-2010 08:48 PM
bash - copy with a directory structure mkiler Linux - General 6 06-03-2008 08:45 AM
Bash case structure (looping) ravvar Programming 4 10-07-2003 07:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 AM.

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
Open Source Consulting | Domain Registration