Chinaunix

标题: shell脚本参数位置 [打印本页]

作者: studyboy_3w    时间: 2010-01-12 09:38
标题: shell脚本参数位置
我想做一个这样的模板

#./test_script -n your_name -s 100 -p -f your_file
#./test_script  -s 100  -f your_file -n your_name -p
这2个命令是一样的, 只要用-*来区别参数, 参数位置可以任意调动 不影响结果。

所以如何得到参数并对变量赋值呢
假设 -n 对应的变量是 Nn
-s -> Ss
-p -> Pp
-f -> Ff
作者: yazi0127    时间: 2010-01-12 09:50
while getopts n:s:p opt
do
    case $opt in
        n) ...
           
           ;;
        s) ...
           
           ;;
        p) ...
           ;;
        ?) exit
           ;;
    esac
done
作者: ubuntuer    时间: 2010-01-12 10:57
原帖由 yazi0127 于 2010-1-12 09:50 发表
while getopts n:s:p opt
do
    case $opt in
        n) ...
           
           ;;
        s) ...
           
           ;;
        p) ...
           ;;
        ?) exit
           ; ...

正解
作者: studyboy_3w    时间: 2010-01-12 17:51
谢谢两位高人了

假设有2个参数是不要加arg的呢 如: ./test_script -n your_name -s 100 -p  -d -f your_file
是否不能这么加参数了 是否应该是  ./test_script -n your_name -s 100 -pd -f your_file




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2