0

I have a perl script that runs this command:

my $sshstart = `ssh username\@server "cd "$(dirname "$(find -type f -name AddAlias.pl)")" "`;

which works when I run the ssh part in the actual terminal but ran in a script returns this:

syntax error at line 1 : `)' unexpected

I've removed the "" everywhere (except the ssh command "" because it has to have that to run any commands once I get into the server) which it can still run just in the terminal.

I've removed all the ()

I've added () to the front and back, or doing only one on each side and still no luck.

Is it because its within the ssh quotes ""?

If so what syntax do I use?

  • 1
    You are bumping up against many common beginner mistakes here, enough to make it tempting to vote to close as an [XY problem](https://en.wikipedia.org/wiki/XY_problem). What exactly should the `ssh` snippet eventually do? Just a `cd` inside an `ssh` is completely useless because the remote process will exit and then the working directory is back to what it was before. – tripleee Oct 23 '22 at 10:20
  • Well after the cd command, which is giving an odd error that's different now, I have it call a script that will live in the folder it finds. So after the cd command which I encased it in "" I put a semi colon so I can then run the script – Jacob Boyce Oct 24 '22 at 11:18
  • Thanks for the clarification. I'll repeat my `find ... -execdir` suggestion. Another common beginner mistake is _assuming_ that you _have to_ `cd` to a particular directory, but let's hope you have that covered, too; perhaps see also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Oct 24 '22 at 11:32
  • Thanks tripleee, I appreciate that. The current directory when I get there after the ssh isn't the one I need. I just have to run a script on that server after sshing there. This is searching for the directory that has that script in it. And each server I do this on, 26 different ones, might not have the same folder where the script is. – Jacob Boyce Oct 25 '22 at 12:20

1 Answers1

1

You need to use single quote and escape $:

my $sshstart = `ssh username\@server 'cd "\$(dirname "\$(find /OnSight/jetplan -type f -name AddAlias.pl)")"'`;
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • 1
    ... Though probably `find ... -execdir 'whatever you wanted to do after the "cd" {} \;` would be more robust, idiomatic, and efficient. – tripleee Oct 23 '22 at 10:16