I need a script to delete a file with the ?character in its name. I don't think changing the name is an option. I'm using bash.
The file will be at "${1}/System/Library/CFMSupport/InterLock?Engine". I wanted to just use a wildcard, but it looks like that doesn't work because of the quotes.
In my testing, "${1}"/System/Library... worked (i.e. putting the quotes directly around the variable and just continuing the string). Is that correct syntax?
AHunter3
09-14-2005, 01:32 PM
Why can't you just type the ? It does display oddly (as \302\256) but it seems to work nonetheless. I created a file "FilenameŽString.txt" on my Desktop and then opened the terminal, went to my Desktop, and typed "rm FilenameŽString.txt". Terminal displayed "rm Filename\302\256Stringt.txt". I hit the return key, Terminal accepted the command and the file vaporized from the Desktop.
giskard22
09-14-2005, 02:12 PM
I was copying and pasting between TextEdit (which had a list of files created by logGen that included this file) and Terminal, and I got a total of 4 \nnn codes instead of 2. So I just figured it didn't work. :)
voldenuit
09-14-2005, 02:53 PM
Unless there is other stuff to be kept that would be unlinked using this expression:
${1}/System/Library/CFMSupport/InterLock*
that would be the cheapest way out.
No quotes, no fuss, plain shell.
giskard22
09-15-2005, 02:41 PM
I can almost guarantee that $1 will have a space in it (It will usually be "/Volumes/Macintosh HD"). Can I still do without any quotes?
biovizier
09-15-2005, 03:36 PM
I'm confused - are you having trouble with the spaces or the symbol? It's kind of a pain, but to use ® in a command, you could do something like:r=$(echo -e '\302\256')
ls -l "$1/System/Library/CFMSupport/InterLock${r} Engine"
giskard22
09-15-2005, 04:11 PM
The problem is having both the spaces and the symbol. My impression is that I need quotes somewhere in there because $1 is practically guaranteed to have a space in it. But quoting the whole string means I can't use a wildcard.
Like I said, "${1}"/System/Library... seems to work. I was just curious if voldenuit's suggestion would still be "space safe".
voldenuit
09-15-2005, 04:43 PM
Like I said, "${1}"/System/Library... seems to work. I was just curious if voldenuit's suggestion would still be "space safe".
No, your syntax is fine if there are spaces to be expected in $1:
"${1}"/System/Library/CFMSupport/InterLock*
houchin
09-16-2005, 08:03 AM
Are you trying to delete one file one time, or is this something that's really going in script for multiple use? Either way, I believe a much simpler solution would be to let the shell autocomplete the file name using tabs at the command line, and then copy that correctly escaped filename to your script if you need to do it again:
cd to whatever {$1} will be. Then start typing the begining of each directory portion, followed by tab. Provided you typed enough to specify a single file, the shell will complete that with portion of the path with the non-standard characters correctly escaped.