【问题标题】:Comparing input with a string in Shell Script [duplicate]将输入与 Shell 脚本中的字符串进行比较 [重复]
【发布时间】:2016-03-12 13:31:20
【问题描述】:

您好,我正在制作一个带参数的 sh 脚本。 我需要用户插入 1 个参数并使用它来运行 scrapy istance。

#!/bin/sh
# sh yahooscraper.sh 

if [ -z "$1"]
then
    echo "ERROR you must enter one arg related to the Yahoo URL DB "
else
    if ["$1" = "-h"]
    then
        echo "msg"
    else
    echo "Reading from $1 database "
    cd yahooquestionscraper/yahooscraper/yahooscraper/spiders
    scrapy crawl yahoo -o yahoo-url-file.json -a database_name=$1
    fi
fi

此脚本在第一种情况下有效,当用户不选择任何参数时,在第二种情况下。但不要在 if ["$1" = "-h"] 条件下工作。

【问题讨论】:

标签: macos shell if-statement command-line sh


【解决方案1】:

test ([) 命令后需要一个空格(] 结束参数前需要一个空格):

if [ "$1" = "-h" ]

在编写 shell 脚本时,值得用http://shellcheck.net 来检查它们。

【讨论】:

    猜你喜欢
    • 2017-01-08
    • 2021-03-08
    • 2023-03-10
    • 2021-07-10
    • 1970-01-01
    • 2011-10-19
    • 2012-11-27
    • 1970-01-01
    • 2019-08-20
    相关资源
    最近更新 更多