backslash issues


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers backslash issues
# 1  
Old 07-08-2004
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  
Old 07-08-2004
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  
Old 07-09-2004
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  
Old 07-09-2004
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! Smilie

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  
Old 07-09-2004
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
HelenSmilie
# 6  
Old 07-09-2004
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not to Escape Backslash in script

Hello Folks, I am looking for a bash script which check the ftp server connection status and transfer success/failure also final stage is like if the transfer/connection failed, i should get an email. First stage - checking for login success or not #!/bin/bash HOST='your.ftp.site'... (3 Replies)
Discussion started by: Thala
3 Replies

2. Shell Programming and Scripting

[Solved] Disappearing backslash

Let's say I have a text file called process.out that contains: cn=long\, ann,cn=users cn=doe\, john,cn=users I need to have the following appended in the beginning ldapdelete -h $OIDHOST So the final output looks like: ldapdelete -h $OIDHOST "cn=long\, ann,cn=users" ldapdelete -h... (4 Replies)
Discussion started by: exm
4 Replies

3. UNIX for Advanced & Expert Users

grep for a backslash question

Why does this work when grepping for a backslash? grep '\\' .bash_history grep "" .bash_historyWhy does this not work when grepping for a backslash? grep "\\" .bash_historyI know this works works but just don't understand why I need 4 backslashes when using double quotes. grep "\\\\"... (7 Replies)
Discussion started by: cokedude
7 Replies

4. UNIX for Dummies Questions & Answers

Weird behavior of backslash, please help!!

Hi I am getting absurd behavior of escape character in echos as followed:oinlcso003{arsadm} #: echo "\as shdd" \as shdd oinlcso003{arsadm} #: echo "Well, isn't that \"special\"?" Well, isn't that "special"? oinlcso003{arsadm} #: echo "Well, isn't that \special\?" Well, isn't that \special\?... (3 Replies)
Discussion started by: nixhead
3 Replies

5. Shell Programming and Scripting

Echo backslash

If I echo "\\" I get a backslash returned ~$ echo "\\" \ Why doesn't this work: string=`echo "\\"` echo $string I get the error message: bash: command substitution: line 1: unexpected EOF while looking for matching `"' bash: command substitution: line 2: syntax error: unexpected end... (2 Replies)
Discussion started by: locoroco
2 Replies

6. UNIX for Dummies Questions & Answers

./configure backslash questions

Hello all. I am going to try my hand at compiling tarballs rather than installing packages. I have a pretty good understanding of the process and have even compiled/installed Top from source. But that was an easy install, I want to try something more complex using the various configure... (6 Replies)
Discussion started by: RobertSubnet
6 Replies

7. Shell Programming and Scripting

How to use backslash and variables in sed

I have a line that contains backslashes in which I want sed to substitute text with variables. The line; \\s008\2033330user$ I want to change this in \\s008.ourschool.com\2033330user$ I now use this script: USER=2033330user sed 's/\\'"$USER"'/.ourschool.com\\'"$USER/" This doesn't... (3 Replies)
Discussion started by: Tubbie
3 Replies

8. Shell Programming and Scripting

double backslash in ksh

Hi I need the "\\hello" (without double quotes) to be written to a file. echo "\\\\hello" >file is working under bash shell but not working under ksh shell (gives only one / in the output) Please advise. TIA Prvn (4 Replies)
Discussion started by: prvnrk
4 Replies

9. UNIX for Dummies Questions & Answers

Escaping backslash

I have a variable containt something like this, c:\mask\mask. How can I escape "\" in the values? I want the value as it it. (9 Replies)
Discussion started by: swmk
9 Replies

10. UNIX for Dummies Questions & Answers

Backslash in find command

When I run my script in debug mode I see that \ is not getting executed as part of command. It is being treated as escape character. find ${DATABASE_PARAM_PATH} -mtime +${RETENTION_PERIOD} -exec rm -rf {} \; Command execution output in debug mode : find... (1 Reply)
Discussion started by: findprakash
1 Replies
Login or Register to Ask a Question