There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses things by hand: #!/bin/shwhileecho$1| grep -q ^-;doeval$(echo$1| sed's/^-//')=$2shiftshiftdoneechohost =$hostechouser =$userechopass =$passechoargs =$@ A sample run look...
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 = $...
Have you ever passed several parameters to a command like this:ls -lah, and thought “I wish my bash scripts could parse command line parameters like that.” Allow me to introduce you to a bash function named getopts. Reader, meet getopts; getopts, meet reader. The functiongetoptsiterates ...
g_default="default_g_value" h_default="default_h_value" i_default="default_i_value" j_default="default_j_value" k_default="default_k_value" l_default="default_l_value" Parse command-line arguments while getopts ":ud🅰️b:c:d:e:f:g:h:i:j:k:l:" opt; do case $opt in u...
echo "The non option arguments are:" $@ fi EOF chmod +x /tmp/demo-equals-separated.sh /tmp/demo-equals-separated.sh -e=log -s=/var/log pos3 pos4 其中${i#*=}用于删除参数$i从左边开始匹配的第一个=及其左边的所有字符。 复制粘贴上述代码块的输出: ...
Parse option arguments. 可以看到,man getopts 提示找不到 getopts 命令的说明,而 help getopts 打印了它的说明。 另外,有一个getopt外部命令也可以解析命令选项,名称比getopts少了一个s,用法也有所差异,不要把这两个命令搞混了。 getopts optstring name [args] ...
Input argument is 'test-value' $ ./parse-long-command-line-args.sh -g test-value Processing 'gamma' option. Input argument is 'test-value' Copy 4. Conclusion In this tutorial, we discussed two methods we can use to parse Linux command-line arguments. First, we discussed bash‘s built-...
与其他参数解析库相比较,如 shflags, argsparse 或bash-modules/arguments,你将看到 Argbash 强大的简单性。如果出现了严重的错误,你需要快速修复解析功能中的一个故障,Argbash 也允许你这样做。 由于你很有可能是 Fedora 用户,因此你可以享受从官方仓库安装命令行 Argbash 的便利。不过,也有一个在线解析代码生成器...
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> `par1' ...
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> `par1' ...