Bash enumerated types


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash enumerated types
# 1  
Old 04-01-2012
Bash enumerated types

I have a variable, vbLevel and want to assign it to one of several values which are "none", "low", "medium", "high".

Then I want to do something like

Code:
if [ vbLevel -gt low ]; then
  ...
fi

# 2  
Old 04-01-2012
your text comparing doesn't makes sense to me. Please elaborate more
If you like to perform some action based in text value following code will help you
Code:
for var in none low medium high;
    do
        case $var in   
             none)      
                    <your statements here>
                    ;;   
             low)       
                     <your statements here>
                     ;;   
             medium)  
                    <your statements here>
                    ;;
             high)       
                    <your statements here> 
                    ;;  
        esac
    done


Last edited by 47shailesh; 04-01-2012 at 05:14 PM.. Reason: formatting
# 3  
Old 04-01-2012
You can do this in ksh93 and bash, without the dollar signs I mean...
Code:
none=0 low=1 medium=2 high=3
vbLevel=2
if [[ vbLevel -gt low    ]]; then
  echo hello
fi

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-01-2012
The assign can also be done with the enumerated type let vbLevel=medium
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 04-01-2012
Indeed. let is an arithmetic evaluation. A synonymous notation would be (ksh/bash):
Code:
(( vbLevel=medium ))

And you can also do this:
Code:
none=0 low=1 medium=2 high=3
vbLevel=2
if (( vbLevel > low )); then
  echo hello
fi


Last edited by Scrutinizer; 04-01-2012 at 07:00 PM..
# 6  
Old 04-01-2012
Quote:
Originally Posted by Scrutinizer
Indeed. let is an arithmetic evaluation. A synonymous notation would be (ksh/bash):
Code:
(( vbLevel=medium ))

And you can also do this:
Code:
none=0 low=1 medium=2 high=3
vbLevel=2
if (( vbLevel > low )); then
  echo hello
fi

So one does not use the $ sign for the variables?
# 7  
Old 04-01-2012
Inside the double parentheses you are allowed to leave the $-signs out if you wish..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

C++ Returning enumerated type

I am trying to create a function to return me the verbosity level, so I can call it as follows String s = "normal"; Verbosity log_level; log_level = get_log_level(s); I started a version in the last function but I am having trouble getting it to work. #ifndef __VERBOSE_HH__... (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

bash: find multiple types[solved]

Hi, I would like to use find to search for multiple types. For example search for symlink and regular file but not directories, sockets etc.... Something like: find . -type l,f -name "stuff" But of course it does not work. Is there any way to avoid an if statement and to do it faster? ... (0 Replies)
Discussion started by: Dedalus
0 Replies

3. Programming

enumeration types in C

If I want to declare an array of structures in C and have the number of items in that array to correspond to the items of an enumeration, is there a way to access the maximum value in the enumeration when declaring the array? For instance: typedef struct { various fields.... } ... (3 Replies)
Discussion started by: cleopard
3 Replies

4. UNIX for Dummies Questions & Answers

mime types

Hi, I am trying to launch an ogg movie from a pdf file which has been produced with pdflatex and \movie {\centerline{\includegraphics {grafiques_xerrades/un_manolo_amb_camera.pdf}}} {hlims_xerrades/XocCumuls.ogg} The switch "externalviewer" makes kpdf launch the default... (5 Replies)
Discussion started by: pau
5 Replies

5. UNIX for Dummies Questions & Answers

Two types of pipes?

What is the difference between: cd /tmp tar -cf - *.txt |gzip > tmp_txt.tar.gz and cd /tmp mknod pipe p gzip < pipe > /tmp/tmp_txt1.tar.gz & tar -cf pipe *.txt Apart from the fact that we have to create the pipe file manually, is there any difference in the performance of the two?... (5 Replies)
Discussion started by: blowtorch
5 Replies

6. Filesystems, Disks and Memory

associated file types

I have a file of type .for extension .In a guui based unix environment like solaris if I double click on that file a specific program designed by me has to run which takes this file as the parameter and exceutes the program. Can anyone help me? (8 Replies)
Discussion started by: nhk_srd
8 Replies

7. UNIX for Dummies Questions & Answers

backup types

can anyone explain me difference between tar and ufsdump commands.............and also i wd like to know the difference between incremental and differential backup......... thx in advance (1 Reply)
Discussion started by: girish_shukla
1 Replies

8. UNIX for Dummies Questions & Answers

So many types of LINUX's!

I installed Redhat into my system. The reason? This was the version my friend was running and he told me about this one, so I downloaded and installed it. Simple enough :D But as I am searching the net, I am coming across many other forms of linux made by other companies. Redhat seems to be... (2 Replies)
Discussion started by: Minnesota Red
2 Replies

9. UNIX for Dummies Questions & Answers

servers types

dear sir , i would like to ask about sun solaries servers generations ? i hear about sparc and ultra . i want to know the versions , and is there other servers types ?? Thank (3 Replies)
Discussion started by: tamemi
3 Replies
Login or Register to Ask a Question