LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2010, 03:46 AM   #1
Kovacs
Member
 
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Rep: Reputation: 31
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
 
Old 11-18-2010, 04:33 AM   #2
MrCode
Member
 
Registered: Aug 2009
Location: Oregon, USA
Distribution: Arch
Posts: 864
Blog Entries: 31

Rep: Reputation: 148Reputation: 148
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"

Last edited by MrCode; 11-18-2010 at 04:39 AM.
 
Old 11-18-2010, 04:47 AM   #3
Kovacs
Member
 
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Original Poster
Rep: Reputation: 31
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.
 
Old 11-18-2010, 06:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Kovacs View Post
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'"
 
Old 11-18-2010, 09:04 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I question on a bash script and filename spaces... trist007 Programming 13 01-17-2010 07:13 AM
Bash escaping super_bash Linux - General 2 08-06-2009 01:04 PM
filename escaping in bash dash9 Linux - Software 1 06-04-2009 11:36 PM
Another basic bash question Izarf Linux - Newbie 1 05-03-2009 06:56 PM
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 02:02 PM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration