views:

47

answers:

2

hi all, i am new in unix. i have to write a shell script which read a file line by line and store it in a separate variable. my text file contents multiple source path and one destination path. something like this::

source_path=abc/xyz
source_path=pqr/abc
desination_path=abcd/mlk

number of source path can vary. I dont have much hands on experience in Unix. Can anyone help me to achieve this one. It will be very helpfull.

Thanks in advance

+1  A: 

A bit hacky but this should work:

old_IFS="$IFS"
IFS="="

while read left right ; do
  echo "Left side: $left"
  echo "Right side: $right"
done < $input_file

IFS="$old_IFS"

If you want to get rid of the " in $right, you might do something like right_content=$(sed 's|"\(.*\)"|\1|' <<<$right). The <<<$right is almost like doing echo $right, and the sed command will remove leading and trailing " (if no quotation marks are present, the string will simply be passed as-is).

DarkDust
@Darkdust::thanx, but do you think that it will really work for multiple source path and store it in a different variable
MAS1
@Darkdust::its not necessory that source_path will contents "".
MAS1
+2  A: 
sourcePaths=`grep source_path myfile |cut -d= -f2`
destPath=`grep destination_path myfile |cut -d= -f2`

now $sourcePaths is a single variable but you can think of it as an array of source_paths. You don't need to store each source_path in a separate variable.

You can loop over the array and do what you want with each source_path. For example:

for i in $sourcePaths
do
    echo $i
done
dogbane