LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-26-2006, 06:00 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
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 '
 
Old 09-26-2006, 06:10 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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
 
Old 09-26-2006, 10:19 PM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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

Last edited by /bin/bash; 09-26-2006 at 10:27 PM.
 
Old 09-26-2006, 11:02 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
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"

Last edited by konsolebox; 09-26-2006 at 11:07 PM.
 
Old 09-27-2006, 08:52 AM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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)"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script error noir911 Programming 5 09-12-2006 07:42 AM
bash script error noir911 Programming 4 07-25-2006 04:58 AM
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 11:01 AM
bash script error near 'else' ? nutthick Linux - Newbie 5 01-23-2005 04:41 PM
bash script error corbis_demon Linux - Newbie 7 09-27-2004 11:41 AM


All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration