【问题标题】:syntax error: `(' unexpected in shell script [duplicate]语法错误:`('在shell脚本中意外[重复]
【发布时间】:2017-01-10 15:06:32
【问题描述】:

我有一个 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]

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

提前干杯:)

【问题讨论】:

  • 嘿@toolic 我已经尝试过这样并且它有效。但是,我想把它放在一个脚本本身中。这可以实现吗?

标签: perl shell


【解决方案1】:

你不能轻易地从 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

【讨论】:

  • 以最后一种方式尝试时出错。第 10 行的语法错误:`
  • @Su_scriptingbee:你确定结尾的EOF 是单独在线的,即它周围没有空格吗?
  • EOF 和右大括号之间有一个空格。我现在更正了它,它工作正常:) 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 2016-08-11
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多