LinuxQuestions.org
Register a domain and help support LQ
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 07-07-2014, 03:45 PM   #1
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Rep: Reputation: 0
many if case


Hi all,
I am looking for an elegant solution for many if cases as:
if [ $chr = 1 ]; then
chri=249250621
fi;
if [ $chr = 2 ]; then
chri=243199373
fi;
if [ $chr = 3 ]; then
chri=198022430
fi;
if [ $chr = 4 ]; then
chri=191154276
and so on...
is there any other option to do handle many if's in bash script? to do the script shorter..

Thanks,
Pap
 
Old 07-07-2014, 04:21 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
Use a case statement:
http://tldp.org/LDP/abs/html/testbranch.html#EX29
 
Old 07-08-2014, 03:12 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
If all you are doing within each "if" is setting a value then make use of arrays:

Code:
#Define the mapping
chri[1]=249250621
chri[2]=243199373
chri[3]=198022430
chri[4]=191154276


#Later on just use
echo ${chri[${chr}]}
An "elegant" method would also be to load the array in one go, here the "0" value is to populate chri[0] to preserve your mapping:

Code:
chri=(0 249250621 243199373 198022430 191154276)

echo ${chri[${chr}]}
 
2 members found this post helpful.
Old 07-09-2014, 05:22 AM   #4
papori
LQ Newbie
 
Registered: Feb 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TenTenths View Post
If all you are doing within each "if" is setting a value then make use of arrays:

Code:
#Define the mapping
chri[1]=249250621
chri[2]=243199373
chri[3]=198022430
chri[4]=191154276


#Later on just use
echo ${chri[${chr}]}
An "elegant" method would also be to load the array in one go, here the "0" value is to populate chri[0] to preserve your mapping:

Code:
chri=(0 249250621 243199373 198022430 191154276)

echo ${chri[${chr}]}
Many Thanks!
 
  


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
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 03:17 AM
Help with sed and awk to change L-case letters to U-case for specific lines in a file rootaccess Linux - General 12 05-21-2012 03:50 PM
mkisofs -iso-level 1 converts to lower case instead of upper case. stf92 Slackware 3 10-29-2010 01:32 AM
Copying files from case-sensitive Linux to case-insensitive Windows via CIFS? SlowCoder Linux - General 4 05-07-2008 08:03 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 05:09 PM


All times are GMT -5. The time now is 02:12 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