0

I'm having a bit of a problem working on a shell script. One of the commands within the script is used to push a set of commands from a Jenkins host to MySQL database hosts to run backups "locally" to the MySQL databases.

The problem comes when exporting a variable for later use. Anonymized example:

ssh -q -o Batchmode=yes mysql@${host} \
"export variable=$(ls -td ${backup_path}/FULL/*/ | head -1); \
echo $variable"

The goal of the ls -td is to pull the latest written directory in the ${backup_path}/FULL subdir.

The error I get when executing this particular command is

ls: cannot access ${backup_path}/FULL/*/: No such file or directory

The echo of $incr_basedir also returns a null output.

I have already attempted is to check for existence of a subdir and verified permissions in ${backup_path}/FULL, here is the output:

-bash-4.2$ pwd
${backup_path}/FULL
-bash-4.2$ ls -l
total 498
drwxr-x---. 28 mysql mysql   2019 May  3 16:53 2017-05-03_16-47-37

How should I proceed in approaching my goal?

  • 1
    You really shouldn't be parsing ls output: mywiki.wooledge.org/ParsingLs – FCTW May 8 '17 at 14:43
  • How might I obtain the "most recently written subdir" without ls? I can also guarantee filenames which will play nicely with ls in this system. @FCTW – Josh Bonello May 8 '17 at 14:59
  • 1
    You could try using find: dwheeler.com/essays/filenames-in-shell.html#find Although I'm not sure that will fix the originating issue... – FCTW May 8 '17 at 15:03
  • 2
    If you use double quotes the variables will be expanded on the "local" side, before being passed to ssh. Use single quotes to have them only get expanded on the remote side, though I'm not sure what you're trying to do with export variable when you never use it – Eric Renouf May 8 '17 at 15:08
  • I only took a snippit of the code, where I am running into issues. I'll see what switching the scope will do. – Josh Bonello May 8 '17 at 15:11
1

The problems were caused due to scoping, as Eric mentioned earlier.

I had many local variables being sent and pushed through the compound ssh instructions which were not listed, which required the usage of double quotes. Since I wanted the ls -td to execute remotely, I made an entirely new ssh command specifically to set this variable. Once the variable was set, I entered it like any other.

    export variable="$(ssh -q -o Batchmode=yes mysql@$host 'ls -td ${backup_dir}/FULL/*/ | head -1')"

The error no longer persists. My issue was, in fact, scoping where the local host did not have the same directory structure as the remote host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.