LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-31-2012, 10:24 PM   #1
rohithsharma
LQ Newbie
 
Registered: Sep 2010
Location: bangalore
Posts: 5

Rep: Reputation: 0
Smile How to read properties valuues in for loop..?


Hi guys,

Can someone help me for clearing below doubt please.Thanks in advance

"test.property" file contains
TEST_VALUES=A,B,C,D

"test.sh" test script file contain

source test.property
for value in $TEST_VALUES
do
echo Value:$value
done

Here , I want output as
Value:A
Value:B
Value:C


But I am getting output as
Value:A,B,C
 
Old 03-31-2012, 10:38 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Can test.property be changed? The "for var in $array" syntax separates entries by spaces. Since your entries are separated by commas, you have three choices:

1) Modify test.property to look like TEST_VALUES="A B C D"
2) Modify your shell to separate by commas instead of (or in addition to) spaces
3) Use a different method of parsing the entries in TEST_VALUES
 
Old 04-01-2012, 12:17 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The behaviour is expected. There is no reason why for value in $TEST_VALUES bash should split the value of TEST_VALUES at each comma. If it did then any variable that happens to have commas in its value would be split!

This works, using shell parameter expansion to split $TEST_VALUES at the commas as desired. It will break if the strings between commas include spaces. That could be fixed by creating an array out of $TEST_VALUES but would be more complex.
Code:
#!/bin/bash

source test.property

for value in ${TEST_VALUES//,/ }
do
    echo Value:$value
done
Note: I have presumed that you did not want to drop the "D" value as stated in the OP.
 
Old 04-01-2012, 11:05 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Yes, the problem here is due to word splitting. Since there are no spaces in the string, there's no splitting.

Two more options for you to consider.

1) Change the IFS value to a comma, so that it splits on them.

Code:
TEST_VALUES=A,B,C,D
IFS=','

for value in $TEST_VALUES ; do
	echo "Value:$value"
done
2) Use an array instead.

Code:
TEST_VALUES=( A B C D )


for value in "${TEST_VALUES[@]}" ; do
	echo "Value:$value"
done
I personally recommend #2, if possible. Arrays are designed for storing lists of values. If necessary, you can use IFS or catkin's technique above to split a scalar variable into an array.

Code:
TEST_VALUES=A,B,C,D

TEST_VALUES=( ${TEST_VALUES//,/ } )

#IFS=','			#alternately, using IFS
#TEST_VALUES=( $TEST_VALUES )

for value in "${TEST_VALUES[@]}" ; do
	echo "Value:$value"
done
See here for more string manipulation techniques:

http://mywiki.wooledge.org/BashFAQ/100]string manipulation


Finally:

QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

( The above examples actually demonstrate one of the exceptions, but you should definitely quote the "echo" output variable. )


Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).

Many people also think that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.

Code:
for var in fee fai foo fum ; do

	if [[ "$var" == "foo" ]]; then
		echo "Found 'foo'."
	fi

done

Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
 
Old 04-10-2012, 01:27 AM   #5
rohithsharma
LQ Newbie
 
Registered: Sep 2010
Location: bangalore
Posts: 5

Original Poster
Rep: Reputation: 0
Smile

@suicidaleggroll,@catkin,@David the H

Thaks for your valuable knoledge sharing regarding shell string manipulation.

Thanks & Regards
Rohith Sharma K S
 
  


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
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
[SOLVED] Trying to read a file with a while loop. sinderone Programming 5 04-26-2010 02:37 AM
java: SQL query falsely read from properties file kpachopoulos Programming 0 01-08-2008 01:38 PM
while READ loop problem caps_phisto Linux - General 3 02-05-2007 09:58 AM

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

All times are GMT -5. The time now is 05:03 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