LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic bash filename escaping question (https://www.linuxquestions.org/questions/linux-newbie-8/basic-bash-filename-escaping-question-845012/)

Kovacs 11-18-2010 02:46 AM

Basic bash filename escaping question
 
Can someone please tell me what I have done wrong with my escaping here:

Code:

$ php -q get_vid_dimensions.php '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
Syntax error: Unterminated quoted string

The original filename is:
Code:

/home/kovacs/16-Exclusive McDonald's Farmville farm.flv

MrCode 11-18-2010 03:33 AM

First of all, I would put the filename in double quotes (i.e. ""). Secondly, you're not escaping all the necessary characters. As a general rule, anything that isn't a number, letter (capital/lowercase), underscore, dot, or a dash (hyphen) shouldn't go into a Linux/UNIX filename; that way you can avoid problems like this. You need to escape your spaces as well (and drop the single-quotes around your escaped apostrophe).

This would be more correct, I think:

Code:

"/home/kovacs/16-Exclusive\ McDonald\'s\ Farmville\ farm.flv"

Kovacs 11-18-2010 03:47 AM

Unfortunately I have no control over the filename. The reason I put the filename in single quotes is to avoid having to escape spaces. Also some of the other filenames in this batch of media have other special characters in them which would also need escaping if I were to put them in double quotes - it's easier overall to put the filenames in single quotes in this instance.

The problem is with the escape sequence used to escape a single quote inside a filename that is encapsulated by single quotes. From the reading I've been doing, it's not enough to use a single backslash, eg.
Code:

'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'
but instead you have to actually make two strings, which bash will concatenate if they are right next to each other:
Code:

'/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
The first single quote represents the end of the first part of the filename, then you have your escaped single quote, then another quote to signal the beginning of the second part of the filename. In theory bash should put the whole together into one filename.

catkin 11-18-2010 05:24 AM

Quote:

Originally Posted by Kovacs (Post 4162819)
Code:

$ php -q get_vid_dimensions.php '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
Syntax error: Unterminated quoted string


The quoting is correct in bash so the string is being parsed twice.

Perhaps this would work:
Code:

"'/home/kovacs/16-Exclusive McDonald'\\''s Farmville farm.flv'"

i92guboj 11-18-2010 08:04 AM

Indeed, that error is not bash complaining, but the php parser. Bash parses the string, the resultant string then is passed to php:

Code:

# echo '/home/kovacs/16-Exclusive McDonald'\''s Farmville farm.flv'
/home/kovacs/16-Exclusive McDonald's Farmville farm.flv

That results in an error on the php's side while parsing the resulting string. So, in other words, you need to get bash to produce an output that is valid php syntax for string handling.

Code:

# echo "'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'"
'/home/kovacs/16-Exclusive McDonald\'s Farmville farm.flv'

I am no php expert, and in any case what is "valid" depends only on what exactly do your php code expect to receive.


All times are GMT -5. The time now is 03:48 PM.