parse arguments in bash

时间:2023-03-09 04:16:18
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://*.com/questions/4882349/parsing-shell-script-arguments