在脚本中运行curl时出错(没有这样的文件或目录)

Bra*_*rad 2 bash curl sh

我有以下.sh文件我拼凑了我在互联网上找到的东西.
目标是读取CSV文件中的第二行第二项,并使用它来向elasticsearch发送删除命令.

#!/bin/sh
OLDIFS=$IFS
IFS=,


function quit {
        echo "Quitting Script"
        IFS=$OLDIFS
    exit 1
}
function fileExists {
    if [ ! -f "$1" ]
        then
            echo "File $1 does not exists"
                quit
        fi
echo "File Name:  $1"
}
function work {
        linesToSkip=1
        {
            for ((i=$linesToSkip;i--;)) ;do
                read
            done
                #Read 2nd item of the 2nd line of CSV file to get PROGRAMURL
            read INDEX PROGRAMURL JUNK
                echo "$PROGRAMURL"
                QUERY="curl -XDELETE http://127.0.0.1:9200/cj/_query  -d '{ \"query\" : { \"match\" : { \"PROGRAMURL\" : "$PROGRAMURL" } } }'"
                $("$QUERY")
                #RESPONSE=`$QUERY`
                #echo $RESPONSE

        } < $1

}

fileExists $1
work $1
IFS=$OLDIFS
Run Code Online (Sandbox Code Playgroud)

除了执行curl脚本之外,一切都有效.我用$(),backtics,exec尝试过,我无法让它工作.

以下是运行bash -vx script.sh时的错误:

bash -vx ./deleteExisting.sh catalog.csv
#!/bin/sh
OLDIFS=$IFS
+ OLDIFS='  
'
IFS=,
+ IFS=,


function quit {
    echo "Quitting Script"
    IFS=$OLDIFS
    exit 1
}
function fileExists {
    if [ ! -f "$1" ]
    then
        echo "File $1 does not exists"
        quit
    fi  
echo "File Name:  $1"
}
function work {
    linesToSkip=1
    {
        for ((i=$linesToSkip;i--;)) ;do
            read
        done
        #Read 2nd item of the 2nd line of CSV file to get PROGRAMURL
        read INDEX PROGRAMURL JUNK
        echo "$PROGRAMURL"
        QUERY="curl -XDELETE http://127.0.0.1:9200/cj/_query  -d '{ \"query\" : { \"match\" : { \"PROGRAMURL\" : "$PROGRAMURL" } } }'"
        $("$QUERY")
        #RESPONSE=`$QUERY`
        #echo $RESPONSE

    } < $1  

}

fileExists $1
+ fileExists catalog.csv
+ '[' '!' -f catalog.csv ']'
+ echo 'File Name:  catalog.csv'
File Name:  catalog.csv
work $1
+ work catalog.csv
+ linesToSkip=1
+ (( i=1 ))
+ (( i-- ))
+ read
+ (( 1 ))
+ (( i-- ))
+ read INDEX PROGRAMURL JUNK
+ echo '"http://www.website.com"'
"http://www.website.com"
+ QUERY='curl -XDELETE http://127.0.0.1:9200/cj/_query  -d '\''{ "query" : { "match" : { "PROGRAMURL" : "http://www.website.com" } } }'\'''
"$QUERY")
"$QUERY"
++ 'curl -XDELETE http://127.0.0.1:9200/cj/_query  -d '\''{ "query" : { "match" : { "PROGRAMURL" : "http://www.website.com" } } }'\'''
./deleteExisting.sh: line 29: curl -XDELETE http://127.0.0.1:9200/cj/_query  -d '{ "query" : { "match" : { "PROGRAMURL" : "http://www.website.com" } } }': No such file or directory
IFS=$OLDIFS 
+ IFS='     
'
Run Code Online (Sandbox Code Playgroud)

示例CSV文件将是

INDEX, PROGRAMURL, other, info
"1", "http://website.com", "other info", "information"
Run Code Online (Sandbox Code Playgroud)

6EQ*_*UJ5 7

运行$("$QUERY")实际上试图在子shell中运行,这是一个被调用的命令<the expanded value of $QUERY>,因此你的 '(bunch of stuff) No such file or directory'错误.

你可能想要这样的东西:

CURLURL="http://127.0.0.1:9200/cj/_query"
CURLDATA='{ "query" : { "match" : { "PROGRAMURL" : "'$PROGRAMURL'" } } }'
RESPONSE=`curl -XDELETE "$CURLURL"  -d "$DATA"`
Run Code Online (Sandbox Code Playgroud)

这里的诀窍是如何嵌套单引号和双引号.有点难以简明扼要地解释,但这里有:

  • Shell变量不会在外部单引号内扩展.
  • 但你不需要在单引号内逃避双引号
  • 因此,我们如何$PROGRAMURL扩展以上是立即将它连接在一对封闭开放的单引号之间.
  • 要随后使用CURLDATA它必须传递给curl双引号内的命令的变量
  • 一个更简单的例子:
VARIABLE1="Hello, world"
VARIABLE2='This is "$verbatim" stuff"'$VARIABLE1'" More stuff'
Run Code Online (Sandbox Code Playgroud)
  • 这产生的值$VARIABLE2This is "$verbatim" stuff"Hello, World" More stuff

其他注意事项:

  • 以上将把curl命令的整个stdout 放入变量中RESPONSE
  • 之后你需要检查$?curl是否真的设法与主机等交谈 - 如果一切顺利,它将为0
  • 您可能希望关闭进度条

跳绳:

  • 跳过行的更好方法是使用:
tail -n +$(( $linesToSkip +1 ))
Run Code Online (Sandbox Code Playgroud)