0

I am working in my Google Drive directory that is mounted on my Mac. The path to my Google Drive folder looks like: "/Users/username/Library/CloudStorage/GoogleDrive-gmail-acct/My Drive"

Notice the space between "My" and "Drive"

I am trying to run a few different codes written in C that I am passing the path of a file in my Google Drive to as an argument on the command line. I set the filename as a variable, and then pass that variable (this is in a script called "script.sh" and I run it on the command line as bash script.sh). My shell is bash (on Mac). It looks like:

filname='/Users/username/Library/CloudStorage/GoogleDrive-gmail-acct/My Drive/myfile'

codename --filename $filname

I am getting the following error:

*Error*: cannot open /Users/<username>/Library/CloudStorage/GoogleDrive-<gmail-acct>/My

So the path is getting broken up at the space

Things I have tried:

  1. Using double quotes ("") around the definition of filname
  2. Using an escape character (backslash) before the space in definition
  3. Lots of combinations of those things
  4. Putting {} around argument, like ${filname} and also "${filname}"

Unfortunately, I have ruled out with Google anyway to rename my Google Drive folder without a space. Does anyone know how I can handle the space since escaping it normal ways is not working?

EDIT:

I have tried the following which does not work:

script.sh

filname='/Users/username/Library/CloudStorage/GoogleDrive-gmail-acct@g.rit.edu/My Drive/file.fits'
codename "$filname"

Error:

> *Error*: cannot open /Users/username/Library/CloudStorage/GoogleDrive-gmail-acct@g.rit.edu/My

EDIT #2 with more details:

The code I am using is a code written in C that is used on the command line. I work in a very small field and the code is not public so I cannot say what it is. It is run by doing:

codename --args

specifically, I am running it in a script.sh script (with bash script.sh) exactly as I said in my previous edit.

The path to the input file has a space in it. I think the problem is how the command line arguments are being piped into the code and are being broken by a space. To clarify, the following does work:

filname='/Users/username/Library/CloudStorage/GoogleDrive-gmail-acct@g.rit.edu/My Drive/file.fits'
echo "$filname"

but then this following does not work:

filname='/Users/username/Library/CloudStorage/GoogleDrive-gmail-acct@g.rit.edu/My Drive/file.fits'
codename "$filname"

Error:

> *Error*: cannot open /Users/username/Library/CloudStorage/GoogleDrive-gmail-acct@g.rit.edu/My

I know I really need to provide a MWE people could run themselves, and I cannot share the exact code, but I have had this exact problem with two or three other codes, all written in C and run from command line.

coxi2016
  • 1
  • 1
  • If you already tried the very last variant the `codename` command or whatever it is handles the argument wrong. `"$filename"` is correct. The `{}` are optional here. For your definition double quotes, single quotes or escape all work fine. – Paul Pazderski Jul 31 '23 at 19:46
  • 1
    Just using `"$filname"` in the command should work. The space is only meaningful the the shell's parser. Once you get past that, nothing else will care about it. – Brian61354270 Jul 31 '23 at 19:46
  • 4
    As a side note, why `filname` instead of `filename`? That's just begging for typos – Brian61354270 Jul 31 '23 at 19:49
  • I just edited the question showing "$filname" does not work – coxi2016 Jul 31 '23 at 19:52
  • What is `codename`? A shell script, also mishandling spaces in the same way? – Jonathon Reinhart Jul 31 '23 at 19:54
  • 1
    If `codename` is a shell script, it needs to quote the variable as well: `"$1"` – Barmar Jul 31 '23 at 19:56
  • 3
    Run your code through https://shellcheck.net/; it'll find all the quoting bugs we're telling you about. – Charles Duffy Jul 31 '23 at 19:57
  • 2
    To be very clear, the exact code you're giving us with the quotes added in the "Edit:" section is correct, so there has to be something you're _not_ showing us that you're doing in how you invoke it or otherwise unshown surrounding context. We need a [mre] -- code complete enough we can run it ourselves without changes to see the same problem -- to have an on-topic, answerable question; try using an online sandbox like https://repl.it/ to test the code samples you propose including to make sure they reproduce the same problem when run without changes. – Charles Duffy Jul 31 '23 at 19:59
  • (...and keep in mind that when you use directory names/script names/etc that don't exist on a default system that your example doesn't create, that prevents your code from being something that can run "without changes") – Charles Duffy Jul 31 '23 at 20:05
  • 1
    Does your code written in C maybe use `system()`? That would be a way you could get word-splitting to happen _after_ `codname` is invoked. Really, though -- if it's happening to you in several C programs you built yourself, write something new designed to be _the smallest/simplest possible C program that has the same problem_ and include it in the question. – Charles Duffy Jul 31 '23 at 21:11
  • Aside from the correct comment by Brian61354270 : Did you consider switching from bash to zsh? In zsh, you wouldn't quote $filename, because in zsh, word splitting occurs before parameter expansion (unless you explicitly request it otherwise). If you are frequently plagued by quoting problems, you will perhaps find zsh easier to use. – user1934428 Aug 01 '23 at 06:38

0 Answers0