Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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
Bash 将perl while循环更改为shell_Bash_Perl_Shell - Fatal编程技术网

Bash 将perl while循环更改为shell

Bash 将perl while循环更改为shell,bash,perl,shell,Bash,Perl,Shell,我对shell不太熟悉,在bash文件中运行以下perl脚本以从Web服务器获取文件: perl -pe 'BEGIN { while (<>) { last if $_ eq "\r\n"; } }' perl-pe'BEGIN{while(){last if$\ueq“\r\n”;}} 有没有办法把它改成shell,这样我就可以独立于perl了 以下是完整的脚本示例: echo -ne "GET /file HTTP/1.0\r\nHost: www.example.com\

我对shell不太熟悉,在bash文件中运行以下perl脚本以从Web服务器获取文件:

perl -pe 'BEGIN { while (<>) { last if $_ eq "\r\n"; } }'
perl-pe'BEGIN{while(){last if$\ueq“\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
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$\r\n”}文件
感谢您提供的任何帮助。

这有什么用

这将删除从第一行到包含
\r
的行的范围(Sed中
\n
不是该行的一部分)

如果输入具有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\u,直到第一个\u为空

你也可以把它写在一行上,但很难阅读:

{ seen_blank=; while read line; do if [ "$seen_blank" ]; then echo $line; elif ! [ "$line" ]; then seen_blank=1; fi; done; }
... 或者使用
curl
wget
。。。 正如评论中指出的那样, 实际上,您可以使用
curl
wget
替换整个管道:

wget -O file http://www.example.com/file
curl -o file http://www.example.com/file

bash的内置功能可能会有所帮助。@Gowtham谢谢,我对bash也不是很熟悉,我的代码主要基于我读过的一些示例。我不确定如何实现read
wget-O文件http://www.example.com/file
?如果你想摆脱
nc
,请看:@Cyrus谢谢,无论如何,这可能是一行?如果我想进一步编辑这个命令,只使用shell/bash,是否可以使用任何东西来替换nc,因为并非所有系统都安装了它?谢谢你的评论顺便说一句,这回答了我的问题,我只是想了解更多的信息。呃。。。对仔细看,正如@Cyrus指出的,看起来你可以用一个简单的
curl
wget
来代替所有这些。。。请参见结尾处的我的更新,这里的要点是不要使用某些系统(如wget或curl)中可能未包含的包@janos
curl
wget
也将处理分块响应,这并不少见。(它们还将处理压缩响应,但如果不请求,您不太可能遇到这些响应之一。)
{ seen_blank=; while read line; do if [ "$seen_blank" ]; then echo $line; elif ! [ "$line" ]; then seen_blank=1; fi; done; }
wget -O file http://www.example.com/file
curl -o file http://www.example.com/file