geudrik Posted December 28, 2011 Share Posted December 28, 2011 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 https://forums.phpfreaks.com/topic/253981-bash-ifelseelif-with-getopts-question/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.