LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 02:46 AM   #1
Kovacs
Member
 
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Rep: Reputation: 32
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, 03: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 03:39 AM.
 
Old 11-18-2010, 03:47 AM   #3
Kovacs
Member
 
Registered: Jul 2003
Distribution: FreeBSD 8.2 RELEASE
Posts: 607

Original Poster
Rep: Reputation: 32
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, 05:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 08:04 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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



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 06:13 AM
Bash escaping super_bash Linux - General 2 08-06-2009 12:04 PM
filename escaping in bash dash9 Linux - Software 1 06-04-2009 10:36 PM
Another basic bash question Izarf Linux - Newbie 1 05-03-2009 05:56 PM
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 01:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:49 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
Open Source Consulting | Domain Registration