0

I have a file in custom_target.config. I have a shell script that is going to read through this config file. The config file is split up by the name of the file and the target directory I am trying to move it to. I was wondering what the best way is to split line in the file and start moving the files to the respective locations.

EXAMPLE_FILE /home/user/example
EXAMPLE_FILE_1 /home/user/example
EXAMPLE_FILE_1 /home/user/example/subfolder

Attemp so far:

while IFS='' read -r line || [[ -n "$line" ]]; do
    for element in $line
    do
      echo $element
    done
    #echo "Text read from file: $line"
done < "$1"
3
  • Feels like this should be over on the *nix SO. I have just been looking at the same thing :) unix.stackexchange.com/questions/111397/… Commented Mar 21, 2018 at 16:58
  • Do you want to move EXAMPLE_FILE to /home/user/example? Try while IFS= read -r file dir; do echo mv "${file}" "${dir}"; done < "$1" and remove echo` when it is what you want. Commented Mar 21, 2018 at 17:12
  • 1
    Be nice to your scripts: ensure that you feed them valid POSIX text files (that end with a newline) instead of cluttering them with hacks like || [[ -n $line ]]. Commented Mar 21, 2018 at 17:13

2 Answers 2

1

Let read do the word splitting instead of trusting $line not to do anything odd.

while read -r key value; do
  echo "KEY: $key"
  echo "VALUE: $value"
done < "$1"
Sign up to request clarification or add additional context in comments.

Comments

0

you can use Awk:

copy files

  `awk '{printf "cp "$1" "$2}' custom_target.config`

move files

  `awk '{printf "mv "$1" "$2}' custom_target.config`

print a strings to be executed: (cp file_name path)

$1 - first field separated by space (file name)

$2 - second field separated by space (path name)

awk '{printf "cp "$1" "$2}' custom_target.config

use `` for execute the output strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.