LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-17-2005, 01:50 AM   #1
nguyenzoro
LQ Newbie
 
Registered: Nov 2005
Posts: 3

Rep: Reputation: 0
String in Bash Shell


Hi everyone,
I have a function that return the value of a key in resource file
EX : MSG001=Message 1
This function will return "Message 1" when called by

message=`getProperty filename MSG001`

But when I'd like to use the result of this function, I got the the following error:

When I echo the result, it is like:
echo $message

I got : $'Message 1\r'

So it will be error when I add this string with another variable

value="message is $message"

output is : message is

What is the problem? Please help me.

getProperty() {
if [ $# -lt 2 ] ; then
echo "$USAGE" ;
exit 1 ;
fi
while read LINE
do
case $LINE in
$2*) replace="$2=";
# replace msgID and = with ""
# ex : replace "MSG0001=" with ""
sbstr=${LINE/"$replace"/""} ;
echo "$sbstr";
break ;;
esac
done < $1
}
 
Old 11-17-2005, 02:57 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Remember that Linux is 'case sensitive'. In your post,you use $message and $Message as though they have the same meaning. They do not. In order for the script to work, you must maintain the integrity of the code throughout the script. In other words, use the same uppercase and lowercase usage.
 
Old 11-17-2005, 03:05 AM   #3
nguyenzoro
LQ Newbie
 
Registered: Nov 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Re: String in Bash Shell

Quote:
Originally posted by nguyenzoro
Hi everyone,
I have a function that return the value of a key in resource file
EX : MSG001=Message 1
This function will return "Message 1" when called by

message=`getProperty filename MSG001`

But when I'd like to use the result of this function, I got the the following error:

When I echo the result, it is like:
echo $message

I got : $'Message 1\r'

So it will be error when I add this string with another variable

value="message is $message"

output is : message is

What is the problem? Please help me.

getProperty() {
if [ $# -lt 2 ] ; then
echo "$USAGE" ;
exit 1 ;
fi
while read LINE
do
case $LINE in
$2*) replace="$2=";
# replace msgID and = with ""
# ex : replace "MSG0001=" with ""
sbstr=${LINE/"$replace"/""} ;
echo "$sbstr";
break ;;
esac
done < $1
}
Sorry,

I mean that after processing this line :
message=`getProperty filename MSG001`
this variable : message has the value :
message=$'Message 1\r'

and when I use this variable to add with another variable, it is not right

for ex :
log_prefix="test"
logfile="$message/$log_prefix"

It was not like : logfile="Message 1/test"

It became : logfile="Message 1\r/test"
So when I created the file with this name, it caused the problem.

Thanks.
 
Old 11-17-2005, 04:39 AM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Now I have a better understanding of your problem. The variable Message 1 has a space in the variable name. Such spaces will cause problems.

There are two ways to cope with those spaces.

One) replace the spaces with the underscore character: _. Something like: Message_1

Two) escape the space with the escape character \ immediately after the last character of the first word of the variable name, followed by a space, then the message number. Something like 'Message\ 1'

I think the underscore will work more reliably. It is also easier to read.

The reason why this is so is that bash will try to interperet each part of the variable name as a seperate command or parameter. You could also try enclosing the variable name in quotes to prevent such interpretation. But, in bash, it makes a differernce if the quotes are single ' ' or double " ", how bash will read the variable name. The easiest solution: use the underscore instead of the space in variable names.

Last edited by bigrigdriver; 11-17-2005 at 04:44 AM.
 
Old 11-19-2005, 04:49 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Another of your problems is your "Message 1" string has a carriage return character on the end of it, probably because you've got a Windows format text file that has /r/n line endings. dos2unix the files
before you extract the values from it.
 
  


Reply


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
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM
Bash problem, no executing of string ChoKamir Programming 3 01-31-2005 04:50 AM
string shell command? minm Linux - Newbie 10 08-19-2004 10:09 AM
BASH string expansions Barbarian Programming 2 11-21-2001 10:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:40 AM.

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