Jump to content

Bash if/else/elif with getopts Question


geudrik

Recommended Posts

I'm quite new to bash, but trying to drag my way up the latter one rung at a time here.

 

I've managed to write the beginnings of a script that's going to help me out with rsync backups (well, it's more an exercise for my sef) but I can't seem to get my statements to work properly...

 

Here's the code I've written

#!/bin/sh
if ( ! getopts "l:r:u:p:df:h" opt); then

    echo "Usage: `basename $0` options [-l /path] [-r /path] [-u username] [-p password] [-d] [-f /path] -h for Help";
    exit 1;

elif (getopts "h" opt) then

    echo "\nHelp page for `basename $0`"
    echo "The following parameters are accepted"
    echo ""
    echo ""
    echo "[-l]      Specifies a Local path to folder to backup."
    echo "[-r]      Specifies a path on the Remote machine to back up to."
    echo "[-u]      The Username used for our SSH Connection."
    echo "[-p]      The password used for our SSH Connection."
    echo "          * Public Key Authentication will be tried first."
    echo "[-d]      Tells `basename $0` to preform a dry-run. ie: dont transfer data"
    echo "[-f]      The local path for our Filter list."
    echo "          * List of files/folders, one line each, that rsync will ignore"
    echo "\n"
    exit 0; # Script technically completes successfully.

else

    # Don't include -h here 
    while getops ":l:r:u:p:df:" opt; do
        case $opt in
            
        l)  
            echo "-l switch thrown. local dir to backup: $OPTARG" >&2
        ;;  
            
            
        r)  
            echo "-r switch thrown. remote dir for backup: $OPTARG" >&2
        ;;  


        u)  
            echo "-u switch thrown. ssh username: $OPTARG" >&2
        ;;  

    
        p)  
            echo "-p switch thrown. ssh password: $OPTARG" >&2
        ;;  


        d)  
            echo "-d switch thrown. This is a dryrun." >&2
        ;;  


        f)  
            echo "-f switch thrown. Path to filterlist: $OPTARG" >&2
        ;;  


        # Invalid Parameter
        \?) 
            echo "Invalid options passed" >&2
            exit 1;
        ;;  

        esac
    done

fi

 

When I run

Fak:backup-script geudrik$ ./BACKUP_MasterScript.sh -f

 

I get the following error:

./BACKUP_MasterScript.sh: option requires an argument -- f
./BACKUP_MasterScript.sh: illegal option -- f

Help page for BACKUP_MasterScript.sh
The following parameters are accepted


[-l]		Specifies a Local path to folder to backup.
[-r]		Specifies a path on the Remote machine to back up to.
[-u] 		The Username used for our SSH Connection.
[-p]		The password used for our SSH Connection.
		* Public Key Authentication will be tried first.
[-d]		Tells BACKUP_MasterScript.sh to preform a dry-run. ie: dont transfer data
[-f]		The local path for our Filter list.
		* List of files/folders, one line each, that rsync will ignore

Assuming I'm not totally derping here, I feel like I should only be getting my switch thrown string.  Why am I not, but instead getting the help contents?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.