Optimize the nested IF


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimize the nested IF
# 1  
Old 01-22-2012
Optimize the nested IF

Hi,
I have to assign a value for a varaiable based on a Input. I have written the below code:
Code:
if [ "$procnam" == "admission" ]
 then
  nf=65
elif [ "$procnam" == "reporting" ]
 then
  nf=46
elif [ "$procnam" == "transfer" ]
 then
  nf=164
elif [ "$procnam" == "diagnosis" ]
 then
  nf=545
elif [ "$procnam" == "emergeny" ]
 then
  nf=56
elif [ "$procnam" == "inpatient" ]
 then
  nf=37
else
exit 0
fi

Can someone give me an idea of how to optimize it/make this short?
Also, what should be done to do a incase-sensivite comparison?

Like,
Code:
if [ "$procnam" == "Admission" ]
 then
  nf=65
elif [ "$procnam" == "REPORTING" ]
 then
  nf=46
...
fi

Smilie
# 2  
Old 01-22-2012
How about:

Code:
case $procnam in
  admission ) nf=65  ;;
  reporting ) nf=46  ;;
  transfer  ) nf=164 ;;
  diagnosis ) nf=545 ;;
  emergency ) nf=56  ;;
  inpatient ) nf=37  ;;
esac

To make it case insensitive you could convert procnam to lowercase first:
Code:
procnam=$(echo "$procnam" | tr [:upper:] [:lower:])


Last edited by Scrutinizer; 01-22-2012 at 06:58 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-22-2012
Thanks It works. Also, how to mark a thread as Solved / Closed in this forum?
# 4  
Old 01-22-2012
Quote:
Originally Posted by machomaddy
Thanks It works. Also, how to mark a thread as Solved / Closed in this forum?

Why close it? You may get other useful or more optimum answers.

For example, in bash4 you can use parameter expansion to make it case-insensitive:
Code:
case ${procnam,,} in
  admission ) nf=65  ;;
  reporting ) nf=46  ;;
  transfer  ) nf=164 ;;
  diagnosis ) nf=545 ;;
  emergency ) nf=56  ;;
  inpatient ) nf=37  ;;
esac

These 2 Users Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Optimize the Script Further

Hi All, I have written a new script to check for DB space and size of dump log file before it can be imported into a Oracle DB. I'm relatively new to shell scripting. Please help me optimize this script further. (0 Replies)
Discussion started by: narayanv
0 Replies

2. Shell Programming and Scripting

Looking to optimize code

Hi guys, I feel a bit comfortable now doing bash scripting but I am worried that the way I do it is not optimized and I can do much better as to how I code. e.g. I have a whole line in a file from which I want to extract some values. Right now what I am doing is : STATE=`cat... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

3. Shell Programming and Scripting

Optimize my mv script

Hello, I'm wondering if there is a quicker way of doing this. Here is my mv script. d=/conversion/program/out cd $d ls $d > /home/tempuser/$$tmp while read line ; do a=`echo $line|cut -c1-5|sed "s/_//g"` b=`echo $line|cut -c16-21` if ;then mkdir... (13 Replies)
Discussion started by: whegra
13 Replies

4. Shell Programming and Scripting

Optimize awk command

WARNING=${1} CRITICAL=${2} echo ${OUTPUT} | gawk -F'' ' { V = $2 R = $0 } END { for ( i = 1; i <= n; i++) { if((V > 0) && (V < V)) print R, ((V - V) / V) * 100 else if ((V > V) && (V > 0)) ... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

pl help me to Optimize the given code

Pl help to me to write the below code in a simple way ... i suupose to use this code 3 to 4 places in my makefile(gnu) .. **************************************** @for i in $(LIST_A); do \ for j in $(LIST_B); do\ if ;then\ echo "Need to sign"\ echo "List A = $$i , List B =$$j"\ ... (2 Replies)
Discussion started by: pk_arun
2 Replies

6. Shell Programming and Scripting

can we optimize this command

can we optimize this command ? sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i1.txt; as here i am using two files ...its overhead..can we optimise to use only 1 file sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i.txt; keeping them same but it... (9 Replies)
Discussion started by: crackthehit007
9 Replies

7. Shell Programming and Scripting

optimize for file reading

HI, I am new to unix . I don't know it is proper place to put my doubt or not .In my requirement , I am reading a file data and validating each fields and one record having 11 fields and each field having specific length.so when i read the file and validation the 11 fields. It taking 3-4 min per... (2 Replies)
Discussion started by: julirani
2 Replies

8. AIX

How to optimize our tape backups ?

Hi, I am currently looking at how we can optimize and speed up our backups here. I am just a beginner operator and our system admin hardly knows anything (long term interim). There is this particular TAR backup of DB backups that for a 10.5Gb amount of files, it takes 5 hours to do the backup on... (6 Replies)
Discussion started by: Browser_ice
6 Replies

9. UNIX for Dummies Questions & Answers

Can we optimize this simple script ?

Hi All , I am just a new bie in Unix/Linux . With help of tips from 'here and there' , I just created a simple script to 1. declare one array and some global variables 2. read the schema names from user (user input) and want2proceed flag 3. if user want to proceed , keep reading user... (8 Replies)
Discussion started by: rajavu
8 Replies

10. Shell Programming and Scripting

optimize the script

Hi, I have this following script below. Its searching a log file for 2 string and if found then write the strings to success.txt and If not found write strings to failed.txt . if one found and not other...then write found to success.txt and not found to failed.txt. I want to optimize this... (3 Replies)
Discussion started by: amitrajvarma
3 Replies
Login or Register to Ask a Question