mardi 13 décembre 2016

Bash - How to make a program continue on with an if statement rather than having to duplicate code?

I swear I'm hear everyday, sorry if I am starting to become a nuisance, anyways here's some code example:

e.g. Program "Ask Name"

if [ $0 -eq 0 ] || [ $0 -eq $1 ]
then
      if [ $# -eq 0 ]
      then
           echo "Sir I insist, I must know your name"
           read $1
      fi   
      #[the long code for if the number of arguments =1 goes here!!]
      #the long code isn't important just imagine some fancy code here
      #fancy code
      #more fancy code
fi

Basically I just want it so that after the if statement the user can somehow amend the number of arguments and then continue on with the rest of the script.

At the moment the user just constantly has to input something and it never stops, most likely because the '$1' as a variable doesn't work.

Although the main reason I am asking this is to find out if there is a way to get away from the if statement and carry on with the program EVEN IF the argument entered meets specific criteria.

another example of what I mean if you don't understand me:

#takes name as argument
if [ $1 == "dave" ]
then
    echo "I don't like people called Dave, give me another name"
    [SOMETHING HERE THAT MAKES $1 THIS INPUT, NOT SURE ABOUT 'read $1']
else 
    echo "Your name is cool, come inside!"
fi

The example is a bit bad but I basically want it so that from the echo message the user will be able to continue with the code despite being called Dave.

As always, I appreciate any advice.

Aucun commentaire:

Enregistrer un commentaire