`
omygege
  • 浏览: 1345236 次
文章分类
社区版块
存档分类
最新评论

shell 传递选项和参数之 getopt 的用法

阅读更多

#这是我管理编译的一个脚本,主要部分请看 ===================== 之后的部分

#!/bin/bash


make_clean()
{

}

move_bin()
{

}

make_decoder()
{

}

make_arch()
{

}

make_decoder_image()
{

}

build_help()
{
echo "no option chosice"
echo " -a --make arch"
echo " -c --make clean"
echo " -d --make decoder"
echo " -i --make decoder.image"
echo " -m --cp decoder_main.bin to nfsroot"
echo " -w --make fb_welcome.bin "
echo " -h --display this help"
echo "make decoder"
}

build_welcom()
{

}

GETOPTOUT=`getopt acdhimwe:fo "$@"`
SETOUT=`set -- "$GETOPTOUT"`

echo "======="
echo "optout is $GETOPTOUT" #这个变量没有用,只是为查看getopt 命令的输出
echo "setout is $SETOUT" #只为查看set -- 命令的输出
echo "after set $@ "


#================================================
if [ $# == 0 ]; then
build_help
fi

set -- `getopt acdhimwe:fo "$@"` #set -- 重新组织$1 等参数
while [ -n "$1" ]
do
echo "\$1 is $1"
case $1 in
-a)
make_arch
;;
-c)
make_clean
;;
-d)
make_decoder
;;
-h)
build_help
;;
-i)
make_decoder_image
;;
-m)
move_bin
;;
-w)
build_welcom
;;
--)
shift
break
;;
-o)
echo "find -o option"
;;
-f)
echo "find -f option"
;;
-e)
echo "find -e option with param $2"
shift
;;
*)
echo $1
echo "unknow option"
esac
shift
done

count=1
for param in "$@"
do
echo "Paraneter \$$count:$param"
count=$[ $count + 1 ]

done


#=======================

要点:

1. getopt 用法

语法:getopt [options] [--] optstring parameters

例如:getopt ab:cd -a -b he free cat
输出:-a -b he -- free cat
getopt 根据 ab:cd 将选项和参数 -a -b he free cat 解析为如下格式:
-a -b he -- free cat
其中 -- 将选项与非选项参数分开 free 和 cat 就时非选项参数

2. set --

-- Do not change any of the flags; useful in setting $1 to -.

set -- 主要是影响特殊变量$1 $2 等,其实在上面的脚本中就是将$1 $2 等参数变量重新组合

例如:

set -- a b c

shell中的特殊位置变量$1 为a $2 为 b $3 为 c

3.如上脚本为build.sh 用法如下:

./build.sh -a -c -d -e dog

./build.sh -acde dog

上面两个命令执行结果相同。

分享到:
评论

相关推荐

    getopt多参数解析函数具体分析命令行参数解析

    参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,...

    命令行解析函数getopt用法详解

    命令行解析函数getopt使用方法详解,更方便分析命令参数

    linux shell命令行选项与参数用法详解

    问题描述:在linux shell中如何处理tail -n 10 access.log这样的命令行选项?在bash中,可以用以下三种方式来处理命令行参数,每种方式都有自己的应用场景。1,直接处理,依次对$1,$2,…,$n进行解析,分别手工处理;...

    windows 版本 getopt_long

    命令行参数可以分为两类,一类是短选项,一类是长选项,短选项在参数前加一杠"-",长选项在参数前连续加两杠"--",如下表(ls 命令参数)所示,其中-a,-A,-b都表示短选项,--all,--almost-all, --author都表示长...

    linux中getopt的用法

    linux中getopt的用法

    Shell – 通过参数名传递参数值

    而且在调用该shell脚本传递参数时容易产出错误。 Linux常用的命令都可指定参数名和参数值,我们怎样才能给自己的shell脚本也采用参数名和参数值这样的方式来获取参数值呢?下面的例子定义了短参数名和长参数名两种...

    getopt命令 解析命令行选项

    在此命令中,选项部分主要用于影响getopt命令解析选项,其中optstring是需要解析的合法选项的字符串,paramenters通常是一组变量,用于存储从命令行获取的每个选项及参数。 语法格式: getopt [参数] 常用参数: ...

    C++版本Windows命令行getopt参数类

    功能:C++命令行参数解析类。(控制台应用程序中使用)在 Windows 平台下使用 Linux 的 getopt, getopt_long, getopt_long_only 函数...压缩包内包含getopt.h,getopt.c和使用代码示例。在VS2010下编译通过,运行良好。

    Linux Shell脚本通过参数名传递参数值

    Linux常用的命令都可指定参数名和参数值,然而我们怎样才能给自己的shell脚本也采用参数名和参数值这样的方式来获取参数值呢?而不是通过$1,$2这种方式进行获取。下面的例子定义了短参数名和长参数名两种获取参数值...

    Shell脚本通过参数名传递参数的实现代码

    Linux常用的命令都可指定参数名和参数值,然而我们怎样才能给自己的shell脚本也采用参数名和参数值这样的方式来获取参数值呢?而不是通过$1,$2这种方式进行获取。下面的例子定义了短参数名和长参数名两种获取参数值...

    getopt的用法

    getopt被用来解析命令行选项参数。就不用自己写东东处理argv了

    getopt函数用法

    getopt函数用法 很详细 需要的下载

    getopt-php, 用于 命令行 参数处理的PHP库.zip

    getopt-php, 用于 命令行 参数处理的PHP库 GetOpt.PHP GetOpt.PHP 是命令行参数处理的库。 它支持PHP版本 5.4和更高版本。特性同时支持短( ( -v ) ) 和长( 例如。 --version ) 选项选项别名。IE 。选项既

    linux解析命令行选项getopt_long用法解读.docx

    linux解析命令行选项getopt_long用法解读.docx

    getopt.h getopt getopt_long

    win10 vs2019下的 getopt及getopt_long getopt/getopt_long函数是GNU C中的函数

    shell脚本命令行参数简介

    bash shell提供了命令行参数添加在命令后面的数据值)、命令行选项修改命令行为的单字符值)和直接读取键盘输入。 1、命令行参数向shell脚本传递数据的最基本方式是使用命令行参数。 1)读取参数读取输入的参数的...

    GetOpt一个命令行参数解析器

    GetOpt:一个命令行参数解析器

    getopt.h getopt.c getopt_long 文件和函数说明

    getopt.h getopt.c getopt_long 函数说明

    getopt.h getopt.c getopt getopt_long 字符串 命令行

    getopt.h getopt.c getopt getopt_long 字符串 转 命令行 编译通过

    getopt.h和cpp

    提供Linux下的getopt.h和cpp,可以放到windows下的vc直接编译通过

Global site tag (gtag.js) - Google Analytics