Perl using find in qx

389 Views Asked by At

III am writing a Perl script that will need to SSH out to numerous remote servers to perform some gzipping of log files. In the following line, I keep receiving this error and am struggling to determine what's causing this. The error I'm getting is;

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `cd /appdata/log/cdmbl/logs/; echo cd /appdata/log/cdmbl/logs/; find . -type f ( -iname '*' ! -iname '*.gz' ) -mmin +1440 ;; exit 0'

And of course, as you can tell by the error, the line I am trying to write is;

my $id = qx{ssh -q $cur_host "cd $log_path; echo cd $log_path; find . -type f \( -iname '*' ! -iname '*.gz' \) -mmin +1440 \;; exit 0"};

Am I overlooking something here that is causing the unexpected token '(' issue I am receiving?

NOTE: I removed the -exec from find just so I could see if I can get past this issue first.

Thanks.

3

There are 3 best solutions below

1
On BEST ANSWER

You need to backslash the parentheses for the shell. Using single backslash in double quotes is not enough, Perl removes the backslash. Use double backslash \\(.

0
On

This is probably not going to answer your question, but it's a nice alternative that I would like to propose.

You said you cannot install additional modules on the production servers. You need to run a bunch of stuff where you are looking for files and zipping them. That can all be done in Perl, and you may have more controll over it than through the "doing command line stuff from a Perl script" approach.

Take a look at Object::Remote, which was written for exactly that purpose. It lets you ssh into machines and run Perl stuff there that you have installed on your local machine. That way, you do not need to add modules or install anything on the remote. All it needs is any kind of more or less recent Perl, which fortunately almost every Linux comes with.

There is a very good lightning talk about it by the author Matt Trout that is well worth watching.

2
On

If the command you built results in a syntax error, wouldn't the first step be to see what command you built?

print qq{ssh -q $cur_host "cd $log_path; echo cd $log_path; find . -type f \( -iname '*' ! -iname '*.gz' \) -mmin +1440 \;; exit 0"}, "\n";