你是这个意思吗?
#!/bin/sh
# usage: sh tes.sh username password addr
# Define foo
# [0]foo [1]user [2]passwd [3]addr
foo () {
user=$1
passwd=$2
addr=$3
echo ${user} ${passwd} ${addr}
}
# Call foo
foo $1 $2 $3
结果:
$ sh test.sh username password address not a data
username password address
您的问题也已经在这里得到解答:
将参数传递给 Bash 函数
显然上面的答案与问题无关,那么这个怎么样?
#!/bin/sh
IN="user=user pass=passwd other= another=what?"
arr=$(echo $IN | tr " " "\n")
for x in $arr
do
IFS==
set $x
[ $1 = "another" ] && echo "another = ${2}"
[ $1 = "pass" ] && echo "password = ${2}"
[ $1 = "other" ] && echo "other = ${2}"
[ $1 = "user" ] && echo "username = ${2}"
done
结果:
$ sh test.sh
username = user
password = passwd
other =
another = what?