我对 shell 不是很熟悉,在 bash 文件中有以下 perl 脚本可以运行以从我的网络服务器获取文件:
perl -pe 'BEGIN { while (<>) { last if $_ eq "\r\n"; } }'
有没有办法把它改成 shell,这样我就可以独立于 perl 了?
这是完整的脚本示例:
echo -ne "GET /file HTTP/1.0\r\nHost: www.example.com\r\n\r\n" | nc www.example.com 80 | perl -pe 'BEGIN { while (<>) { last if $_ eq "\r\n"; } }' > file
感谢您提供的任何帮助。
最佳答案
这是做什么的?
perl -pe 'BEGIN { while (<>) { last if $_ eq "\r\n"; } }'
它删除开头的所有内容,直到仅包含 \r\n
的行。这个 Sed 会做同样的事情:
sed -e '1,/^'$'\r''$/d'
这是做什么的,它删除了从第一行到包含 \r
的行的范围(\n
不是 Sed 中该行的一部分) .
如果输入有 UNIX 风格的行结尾,它可能会简单得多:
sed -e '1,/^$/d'
将 perl while 循环更改为 shell
如果您的意思是将 Perl 更改为纯 Bash, 甚至没有 Sed 或 Awk,这是一种方法:
strip_until_first_blank() {
seen_blank=
while read line; do
if [ "$seen_blank" ]; then
echo $line
elif ! [ "$line" ]; then
seen_blank=1
fi
done
}
您可以将管道中的 Perl 命令替换为 strip_until_first_blank
。
您也可以将其写在一行中,但这样会难以阅读:
{ seen_blank=; while read line; do if [ "$seen_blank" ]; then echo $line; elif ! [ "$line" ]; then seen_blank=1; fi; done; }
... 或使用 curl
或 wget
...
作为@Cyrus在评论中指出,
您实际上可以用 curl
或 wget
替换整个管道:
wget -O file http://www.example.com/file
curl -o file http://www.example.com/file
关于bash - 将 perl while 循环更改为 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195428/