The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed and awk backslash characters potro Shell Programming and Scripting 7 05-19-2008 09:34 AM
How to handle backslash in grep string rajbal UNIX for Advanced & Expert Users 6 06-18-2007 09:22 PM
How ro handle backslash character in grep? rajbal Shell Programming and Scripting 1 06-18-2007 09:19 PM
ignoring backslash while executing command agalkin Shell Programming and Scripting 6 11-16-2005 06:04 PM
Adding a backslash to users' input netguy Shell Programming and Scripting 13 04-30-2004 06:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-08-2004
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
backslash issues

Hi,

I have a script which looks through an input file and takes data from the file to use within the script.

Everything works fine until the script reads the item \windows\directory\structure\ from the input file into a variable.

As unix sees the backslash as an escape character, the variable, when echoed shows windowsdirectorystructure when the literal \windows\directory\structure\ is required.

I tried excaping the escape characters in the input file so that the variable included them, ie putting \\windows\\directory\\structure\\ into the input file. This came back with the same incorrect result too. I also tried \\\windows\\\directory\\\structure\\\ just in case the shell interpreted the variable another time but this showed the same result too.

I also tried using '\windows\directory\structure\' including single quotes in the input file but the variable literally is '\windows\directory\structure\' including single quotes.

If I try assigning the variable on the command line rather than from within the script, I get the desired result. ie

var='\windows\directory\structure\'
echo $var
\windows\directory\structure\

Does anyone have any advice on how I can get the variable in the script to equal \windows\directory\structure\ ?

Many thanks
Helen
  #2 (permalink)  
Old 07-08-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
If I have a file, say, files.txt
Code:
C:\\path\\to\\first\\windows\\file
C:\\path\\to\\second\\windows\\file
and run the following script against it
Code:
#!/bin/sh

while read var
do
    echo $var
done < files.txt

exit 0
all is well, and the output is:
Code:
$ ./test.sh
C:\path\to\first\windows\file
C:\path\to\second\windows\file
Cheers
ZB
  #3 (permalink)  
Old 07-09-2004
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
Hi,

The input file I use is slightly different and similar to the following:

field1,field2,field3,C:\\path\\to\\first\\windows\\file,field5
field1,field2,field3,C:\\path\\to\\second\\windows\\file,field5

The script is similar to this:

#!/bin/sh

while read var
do

dir=`echo $var | cut -d , -f 4`
echo $dir

done < files.txt

exit 0

When running it i get:

$test.sh
C:\path o
irst\windows
ile
C:\path o\second\windows
ile
$

Thanks for your help
Helen
  #4 (permalink)  
Old 07-09-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Give this a try

Code:
#!/bin/sh
     
while read var
do
     
    dir=`echo $var | cut -d , -f 4 | sed 's/\\\\/\\\\\\\\/g'`
     
    echo $dir
     
done < files.txt
    
exit 0
Change your input file to
Code:
field1,field2,field3,C:\\\\path\\\\to\\\\first\\\\windows\\\\file,field5
field1,field2,field3,C:\\\\path\\\\to\\\\second\\\\windows\\\\file,field5
It's those backslashes that are causing the problem, and need to be sed'ed - and we need to backslash escape the backslashes for each stage of evaluation - and then substitute the correct number of backslash escaped backslashes!

This nightmare only seems to rear it's ugly head under really old original Bourne shells. I've tested my original solution under bash and ksh and it works fine. The original bourne shell on HP-UX (/usr/old/bin/sh) shows this problem, and requires backslash-city as above.

Peace
ZB
  #5 (permalink)  
Old 07-09-2004
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
Hi ZB

Cool, that works a treat. I'm just glad there isn't a tax on backslashes!! If you're ever around Sheffield, we owe you a drink.

Thanks so much
Helen
  #6 (permalink)  
Old 07-09-2004
anarchie anarchie is offline
Registered User
  
 

Join Date: May 2004
Location: Hawaii
Posts: 37
If you're using cygwin, it has always let me get away with regular slashes instead of backslashes. Even with "c:/Program Files" and the like.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0