语法错误:`('shell脚本中的意外

时间:2017-01-10 15:06:32

标签: perl shell

我有一个shell脚本,其中包含一些函数。其中一个函数必须通过perl执行。 perl函数检查端口是否在远程服务器上打开。

#!/usr/bin/ksh

function1
function2
telnet_check()
{

#!/usr/bin/perl -w

use IO::Socket;
use IO::Socket::INET;

my ($host,$port);
$host=Ip address ;
$port=9443;
my $sock=IO::Socket::INET->new("Ip address:$port") or die "" ;

}

some shell commands

执行上述脚本时,收到错误

 syntax error at line: `(' unexpected [which falls in the line my ($host,$port); under the Perl function]

任何人都可以帮助解决上述错误。

提前干杯:)

1 个答案:

答案 0 :(得分:3)

您无法轻松地从ksh切换到Perl。引用脚本并将其作为参数传递给perl的-e

perl -MIO::Socket -MIO::Socket::INET -we 'my ($host, $port) = qw( host.name 9443 ); ...'

或者将perl脚本存储在自己的文件中,并运行它:

perl /path/to/the/script.pl

或者,将脚本发送到perl的标准输入 - 只有在您不需要从脚本中读取输入时才有效。

cat <<'EOF' | perl
use IO::Socket;
use IO::Socket::INET;
...
EOF
相关问题