parse arguments in bash

There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses things by hand:

#!/bin/sh

while echo $1 | grep -q ^-; do
    eval $( echo $1 | sed 's/^-//' )=$2
    shift
    shift
done

echo host = $host
echo user = $user
echo pass = $pass
echo args = $@

A sample run looks like:

$ ./a.sh -host foo -user me -pass secret some args
host = foo
user = me
pass = secret
args = some args

转自: http://stackoverflow.com/questions/4882349/parsing-shell-script-arguments
posted @ 2016-05-18 09:37  圆旭  阅读(205)  评论(0编辑  收藏  举报