从脚本不期望的输出 [英] not expected output from script

查看:109
本文介绍了从脚本不期望的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行该code与参数如 ./ getopts的-a 它打印喜-a虽然进出料将嗨你好。任何想法什么问题呢?

 #!/斌/庆典而getopts的一个:名称

        案例$名称
          一)aopt = $ OPTARG ;;
          *)回声无效ARG;;
        ESAC
DONE如果[! -z $ aopt]];然后
    回声$ aopt
科幻移$(($ OPTIND - 1))#exit 0


解决方案

它看起来像被捆绑到你的 -a 选项的唯一事情就是。如果你只需要拉你好 -a一起喜你可以从$ 3命令行拉。

 ,而getopts的A:名称;做
            案例$名称
                一)aopt = $ OPTARG ;;
                *)回声无效ARG;;
            ESAC
        DONE
        如果[! -z $ aopt]];然后
            回声$ aopt $ 3
        科幻
        移$(($ OPTIND - 1))

请注意,在你身边需要重复一下。我会建议采取一看的http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt关于getopt的一个漂亮的快速阅读。

when i run this code with an argument e.g ./getopts -a it prints "hi -a" though the expected out would be "hi hello". Any ideas whats going wrong?

 #!/bin/bash

while getopts a:name
do
        case $name in
          a)aopt=$OPTARG;;
          *)echo "Invalid arg";;
        esac
done

if [[ ! -z $aopt ]]; then
    echo $aopt
fi

shift $(($OPTIND - 1))

#exit 0

解决方案

It looks like the only thing being tied to your -a option is hi. If you simply need to pull hello along with -a hi you can just pull it from the command line with $3.

while getopts "a:" name; do
            case $name in
                a)aopt=$OPTARG;;
                *)echo "Invalid arg";;
            esac
        done
        if [[ ! -z $aopt ]]; then
            echo "$aopt $3"
        fi
        shift $(($OPTIND - 1))

Please note the "" around what you need to echo. I would suggest taking a look at http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt for a nice quick read on getopt.

这篇关于从脚本不期望的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆