LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-13-2007, 09:02 PM   #1
insecurityman
LQ Newbie
 
Registered: Jan 2007
Posts: 3

Rep: Reputation: 0
echo escape character weirdness


I've been working on my first bash script of any complexity - a wrapper script for Wine - and I've been doing okay except for one baffling issue with echo, of all things. The problem comes up in the following command, which I use to strip Wine application command line arguments:
Code:
WINECOMMAND=`echo $@ | grep -o -i ".*\(exe\|bin\|msi\)"`
The odd thing about this is that echo, when run from the script, acts as though its been executed with the -e command-line option, meaning that when given the command
Code:
"C:\Program Files\Interplay\Fallout\falloutw.exe"
it interprets the \f as a carriage return and turns it into
Code:
"C:\Program Files\Interplay\Fallout
                                   alloutw.exe"
Stranger still, when I try it in a bash shell it executes as it should.

This is on Ubuntu Gutsy, and this is a /bin/sh script - is this a dash issue? Would converting the script to /bin/bash clear things up? Or am I just missing something blatantly obvious?

(The entire script can be found in this thread, by the way)

Thanks for any help!

Last edited by insecurityman; 09-13-2007 at 09:04 PM.
 
Old 09-14-2007, 03:16 AM   #2
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,644

Rep: Reputation: Disabled
Did you try it with backticks <'> or <`>instead of <">?

Btw. #!/bin/sh is used for portability, if you have bash anyhow and use the script only on that machine just change it as you suggested yourself, maybe you'll find the causation later...
 
Old 09-14-2007, 02:25 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 620

Rep: Reputation: 362Reputation: 362Reputation: 362Reputation: 362
Hi!

Yes, this is a `dash' issue.
I tried to run following script
Code:
line="C:\Program Files\Interplay\Fallout\falloutw.exe"
WINECOMMAND=`echo -E $line | grep -o -i ".*\(exe\|bin\|msi\)"`

echo -E "$line"
echo -E "$WINECOMMAND"
through `bash' and `dash' interpreters. Here are the results:
Code:
$ bash escape.sh
C:\Program Files\Interplay\Fallout\falloutw.exe
C:\Program Files\Interplay\Fallout\falloutw.exe
$ dash escape.sh
-E C:\Program Files\Interplay\Fallout
                                     alloutw.exe
-E -E C:\Program Files\Interplay\Fallout
                                        alloutw.exe
As you can see, `dash' did not interpret the `-E' option of echo (don't interpret backslash sequences) as an option.

You can use something like this:
Code:
ECHO='/bin/echo -E'
line="C:\Program Files\Interplay\Fallout\falloutw.exe"
WINECOMMAND=`$ECHO $line | grep -o -i ".*\(exe\|bin\|msi\)"`

$ECHO "$line"
$ECHO "$WINECOMMAND"
... or use bash.
 
Old 09-15-2007, 02:54 AM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by insecurityman View Post
I've been working on my first bash script of any complexity - a wrapper script for Wine - and I've been doing okay except for one baffling issue with echo, of all things. The problem comes up in the following command, which I use to strip Wine application command line arguments:
Code:
WINECOMMAND=`echo $@ | grep -o -i ".*\(exe\|bin\|msi\)"`

Use printf rather than echo for both reliability and portability:

Code:
printf "%s\n" "$*\n" | grep ...
Quote:
The odd thing about this is that echo, when run from the script, acts as though its been executed with the -e command-line option, meaning that when given the command
Code:
"C:\Program Files\Interplay\Fallout\falloutw.exe"
it interprets the \f as a carriage return and turns it into
Code:
"C:\Program Files\Interplay\Fallout
                                   alloutw.exe"

Dash adheres pretty closely to the POSIX/XSI standard, which says that echo shall interpret certain escape sequences rather than render them literally.
Quote:
Stranger still, when I try it in a bash shell it executes as it should.

Bash has a number of non-POSIX extensions.
Quote:
This is on Ubuntu Gutsy, and this is a /bin/sh script - is this a dash issue? Would converting the script to /bin/bash clear things up? Or am I just missing something blatantly obvious?

For portability, stick to POSIX syntax.
 
Old 09-17-2007, 08:58 PM   #5
insecurityman
LQ Newbie
 
Registered: Jan 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks, all of you - that was really informative. This whole learning bash things is surprisingly fun.

I ended up converting the script over to bash, but the next thing I'll do is convert it back before I start getting too used to bashisms. I'll also start using printf instead of echo - I vaguely remember that issue being brought up in a few C tutorials I did a while back, but I didn't think to apply it here.

Thanks once more.
 
Old 09-18-2007, 01:42 AM   #6
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,644

Rep: Reputation: Disabled
In case you don't know it already:

http://www.tldp.org/guides.html#abs

and for variety's sake

http://zsh.sunsite.dk/Guide/zshguide.html

Last edited by JZL240I-U; 09-18-2007 at 01:43 AM.
 
Old 09-23-2007, 02:33 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
Unless you need it for portability, I would use egrep instead of grep. I would also enclose the regex in single, rather than double, quotes:
Code:
| egrep -oi '.*(exe|bin|msi)'
I also suspect that your regex isn't selective enough -- that at the very least you need to check for a dot before the file type:
Code:
| egrep -oi '.*\.(exe|bin|msi)'

insecurityman,
Is this the right link?
http://ubuntuforums.org/showthread.php?t=533257
I can't seem find your script there. Maybe your Ubuntu forum name & the post# would help.
 
Old 10-20-2008, 04:20 AM   #8
Arfyness
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Wink Call the executable directly.

This is from ages ago, but it's a good question and it's not solved here.

Quote:
Originally Posted by insecurityman View Post

Stranger still, when I try it in a bash shell it executes as it should.

This is on Ubuntu Gutsy, and this is a /bin/sh script - is this a dash issue? Would converting the script to /bin/bash clear things up? Or am I just missing something blatantly obvious?

(The entire script can be found in this thread, by the way)

Thanks for any help!
This is common. Especially for something like 'echo'.

You'll find that most shells have an internal version of very simple commands, like ls, echo, pwd, cd, mkdir, etc... I bet you my paycheck* that you can solve this problem by replacing "echo" in your script with "/bin/echo" to call that binary instead of the shell builtin.

That will ensure that the 'echo' you call is the one described in the manpage.

* disclaimer, I'm currently unemployed.

-Arfy

Last edited by Arfyness; 10-20-2008 at 04:21 AM.
 
  


Reply

Tags
bash, characters, echo, escape, solution, wine


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
awk escape character for colon in string quadmore Programming 2 02-27-2007 04:56 PM
.bashrc is there an escape character for alias? acummings Slackware 11 01-13-2007 10:56 AM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 04:57 PM
Escape character ? juanb Linux - Newbie 2 08-31-2004 10:03 AM
Escape character not working in shell script philipz Programming 1 04-29-2004 09:58 AM


All times are GMT -5. The time now is 10:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration