0

I am pretty much a noob when it comes to writing a shell script. I find myself with such a simple issue and I've tried googling and looking at other StackOverflow questions but to no avail. I still get the same error. I am working with Jenkins and running this command as a shell script.

I am trying to input a file path into another shell script as a variable. However, I kept getting the error that the file or directory doesn't exist. I realized that there was a space in one of the folders in the path. I have tried escaping it with double quotes, single quotes, and also formatting it based on the answers to this question.

My current command:

FILENAME=$(printf %q "//MyServer/Build Archive/Daily_Builds/DailyBuild_29627.zip")
$FILENAME

I get the following output:

printf %q '//MyServer/Build Archive/Daily_Builds/DailyBuild_29627.zip'
+ FILENAME='//MyServer/Build\ Archive/Daily_Builds/DailyBuild_29627.zip'
+ '//MyServer/Build\' Archive/Daily_Builds/DailyBuild_29627.zip
line 6: //MyServer/Build\: No such file or directory
Dtb49
  • 1,211
  • 2
  • 19
  • 48
  • 1
    The backslash is not escaping the space in this case; it's a literal character in the value of `FILENAME`. – chepner Sep 29 '20 at 14:33
  • 1
    `%q` is intended to produce values that are safe to use with `eval`. – chepner Sep 29 '20 at 14:37
  • 1
    ...so, to summarize the above comments: Everything is behaving as designed. Just running `$FILENAME` isn't _ever_ expected to work; see [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) – Charles Duffy Sep 29 '20 at 14:42
  • BTW, while some of the answers on the linked duplicate suggest `eval`, this is not an advisable practice. See [BashFAQ #48: `eval` command and security issues](http://mywiki.wooledge.org/BashFAQ/048). Instead, follow the practices given in BashFAQ #50 above. – Charles Duffy Sep 29 '20 at 14:43
  • How can I get this to work though? lol If I remove it and just surround it with double quotes or single quotes it still doesn't work. I just get, //MyServer/Build: No such file or directory – Dtb49 Sep 29 '20 at 14:48
  • What is your real use case that `FILENAME="//MyServer/Build Archive/Daily_Builds/DailyBuild_29627.zip"` isn't sufficient, followed by `"$FILENAME"`? (This is OK, because you only have a single command name in the variable, not a command plus its arguments.) – chepner Sep 29 '20 at 14:50
  • `myshellScript.sh filename=$FILENAME` with this I get the following, `Archive/Daily_Builds/DailyBuild_29627.zip: No such file or directory` – Dtb49 Sep 29 '20 at 14:52
  • Didn't ever figure out why I couldn't escape the space but, instead just changed directories to the folder in question instead. I didn't have any issues after that – Dtb49 Sep 29 '20 at 15:37

0 Answers0