【发布时间】:2019-02-22 18:48:18
【问题描述】:
我有这个文件
1.json2 - 2.json2 - 3.json2
1.json2.ml - 2.json2.ml -
示例 ml 文件
1.json2.ml
{"message":"Validation error","error":"validation_error",...
2.json2.ml
{"Ok":"OK":"OK"...}
我想搜索 *.json2.ml 是否没有执行帖子并保存。 如果文件存在,查看是否有错误并执行 Post。
这是我使用的代码
find . -type f -name '*.json2' | xargs bash -c 'for fname
do if [ ! -e ${fname}.ml ]
then curl -X POST -H "Content-Type: application/json" -d @${fname} https://web/api/post > ${fname}.ml
else
sed '1d' ${fname}.ml | while read line
do
FS=',' read pid pname
if [ "$var" -e ""error":"validation_error"" ]
then
curl -X POST -H "Content-Type: application/json" -d @${fname} https://web/api/post > ${fname}.ml
echo que ha y $pname
fi
done
' bash
我有这个结果
syntax error near unexpected token `fi'
预期的结果是什么
1 - Post 3.json2 ( file .ml no exist )
2- Post 1.json2 ( File .ml exist and have error in )
3- 2.json2 and 3.json2 ( do nothing because json2.ml is OK)
【问题讨论】:
-
您使用单引号作为
bash -c参数的分隔符,以及其中的字符串。那些内引号是字符串的结尾。 -
看来您没有复制整个脚本。它在
while循环的中间结束。 -
在单引号内的字符串周围使用双引号,例如
sed "1d" -
虽然,在这种情况下,
sed的1d参数周围的单引号实际上并没有引起问题。它终止字符串,但您可以简单地省略它们。但是,您的脚本显然被截断了,因为在任何地方都没有fi的实例。 -
在 xargs 的参数中编写复杂的脚本是个坏主意。编写脚本,将其放入文件中,使其可执行并调用它。