如何在 shell 中比较 2 个字符串?

发布于 2024-09-02 23:21:24 字数 181 浏览 5 评论 0原文

我希望用户在命令行输入一些内容 -l 或 -e。 所以例如 $./report.sh -e 我想要一个 if 语句来分解他们做出的任何决定,所以我已经尝试过......

if [$1=="-e"]; echo "-e"; else; echo "-l"; fi

显然不起作用 谢谢

I want the user to input something at the command line either -l or -e.
so e.g. $./report.sh -e
I want an if statement to split up whatever decision they make so i have tried...

if [$1=="-e"]; echo "-e"; else; echo "-l"; fi

obviously doesn't work though
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乜一 2024-09-09 23:21:24

我使用:

if [[ "$1" == "-e" ]]; then
    echo "-e"
else
    echo "-l";
fi

但是,为了解析参数,getopts 可能会让您的生活更轻松:

while getopts "el" OPTION
do
     case $OPTION in
         e)
             echo "-e"
             ;;
         l)
             echo "-l"
             ;;
     esac
done

I use:

if [[ "$1" == "-e" ]]; then
    echo "-e"
else
    echo "-l";
fi

However, for parsing arguments, getopts might make your life easier:

while getopts "el" OPTION
do
     case $OPTION in
         e)
             echo "-e"
             ;;
         l)
             echo "-l"
             ;;
     esac
done
帅哥哥的热头脑 2024-09-09 23:21:24

如果您希望将所有内容都写在一行上(通常这会导致阅读困难):

if [ "$1" = "-e" ]; then echo "-e"; else echo "-l"; fi

If you want it all on one line (usually it makes it hard to read):

if [ "$1" = "-e" ]; then echo "-e"; else echo "-l"; fi
没有心的人 2024-09-09 23:21:24

方括号和方括号内的内容之间需要有空格。另外,只需使用一个 =。您还需要一个then

if [ $1 = "-e" ]
then
   echo "-e"
else
   echo "-l"
fi

然而,-e 特有的问题是它在 echo 中具有特殊含义,因此您不太可能得到任何返回结果。如果您尝试 echo -e,您将看不到任何打印结果,而 echo -decho -f 则执行您所期望的操作。在其旁边放置一个空格,或将其括在括号中,或使用其他方式使其在发送到 echo 时不完全是 -e

You need spaces between the square brackets and what goes inside them. Also, just use a single =. You also need a then.

if [ $1 = "-e" ]
then
   echo "-e"
else
   echo "-l"
fi

The problem specific to -e however is that it has a special meaning in echo, so you are unlikely to get anything back. If you try echo -e you'll see nothing print out, while echo -d and echo -f do what you would expect. Put a space next to it, or enclose it in brackets, or have some other way of making it not exactly -e when sending to echo.

悲念泪 2024-09-09 23:21:24

如果您只想打印用户提交的参数,则只需使用 echo "$1" 即可。如果您想在用户未提交任何内容的情况下回退到默认值,可以使用 echo "${1:--l}:- 是默认值的 Bash 语法)但是,如果您想要真正强大且灵活的参数处理,您可以查看 getopt

params=$(getopt --options f:v --longoptions foo:,verbose --name "my_script.sh" -- "$@")

if [ $? -ne 0 ]
then
    echo "getopt failed"
    exit 1
fi

eval set -- "$params"

while true
do
    case $1 in
        -f|--foo)
            foobar="$2"
            shift 2
            ;;
        -v|--verbose)
            verbose='--verbose'
            shift
            ;;
        --)
            while [ -n "$3" ]
            do
                targets[${#targets[*]}]="$2"
                shift
            done
            source_dir=$(readlink -fn -- "$2")
            shift 2
            break
            ;;
        *)
            echo "Unhandled parameter $1"
            exit 1
            ;;
    esac
done

if [ $# -ne 0 ]
then
    error "Extraneous parameters." "$help_info" $EX_USAGE
fi

If you just want to print which parameter the user has submitted, you can simply use echo "$1". If you want to fall back to a default value if the user hasn't submitted anything, you can use echo "${1:--l} (:- is the Bash syntax for default values). However, if you want really powerful and flexible argument handling, you could look into getopt:

params=$(getopt --options f:v --longoptions foo:,verbose --name "my_script.sh" -- "$@")

if [ $? -ne 0 ]
then
    echo "getopt failed"
    exit 1
fi

eval set -- "$params"

while true
do
    case $1 in
        -f|--foo)
            foobar="$2"
            shift 2
            ;;
        -v|--verbose)
            verbose='--verbose'
            shift
            ;;
        --)
            while [ -n "$3" ]
            do
                targets[${#targets[*]}]="$2"
                shift
            done
            source_dir=$(readlink -fn -- "$2")
            shift 2
            break
            ;;
        *)
            echo "Unhandled parameter $1"
            exit 1
            ;;
    esac
done

if [ $# -ne 0 ]
then
    error "Extraneous parameters." "$help_info" $EX_USAGE
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文