LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   String in Bash Shell (https://www.linuxquestions.org/questions/linux-newbie-8/string-in-bash-shell-383916/)

nguyenzoro 11-17-2005 01:50 AM

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
}

bigrigdriver 11-17-2005 02:57 AM

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.

nguyenzoro 11-17-2005 03:05 AM

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.

bigrigdriver 11-17-2005 04:39 AM

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.

eddiebaby1023 11-19-2005 04:49 PM

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.


All times are GMT -5. The time now is 10:21 PM.