LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-01-2007, 04:51 PM   #1
rossi143
LQ Newbie
 
Registered: Mar 2007
Posts: 5

Rep: Reputation: 0
Shell Script -- read problem!!


Hello! I am writing a program that reads a bunch of arguments from the command line,then read information from a file(passed as one of the arguments) and do some computation. The problem I am facing is when a backslash(\) is present as one of the arguments, suppose $ myprog \ abc xyz,the backslash is completely ignored, for the previous example $1 is "abc" where as it should be "\", I have tried using double-quotes("$1") but it still stays the same. The problem is the same when I am reading from a file the backslash character is completely ignored as if it were a space or a tab.
How can I prevent htis and read "\" as it is? Your help will be greatly appreciated.
 
Old 04-01-2007, 05:14 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by rossi143
The problem I am facing is when a backslash(\) is present as one of the arguments, suppose $ myprog \ abc xyz,the backslash is completely ignored, for the previous example $1 is "abc" where as it should be "\", I have tried using double-quotes("$1") but it still stays the same.
Are you trying to escape the space before "abc" or is the backslash a literal character. In your example, the backslash escapes the space character so the arguments are interpreted by the shell as " abc" "xyz". If it is a literal character, use either single quotes or a double backslash. Try to avoid slashes in filenames if possible. It is one of the evil characters that can cause problems. For example, if a filename can begin with a hyphen, you need to precede the arguments with a double hyphen so that the file isn't taken as an option. Exclaimation points are used by the shell to recall previous shell entries, but just when you are using an interactive shell.

Last edited by jschiwal; 04-01-2007 at 05:16 PM.
 
Old 04-01-2007, 07:22 PM   #3
rossi143
LQ Newbie
 
Registered: Mar 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal
Try to avoid slashes in filenames if possible. It is one of the evil characters that can cause problems. For example, if a filename can begin with a hyphen, you need to precede the arguments with a double hyphen so that the file isn't taken as an option. Exclaimation points are used by the shell to recall previous shell entries, but just when you are using an interactive shell.
Yes backslashes(\) are literal characters.
The problem is that the input for the filenames will be provided by different users, so my program should be ready to handle backslashes at all times. Furthermore, I am trying to read the arguments into local variables( var=$1) so I am not quite sure where to add the backslash character. I face this same problem with backslashes while reading them from a file, read completely ignores them as if they were just a tab or a space.

Last edited by rossi143; 04-01-2007 at 07:23 PM.
 
Old 04-01-2007, 08:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
already replied in another thread
Code:
$ set -- \\ abc
$ echo $1
\
$ set -- '\' abc
$ echo $1
\

Last edited by ghostdog74; 04-01-2007 at 08:57 PM.
 
Old 04-02-2007, 03:17 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
What does a backslash in the input represent? It better not be part of a filename, because windows uses backslashes to denote directories.

To enter a single argument starting with '\ ', either put it in single quotes or use:\\\<space>. The first two backslashes will be reduced to a single backslash. The third escapes the space. Be sure to use double quotes around the variables in the program, if a variable may have any special characters that bash will interpret specially.

Changing the IFS variable to \n is also used to prevent an argument from being broken up, but this will give you one argument containing all the arguments.

Last edited by jschiwal; 04-02-2007 at 03:19 AM.
 
  


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
read from stdin in a shell script bujecas Linux - General 3 07-03-2009 04:46 PM
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
shell script read non-interactive comtmr Linux - General 6 11-01-2006 06:54 AM
shell script that read each line separatly xpucto Programming 6 09-20-2005 08:06 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

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