TryTryAgain
TryTryAgain

Reputation: 7820

"Escaping" nightmare

I have been trying to put the following into a bash script:

scp /cygdrive/c/Program\ Files\ \(x86\)/Spiceworks/data/configurations/LA_$(date +%F)-firewall-5520 "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08\ -\ Security/Firewalls"

...which works as expected via command line.

When I try to place it in a script, I'm obviously going to have to double escape the spaces, anything else? I'm unable to have success with the multitude of variations I've tried.

I would think:

scp /cygdrive/c/Program\\ Files\\ \(x86\)/Spiceworks/data/configurations/LA_\$(date\ +%F)-firewall-5520 "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08\\ \\-\\ Security/Firewalls"

...would work, but it doesn't. Any suggestions, before I go grey and bald? Thanks

I wont waste the space with all variations I've tried, but I will say I have tried escaping almost everything and basically nothing and many in between with no success. When I receive a "No such file or Directory" I obviously escape until the file actually resolves, but even when I do not get a path error, the command is not successfully completing.

I do understand this is quite a specific case, but I imagine it will help others in the future: escaping spaces, in a bash script, using embedded expect (I have tested with a #!/bin/bash shebang and the embedded expect using expect -c ' .... ' as well as #!/usr/bin/expect using a simple spawn scp command with no change.

EDIT (Based on responses):

I should have mentioned I have tried quoting it...

Quoting the first (host) part gives me the error

can't read "(date +%F)": no such variable while executing

"spawn scp "/cygdrive/c/Program Files (x86)/Spiceworks/data/configurations/LA_$(date +%F)-firewall-5520" "[email protected]:/home/sf-mlawler/SBOX_..."

...it is not a variable, it is a function giving me the current date in the format year-month-day

Quoting the destination section without escaping anything gives me the error

scp: ambiguous target

Upvotes: 1

Views: 753

Answers (2)

ocodo
ocodo

Reputation: 30248

Just stick it in quotes and stop hitting yourself, as @that_other_guy said, you cannot, and shouldn't try and escape twice, wherever you got that idea, disregard it as a source of information. (my guess is it came from "thin air")

scp "/cygdrive/c/Program Files (x86)/Spiceworks/data/configurations/LA_$(date +%F)-firewall-5520" "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08 - Security/Firewalls"

You could even give yourself some helpers:

export PROGFILESx86="/cygdrive/c/Program Files (x86)"
export SPICEWORKS_CONFIGS="${PROGFILESx86}/Spiceworks/data/configurations"

(add them to .bashrc, exec it.) and then do:

scp "${SPICEWORKS_CONFIGS}/LA_$(date +%F)-firewall-5520" "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08 - Security/Firewalls"

I'd also be concerned the the files you are attempting to scp actually exist, wrap it up in a condition to verify they're there, it only makes sense when you're auto-generating a filename.

firewall_config="${SPICEWORKS_CONFIGS}/LA_$(date +%F)-firewall-5520"

if [ -e "${firewall_config}" ]
    then
        scp "${firewall_config}" "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08 - Security/Firewalls"
    else         
        echo "File doesn't exist: ${firewall_config}"
fi

Update:

Since you've updated your question, it's obviously the date call that's giving you the biggest problem, again, indirection saves you doing bizarre escape-char-yoga, just get the result into a var and use that.

fw_date=$(date +%F)
firewall_config="${SPICEWORKS_CONFIGS}/LA_${fw_date}-firewall-5520"

Upvotes: 2

that other guy
that other guy

Reputation: 123460

The same command that worked on your bash shell should work the same in your bash script. You shouldn't and cannot escape it twice.

If it doesn't work, make sure you're testing the script from the same terminal, ensure that the script contents and your command is identical, then paste the output from your terminal when you do:

$ scp /cygdrive/c/Program\ Files\ \(x86\)/Spiceworks/data/configurations/LA_$(date +%F)-firewall-5520 "[email protected]:/home/sf-mlawler/SBOX_Automation/SBOX_Dumps/08\ -\ Security/Firewalls"
(successful scp output here)
$ cat testscript
(testscript contents here, should be identical to above scp command)
$ bash testscript
(unsuccessful script/scp output here)

Upvotes: 3

Related Questions