LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script error (http://www.linuxquestions.org/questions/programming-9/bash-script-error-487050/)

noir911 09-26-2006 05:00 AM

bash script error
 
I am trying to write a bash script which will generate ssh key for the user and along the way I am doing 2 sanity checks.

0 if user doesn't put rsa or dsa as command line argument $1 it will give an error

0 if user doesn't provide a file name as $2 it will give an error.

But it is giving me an error. Here's my code -

Code:

    1  #!/bin/bash
    2
    4
    14
    15  usage()
    16  {
    17              echo
    18              echo "Usage: <keytype> <filename>"
    19              echo
    20              exit
    21  }
    22
    23  if [ -z "$1" ]
    24    elif
    25    [ -z  "$2" ]; then
    26      usage
    27  fi
    28
    29  if [ -z "$1" != "rsa" || -z "$1" != "dsa" ]; then
    30      echo "you can either choose rsa or dsa key"
    31      exit 1
    32  fi
    33
    34  ssh-keygen -t $1 -f $2 -N "" -C "$(hostname)"

keygen.sh: line 24: syntax error near unexpected token `elif'
keygen.sh: line 24: ` elif '

acid_kewpie 09-26-2006 05:10 AM

you need a "then" before you can use an "else" or "elif" against it. that if block starting line 23 is totally incorrect though.

Code:

if [ $# -lt 2 ]
then
  usage
  exit 1
fi


/bin/bash 09-26-2006 09:19 PM

I always try to use either a case or getopts to work with commandline arguments. It makes your code "flow" better.
Code:

#!/bin/bash
usage() {
echo
echo "Usage: <keytype> <filename>"
echo
exit
}

case "$1" in
  rsa    ) FILE="$2";
          ;;
  dsa    ) FILE="$2";
          ;;
    *    ) usage;;
esac

[ ! -f "$FILE" ] && usage && exit
# If we make it here then our commandline arguments are OK
echo '$1 = '"$1  --  "'$2 = '"$2"

# This would also work
case "$1" in
  rsa | dsa  ) FILE="$2";
            ;;
      *      ) usage;;
esac


konsolebox 09-26-2006 10:02 PM

same idea as /bin/bash using case but if you only want to use ifs:

Code:

#!/bin/bash

usage() {
        echo
        echo "Usage: <keytype> <filename>"
        echo
        exit
}

if [ "$1" != "rsa" ] && [ "$1" != "dsa" ]; then
        usage
fi

if [ -z "$2" ] || [ ! -f "$2" ]; then
        usage
fi

ssh-keygen -t $1 -f $2 -N "" -C "$(hostname)"

i also suggest that you the rsa|dsa options in the usage
Code:

echo "Usage: rsa|dsa filename"

OR

echo "Usage: <keytype> <filename>"
echo " keytype can be rsa or dsa"


/bin/bash 09-27-2006 07:52 AM

Using getopts makes your code look more professional IMO.
Code:

#!/bin/bash
usage() {
echo
echo "Usage:              <-d|-r> <filename>"
echo "Options:            -d use dsa key."
echo "                    -r use rsa key."
echo "                    filename where the key will be saved."
exit
}

while getopts ":d:r:" Option
do
  case $Option in
    d    ) OPT1="dsa";OPT2=$OPTARG;;
    r    ) OPT1="rsa";OPT2=$OPTARG;;
    *    ) usage;;
  esac
done
shift $(($OPTIND - 1))

[ -z "$OPT2" ] && usage

ssh-keygen -t $OPT1 -f $OPT2 -N "" -C "$(hostname)"



All times are GMT -5. The time now is 06:08 AM.