我有一个.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
除了执行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='
'
示例CSV文件如下:
INDEX, PROGRAMURL, other, info
"1", "http://website.com", "other info", "information"
query
的函数。将$PROGRAMURL
作为参数传递。 - n. m.$1
的内容与该函数内的其余参数-d
连接起来... - 6EQUJ5