Using grep/sed to parse BASH cmdline arguments -
i got script expects 2 args (filename , md5hashval). can extract hex output of md5sum using md5sum test.sh | grep -om1 '^[0-9a-f]*.' reason, same cmd fails when invoked script. whats best way check cmdline arguments passed bash script? here's code looks like:
#!/bin/bash while getopts ":f:s" opt; case $opt in f) filename=`echo $optarg | sed 's/[-a-za-z0-9]*=//'` echo ${filename} ;; s) md5sum=`echo $optarg | grep -om1 '^[0-9a-f]*'` echo $md5sum ;; \?) echo "invalid option: -$optarg" >&2 exit 1 ;; :) echo "option -$optarg requires argument." >&2 exit 1 ;; esac done
since s
alternative requires argument, need place colon after it. should be:
while getopts "f:s:" opt; ...
from getopts
man page:
if character followed colon, alternative expected have argument, should separated white space.
bash grep
No comments:
Post a Comment