[英]Passing argument to KSH script with spaces
I have a need to pass string of words to a KSH script as follows我需要将字串传递给 KSH 脚本,如下所示
$ script.sh -p "a b c"
I am expecting to see the following我期待看到以下内容
PARMS a b c
But when I try to print out the value of $PARMS, only the first word word that is "a" prints out但是当我尝试打印出 $PARMS 的值时,只有“a”的第一个单词会打印出来
#!/bin/ksh
ARGS=`getopt p: $*`
set -- $ARGS
for i
do
case "$i"
in
-p) PARMS=$2; shift; shift;;
--) shift; break;;
esac
done
echo "PARMS" "${PARMS}"
This is an example of getopts usage in ksh.这是 ksh 中 getopts 用法的一个示例。
#!/bin/ksh
DEFAULT_SITE_NAME="a.b.c.d"
SITE_NAME=$DEFAULT_SITE_NAME
USAGE="[-author?Andre Gelinas]"
USAGE+="[-copyright?2020]"
USAGE+="[+NAME?getopts.sh --- Example of getopts]"
USAGE+="[+DESCRIPTION?Example of getopts usage in ksh.]"
USAGE+="[s:site]:?[site:=$DEFAULT_SITE_NAME?Site name.]"
USAGE+="[p:param]:[param?Parameters to test.]"
USAGE+=$'[+SEE ALSO?\aMAN Page\a(1)]'
while getopts "$USAGE" optchar ; do
case $optchar in
p) PARAM_TO_PRINT=$OPTARG ;;
s) SITE_NAME=$OPTARG ;;
esac
done
print "Paramters [p] : "$PARAM_TO_PRINT
print "Site [s] : "$SITE_NAME
It supports short (-p) and long (--param=) type of option.它支持短 (-p) 和长 (--param=) 类型的选项。 The -s is there only as an example of optional option with default value.
-s 仅作为具有默认值的可选选项的示例。 It supports also --help and --man for the usage.
它还支持 --help 和 --man 的用法。
Example :例子 :
$ ./getopts.sh --param="a b c" --site=t.t.t.t
Paramters [p] : a b c
Site [s] : t.t.t.t
$ ./getopts.sh -p "a b c"
Paramters [p] : a b c
Site [s] : a.b.c.d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.