1

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

MAS1
  • 1,691
  • 6
  • 19
  • 22

3 Answers3

4
sourcePaths=( $(grep source_path myfile |cut -d= -f2) )
destPath=`grep destination_path myfile |cut -d= -f2`

$sourcePaths is 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
  • 266,786
  • 75
  • 396
  • 414
1

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
  • 90,870
  • 19
  • 190
  • 224
0
    #!/bin/sh
#path to config file
CONFIG_FILE=/tmp/conf.log
#check if file exist
if [[ -f $CONFIG_FILE ]]; then
#use source filename or .file name
    . $CONFIG_FILE
#test variable
echo "source is $source"
echo "dest is $dest"
fi
annee
  • 1