Is there a more elegant


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a more elegant
# 1  
Old 10-29-2015
Is there a more elegant

Hi,

I am wanting to test that an argument passed is one of tstt11/2/3, tstq11/2/3 or tstp11/2/3 and I am currently doing it as below.

Just wanting to know if there is a more 'elegant' way of doing this :-)

Code:
arg_inst=`echo $1 | awk '{ print tolower($1) }'`

if   [[ ${arg_inst} == "tstt11" || ${arg_inst} == "tstq11" || ${arg_inst} == "tstp11" ]] then
   echo "- arg_inst = $arg_inst ---> NODE 1"
elif [[ ${arg_inst} == "tstt12" || ${arg_inst} == "tstq12" || ${arg_inst} == "tstp12" ]] then
   echo "- arg_inst = $arg_inst ---> NODE 2"
elif [[ ${arg_inst} == "tstt13" || ${arg_inst} == "tstq13" || ${arg_inst} == "tstp13" ]] then
   echo "- arg_inst = $arg_inst ---> NODE 3"
else
   echo "- arg_inst = $arg_inst is not a valid instance that we required !!!"
fi

# 2  
Old 10-29-2015
How about (recent shell (bash, ksh) required):
Code:
if   [[ ${1,,} == tst[tqp]1[123] ]]
  then  echo "- arg_inst = $1 ---> NODE ${1#${1%?}}"
  else  echo "- arg_inst = $1 is not a valid instance that we required !!!"
fi

# 3  
Old 10-29-2015
General shell approach (should work in any POSIX compliant shell):
Code:
node_nr=${1#[tT][sS][tT][tTpPqQ]1}
case $node_nr in
  [123])
    echo "- arg_inst = $1 ---> NODE $node_nr"
    ;;
  *)
    echo "- arg_inst = $1 is not a valid instance that we required !!!"
esac

or alternatively:
Code:
pat=[tT][sS][tT][tTpPqQ]1
case $1 in
  $pat[123])
    echo "- arg_inst = $1 ---> NODE ${1#$pat}"
    ;;
  *)
    echo "- arg_inst = $1 is not a valid instance that we required !!!"
esac


Last edited by Scrutinizer; 10-29-2015 at 12:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store the last 10 strings. A way to make it elegant?

Hello, Please find an ugly code. bouuu. It shall work onto RAM only to update from the last 10 strings from $URLTO. I shall read first : "$HOME/.fvwmoscfg/fvwmclipboardmplayerplayurl10.ini" The code works but it is very ugly. How could it be made elegant please? Thank you ... (2 Replies)
Discussion started by: french00b
2 Replies

2. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

3. Solaris

Elegant Solutions to kill telnet/ssh session

We have a generic user account "user1" setup on Solaris 8 that is used by an application. I dont want users to telnet/ssh using this account. Instead if they want to gain access, they must su or sudo to this after logging in with their own ID. My earlier attempts to accomplish this by disabling... (7 Replies)
Discussion started by: boshyd
7 Replies

4. Shell Programming and Scripting

Elegant gunzip of tar Contents

I am faced with a situation where I have directories of gunzipped contents bundled into a tar file. It might look something like this. x coop/batch/bin/ha90x20.gz, 632641 bytes, 1236 tape blocks x coop/batch/icm/HA90X20.icm.gz, 1821 bytes, 4 tape blocks x coop/batch/aeenv.gz, 4117 bytes, 9 tape... (2 Replies)
Discussion started by: scotbuff
2 Replies

5. Shell Programming and Scripting

Please make this code elegant.

Hi All, Following is the part of my script.It does contain many for loops and is not elegant. Please feel free to suggest any changes to make this elegant. Thanks! nua7 for i in `ls $CATALINA_HOME/shared/lib/*.jar`; do LOCALCLASSPATH="$LOCALCLASSPATH:$i" done for i in... (3 Replies)
Discussion started by: nua7
3 Replies

6. Shell Programming and Scripting

more elegant way for conditional statement needed

Hi all, I have a script which gets its input from a text file (file.txt) and processes each line within a loop. I have a counter which increases by one and I want something to happen every 7th, 14th, 21st, etc. line read. Currently the code looks and works perfectly like this: ... (3 Replies)
Discussion started by: candyflip2000
3 Replies
Login or Register to Ask a Question