Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux shell脚本,多次使用变量_Linux_Bash_Shell - Fatal编程技术网

Linux shell脚本,多次使用变量

Linux shell脚本,多次使用变量,linux,bash,shell,Linux,Bash,Shell,我创建了一个脚本,它使用数据文件访问网站,并将网站响应(一行XML)输出到输出文件。我希望输出从数据文件的查询开始,然后是网站的响应。当我回显查询一行并将其写入一个输出文件,然后将站点的响应写入同一个输出文件时,它使用两行,但我只需要一行,因为我希望最终得到一个逗号分隔的文件,可以在excel中导入 这适用于具有两行数据的情况: while read -r line || [[ -n $line ]] do datatogather="$line" echo $datatogather >

我创建了一个脚本,它使用数据文件访问网站,并将网站响应(一行XML)输出到输出文件。我希望输出从数据文件的查询开始,然后是网站的响应。当我回显查询一行并将其写入一个输出文件,然后将站点的响应写入同一个输出文件时,它使用两行,但我只需要一行,因为我希望最终得到一个逗号分隔的文件,可以在excel中导入

这适用于具有两行数据的情况:

while read -r line || [[ -n $line ]]
do
datatogather="$line"
echo $datatogather >>outputfile.txt
curl http://login:password@somewebsite.info/application.php?$datatogather >>outputfile.txt
echo >>outputfile.txt
done < datafile.txt
读取时-r行| |[[-n$line]]
做
datatogather=“$line”
echo$datatogather>>outputfile.txt
卷曲http://login:password@somewebsite.info/application.php?$datatogather>>outputfile.txt
echo>>outputfile.txt
完成
这不起作用(尽管它在输出文件中显示逗号,因此正在处理该行):

读取时-r行| |[[-n$line]]
做
datatogather=“$line”
echo$datatogather,>>outputfile.txt | curlhttp://login:password@somewebsite.info/application.php?$datatogather>>outputfile.txt
echo>>outputfile.txt
完成

用sed剥离垃圾数据的输出文件是一件轻而易举的事情,甚至将输入文件读入站点也比在一行中多次使用一个变量要容易得多。希望您能帮助我。

执行一个
echo
操作而不是两个,并且只执行一个重定向而不是多个重定向,并使用捕获
curl
的输出:

while read -r line && [[ -n "$line" ]]
do
    echo "$line,$(curl http://login:password@example.com/application.php?$line)"
done < datafile.txt  >>outputfile.txt
请注意,如果网站的输出中包含任何逗号,您可能会遇到问题,但您(尚未)询问过这一点

您不希望将
tr
操作置于循环之外;您希望在每个
echo
的末尾都有一个换行符,因此这是不好的:

读取时-r行&[-n“$line”]]
做
回显“$行,$(卷曲http://login:password@example.com/application.php?$line)
完成>outputfile.txt

您将
read-r-line | |[$line]]
更改为
read-r-line&&[$line]
。这完全改变了代码的含义。前一种情况是一种处理方法,因此文件不是(我真的不喜欢越来越多的人认为这样做很聪明,而且做
read-r line | |[$line]看起来很酷)
当他们甚至不知道a是什么时。后一种情况会在空行(或不完整的行)出现时打破循环。
[
中的引号在等式的rhs中是强制性的。在这里可以安全地使用
[-n$line]
或者更好的
[$line]]
。现在您提到了引号:您肯定应该引用地址
http://login:password@example.com/application.php?$line
curl
语句中!您显然有一个glob字符
和一个变量扩展!这就是您需要引号的地方。感谢您的回复。唯一的问题是我得到了换行符在我的输出中。它在一行上用逗号显示行值,在另一行上显示输出。我的input datafile.txt在每行上都有一个变量,从网站创建的输出在每行上也是一个变量(xml字符串)。以下操作不起作用:
echo“$(echo“$line”| tr'\n'')$(请登录:password@example.com/application.php?$line)“
while read -r line && [[ -n "$line" ]]
do
    echo "$line,$(curl http://login:password@example.com/application.php?$line)"
done < datafile.txt  >>outputfile.txt
while read -r line && [[ -n "$line" ]]
do
    echo "$line,$(curl http://login:password@example.com/application.php?$line | tr '\n' ' ')"
done < datafile.txt  >>outputfile.txt
while read -r line && [[ -n "$line" ]]
do
    echo "$line,$(curl http://login:password@example.com/application.php?$line)"
done < datafile.txt | tr '\n' ' ' >>outputfile.txt