simplify/combine if statements would be nice


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simplify/combine if statements would be nice
# 1  
Old 07-21-2011
simplify/combine if statements would be nice

below is something i inherited:

Code:
if [ "$QUEUE" = "normal" ] && [ "$NCPU" = "2" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m quadcore"
fi

if [ "$QUEUE" = "normal" ] && [ "$NCPU" = "4" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m quadcore"
fi

if [ "$QUEUE" = "normal" ] && [ "$NCPU" = "8" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m octocore1"
fi

below is what i changed it to:

Code:
if [ "$QUEUE" = "normal" ] && [ "$NCPU" = "2" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m quadcore"
elif [ "$QUEUE" = "normal" ] && [ "$NCPU" = "4" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m quadcore"
elif [ "$QUEUE" = "normal" ] && [ "$NCPU" = "8" ] && [ "$MEMARC" = "SMP" ]; then
        HOST_SELECT="-m octocore1"
fi

but something tells me i should be able combine the first if and elif into a single line by doing some sort of "or" thing with the 2 and 4 NCPU condition. or perhaps there's something to simplify it further? any suggestions would be appreciated.
# 2  
Old 07-21-2011
Code:
HOST_SELECT="-m quadcore"
if [ "$NCPU" = 8 ]; then
  HOST_SELECT="-m octocore1"
fi

But then you should check $QUEUE and $MEMARC.
# 3  
Old 07-21-2011
I would go with something like this:

Code:
if [ "$QUEUE" = normal ] && [ "$MEMARC" = SMP ]; then
  case $NCPU in
    ( 2|4 ) HOST_SELECT='-m quadcore'   ;;
    (  8  ) HOST_SELECT='-m octocore1'  ;;
  esac
fi

This User Gave Thanks to radoulov 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

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

2. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

3. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

4. UNIX for Dummies Questions & Answers

Struggling to combine two Greps statements

Greetings! I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values. When running each statement separately, I get... (4 Replies)
Discussion started by: alan
4 Replies

5. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies

6. Shell Programming and Scripting

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

7. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies

8. Shell Programming and Scripting

Help to simplify with awk

Hi, Need your help guys. I'm trying to tweak my current shell-script to make it run faster. I think a part of the code that takes too long is the splitting of the 1st field of my CSV raw-file to date-time. Below is the 1st column of my CSV file: $ awk -F"," {'print $1'} temp-*|head... (5 Replies)
Discussion started by: daytripper1021
5 Replies

9. Shell Programming and Scripting

How can I simplify the script

Hi all, How can I simplify following script, Logic is to find two strings (strings are case sensitive) from a file. if ; then if ; then Group=`echo $1_hostname` fi fi Please help me on this. Regards Sudhish s. kumar (8 Replies)
Discussion started by: sudhish
8 Replies

10. Programming

nice command and nice() system call

Hi I want to implement the nice command in the shell that I am building. I came to know that there is a corresponding nice() system call for the same. But since I will be forking different processes to run different commands typed on the command prompt, is there any way I can make a command... (2 Replies)
Discussion started by: tejbuch
2 Replies
Login or Register to Ask a Question