i'm writing bash script automate job, , in meantime practice bash. i'm writing script automate git cloning , updating stuff. script have 3 options (-g, -f, -h). when types -h should display message (which have written omitted below), when writes -g, should accept @ least 1 argument, can accept second 1 optional. which, first 1 url of repository clone , second 1 directory, clone it. on other hand, if types -f, script should 1 argument, file name. want read file, line line, , git cloning , updating each git url inside file.
if run script following error message each option, , if call without option, or valid option followed other argument, still doesn't anything, returns:
./gitupdate.sh: option requires argument -- g
i guess doesn't use $2 , $3 somehow in code. again, when pass -h, has call function , display message, doesn't need use other argument.
i guess problem because have wrong @ bottom, use getopts option name specified user. in code assume option first argument, $1, , second 1 $2 url or filename, according option specified, , $3 optional 1 works -g option.
below can find code:
#!/bin/bash declare default_directory=$home declare action declare src function clone_repo() { if [ -n "$2" ]; if [ "$(ls -a "$2" 2>/dev/null)" ]; cd "$2" else git clone "$1" "$2" fi else git clone "$1" #todo: directory name created cloning # , cd it. fi git remote add upstream "$1" git fetch upstream } function read_repos_from_file() { if [ -f "$1" ]; while read -r line; clone_repo "$line" "$2" done < "$1" else echo -e "error: specified file not found." exit 1 fi } while getopts "f:h:r" option case "${option}" in f) action=read_repos_from_file; src="$optarg";; g) action=clone_repo; src="$optarg";; h) ; exit 1 ;; esac done shift $((optind-1)) [ -z "$action" ] && ( help; exit 1 )
if me, glad.
edit: corrected typo mistakes in above code. , updated error message. script doesn't work correctly. guess need change in code, make something, , $2 , $3 arguments. doesn't display message, when -h option passed, , there call function have created. maybe need somehow change getopts part.
edit 2: made advised changes, , changed above code.
git()
beginning of function definition (the keyword function
optional when function name followed parentheses). if want call function git()
need define first, , call without parentheses:
function git() { # stuff } git
it's not practice create functions same name existing binaries, though. in case should call git clone
line read file:
while read -r line; git clone "$line" done < "${file}"
edit: updated, since question changed significantly.
your argument processing is … weird, frank. when you're using option parser, shouldn't work around way option parser works. "g:"
means option -g
1 argument. don't try make option more 1 argument, 1 of them being optional on top of it. if need additional (optional) argument output directory, make either option (e.g. "d:"
) or non-option argument.
i'd suggest change option-processing this:
while getopts "f:g:h" option; case "$option" in f) action=file; src="$optarg";; g) action=repo; src="$optarg";; h) help; exit 1;; esac done shift $((optind-1)) [ -z "$action" ] && ( help; exit 1 )
after "$@" holds non-option arguments (in case optional output directory), call functions this:
$action $src "$@"
with functions simplified this:
function repo() { if [ -n "$2" ]; if [ "$(ls -a "$2" 2>/dev/null)" ]; cd "$2" else git clone "$1" "$2" fi else git clone "$1" fi ... } function file() { if [ -f "$1" ]; while read -r line; repo "$line" "$2" done < "$1" else echo "error: specified file not found." exit 1 fi }
on more general note, should make names more self-explanatory. better names functions cloning repository or reading repositories file clone_repo
, read_repos_from_file
respectively. also, -c
or -r
better mnemonics option triggers cloning of repository.
Comments
Post a Comment