Jump to content

Backslash in Bash scripts [solved]


Recommended Posts

I am writing a Bash script to convert a text file from one format to another. The input file makes extensive use of the "\" character. I want to retain these characters unchanged.

 

As far as I can tell the "\" is acted upon as soon as a line is read from a file. This means that I do not get the opportunity to escape it or temporarily replace it with another character.

 

One solution is to use a text editor to replace the "\" with a temporary character, run the script and then use the editor to replace the "\". Howver this is a messy way of solving the problem.

 

Is there some way I can tell Bash to ignore "\" as an escape character? Maybe I can run sed at the beginning of and end of the script?

Link to comment
Share on other sites

I'm not sure what you're doing, but could you use sed to replace '\' with '\\' in the input file? This should read in as a '\' and you wouldn't then need a second replace at the end.

 

This, of course, in case you can't actually suppress '\' expansion on reading.

 

Also, have you considered using one of the specialised file manipulation languages (awk, perl) instead of a bash script?

Link to comment
Share on other sites

I'm not 100% sure of what you are doing or how you are managing it, but if you cat the file in, the \ remain intact:

backslash.txt:

\test
\\test
\\40
\\\3
\13

 

backslash.sh:

#!/bin/bash

test="$(cat <backslash.txt)"

echo "$test"

 

output:

\test
\\test
\\40
\\\3
\13

 

I am but a lowly bash apprentice...aru or theYinYeti will come along and fix everything.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Thanks for the quick replies.

 

I am converting Proxoimitron .cfg files to Proximodo filters.txt format. Until I started this project the longest script I had written was three or four lines so I am ging through a steep learning curve.

 

I have not yet considred using awk. I am aware of its existance but first I want to learn to write bash scripts and use regular expressions (-;

 

I have carried out two tests:

 

#exec < $backslashtest

#read Line

echo line

 

This removed one \.

 

I then tried the CAT example and it did not remove any \

 

Therefore I have two options - either use cat instead of read or use sed to insert an extra \.

Link to comment
Share on other sites

To be clear, the 'cat' way could be done line by line, as well:

I will try this as well.

 

I am still coming to grips with the Bash syntax. cat < backslash.txt is a command. Therefore could the test= line also be written using backquotes:

 

test=`cat < backslash.txt`

 

or does that have a different meaning?

Link to comment
Share on other sites

http://www.tldp.org/LDP/abs/html/commandsub.html

 

Hell and bat's do's... that's amazing. I keep having to remind myself that bash scripts are NOT just windows batch files on steroids, but proper programs in their own right.

 

Looks like the "test=$(<backslash.txt)" syntax will answer the original question, then.

Link to comment
Share on other sites

Hello,

 

I'd be glad to help, but I don't get what it is you want to do. As was already said, awk and sed are good tools for manipulating text files, and bash can do usefull stuff too. But in any case, '\' characters shouldn't give you trouble. In what circumstances exactly do you have the problem of '\' disapearing?

 

Yves.

Link to comment
Share on other sites

Problem solved!

 

I did what I should have done at the start. I applied the RTFM principle, went to the GNU site and looked up the official Bash documentation.

 

read -r Line

 

treats \ as a normal rather than an escape character.

 

It is interesting what you discover when you read the complete documentation B) . For example -d delim specifies that delim be used to terrminate the input line rather than newline. I am sure that that could be useful somewhere ;)

Link to comment
Share on other sites

Yes, yes, yes, and so on. :)

I actually like reading manuals. There's so much to learn inside. bash, awk, and sed are great tools, really. And regular expressions are something you can't live without once you're comfortable with them.

 

Yves.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...