Solved

Compare parameter enter by user in shell script

Posted on 2014-09-23
14
145 Views
Last Modified: 2014-11-16
Hi,

User enters parameter say “:foo_test”, than I need to execute a command else if it enters “NA” than I need to execute another command and if value is blank than need to execute third command, here user can enter any value for first option, means parameter starting with colon, so user can enter “:soo_fast”or “:foo_test” or “:jack_jill” etc so this way user can enter around 70 such fixed option, how can I put if else condition in shell script for this request, any help in this regards will be highly appreciated.

Thanks,
Jhoom
0
Comment
Question by:saharey
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40340941
What is the operating system for this question?
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 286 total points
ID: 40341232
(I'm assuming some sort of Unix/linux here, or Windows with Cygwin)

If you have lots of possible entries, a case statement is probably your best bet:
echo Enter your response
read ans
case $ans in
    :foo_test) echo Got foo_test
               # run commands here for foo_test
               ;;
    :jack_jill) echo Got jack_jill
               # run commands here for jack_jill
               ;;
    NA)        echo Got NA
               # run commands here for NA
               ;;
    me|you)    echo Got me or you
               # run commands here for me or you
               ;;
    "")        echo Got empty response
               # run commands here for empty answer
               ;;
    *)         echo Got unknown response - $ans
               # run commands here for unknown answer
               #  (e.g. log error and exit)
               ;;
esac

Open in new window

The colons at the start of the first couple of names are part of the answer (they are not special characters as far as the "case" statement is concerned).  Each section ends with a double semi-colon - thats's the "case" statement marker for the end of a section.  The "me|you" entry shows how to use the same section of code for multiple case values.  You can use wild-cards in your entries (* for 0 or more of any character, ? for any single character).
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40341248
If you really want to use "if" staements then you could have something like:
read ans
if [ "$ans" = ":foo_test" ]; then
   echo Do work here for foo_test
elif [ "$ans" = ":jack_jill" ]; then
  echo here for jack_jill
elif [ "$ans" = "NA" ]; then
  echo here for NA
elif [ "$ans" = "" ]; then
  echo empty answer
else
  echo anything else here
fi

Open in new window

(The text strings don't strictly have to have quotes round them, but most people do it and they will allow you to process an answer with a space in it)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 72 total points
ID: 40341891
To test for a parameter "passed" to a script, try something like this:
# myScript.sh
#
param1=$1       #<- Test for first parameter
typeset -l param1=${param1:-NA}   # Default to 'NA' and change it to lower case for ease

case $param1
	:soo_fast) echo "Processing: $param1"
		   #-- do :soo_fast process;;
	:foo_test) echo "Processing: $param1"
		   #-- do :foo_test process
		   ;;
	:jack_jill) echo "Processing: $param1"
		    #-- do :jack_jill process
		    ;;
		na) echo "Processing: $param1"
		    #-- do default process
		    ;;
		 *) echo "!Error, invalid parameter: $param1"
		    exit 1
		    ;;
esac

Open in new window

0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 286 total points
ID: 40342513
@MikeOM_DBA, I'd miss out the typeset line with its ":-NA" bit, otherwise theOP can't check for an empty parameter.
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 142 total points
ID: 40345741
If using bash, you might like to think about using select. You can tell if the user enters nothing, otherwise his choice is returned as a number. You could use if/else but it would be far neater (and easier to maintain) to call a function (e.g. fn33 for choice 33).
I would dummy up an example for you, but before I do, could you please confirm that you are using bash? select is bash-specific.
0
 

Author Comment

by:saharey
ID: 40347442
Thanks to all for quick replies and to probable solution to my query, what I think here is  putting if condition by comparing exact string will make shell script too lengthy and unmanageable over the period of time as there are already more than 70 modules to test, so I thought this will be best solution fit for me.

if [[ $1 == \:* ]]; then
echo "Very Good $1"
elif [[ $1 == NA ]]; then
echo "it is  not applicable $1"

else
echo "happy"
fi

Open in new window


Since each of my modules names will start with colon “:” so decided to use above solution, need experts comments on above solution, I need experts comments on above solution.
0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 142 total points
ID: 40348977
This sample script shows how to use select. To avoid your having an accident with select / paste, I have also attached it
#!/bin/bash
#set -x

main () \
{
  MSG="Please type one of the displayed numbers (Ctrl-D finishes)"
  echo $MSG
  while (true)
  do
    keep_going=false
    select answer in \
      :foo_test \
      NA \
      :soo_fast \
      :jack_jill \
      ;
    do
      
      # Empty answer means choice was illegal
      if [ -z $answer ]
      then
        echo $MSG
        keep_going=true
        break
      fi
      
      # Execute the selected function
      $answer
    done
    
    # Ctrl-D gets here directly, so keep_going will still be false
    $keep_going || break
  done
}
:foo_test () \
{
  echo "You entered the choice for :foo_test"
}

NA () \
{
  echo "You entered the choice for NA"
}

:soo_fast () \
{
  echo "You entered the choice for :soo_fast"
}

:jack_jill () \
{
  echo "You entered the choice for :jack_jill"
}

#Run the mainline
main

Open in new window

script.sh
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 286 total points
ID: 40349484
One problem with the solution in http://#a40347442 is that it checks that the entry starts with a colon, but doesn't check that it is a valid entry.

If you have a file containing all of the valid entries (all of the ones starting with a colon) you coudl add a check with:
    if grep -q "^${1}$" valid_entries_file.lst; then
        process valid entry
    else
        report invalid entry
fi

Open in new window

0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 40349609
The solution in http:#a40348977 does restrict the user to making a valid choice. Perhaps I didn't make that clear - was in  a bit of a rush this morning. Here is some output
20:02:57$ ./script.sh 
Please type one of the displayed numbers (Ctrl-D finishes)
1) :foo_test
2) NA
3) :soo_fast
4) :jack_jill
#? 1
You entered the choice for :foo_test
#? 3
You entered the choice for :soo_fast
#? 2
You entered the choice for NA
#? 4
You entered the choice for :jack_jill
#? 5
Please type one of the displayed numbers (Ctrl-D finishes)
1) :foo_test
2) NA
3) :soo_fast
4) :jack_jill
#? 
1) :foo_test
2) NA
3) :soo_fast
4) :jack_jill
#? 
20:05:40$ 

Open in new window

select itself will re-display the options if you just hit Enter (line 21 above). Line 26 is a Control-D: the script recognizes this because keep_going has not been set true. After the invalid choice on line 15, the script issues a message and forces the menu to be re-displayed.
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 286 total points
ID: 40349636
I was talking about the OP's script (the one with "if [[ $1 == \:* ]]"), not yours!  Select will indeed cehck for valid answers, since it only allows those valid answers to be chosen.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 40351069
No worries Simon, I did realize that ;)
0
 

Author Comment

by:saharey
ID: 40396871
Thanks to all experts who took out their time to answer my query, I really appreciate this help.
But the comments by experts did not fit to my solution, so do I still need to accept the answers from experts and reward the points to them.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40397167
. . . the comments by experts did not fit to my solution
Perhaps you may have not been "clear" in stating your requirements.
You solution from post#a40347442  (as stated by simon3270) doesn't check if it is a valid entry. Therefore, you may have to combine ideas from the several answers to obtain a solid solution.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question