0

I'm new to writing scripts in Linux.

I have input texts files where each line carries two strings (file paths) separated by one or more blank spaces.

I am writing an automated script file that passes the file listed in the first string (path), copies that file to the second string (path), and repeat for every line in each input file.

I have the .zip file containing all files listed in the input file.

It looks like this:

/path/to/file.x   /different/path/to/file.x
/path/to/file2.x  /different/path/to/file2.x

etc.

Where file.x will be passed then copied from the separate .zip file to different path.

Each input file comes with its own .zip, has different number of lines and different paths indicated.

I thought I would try running the script:

~$ ./script.sh files.zip map.txt

then writing for the script:

zipfile = "$1"
map = "$2"
externalprogram "$zipfile" "$map"

while IFS='' read -r line; 
do
    VAR_STR=$line
    aLeng=${#VAR_STR[@]}
    IFS=/ read -a arr <<< "$VAR_STR"
    FILE="${arr[${aLeng}]-1}"

    cp <path to zip file>/$FILE /different/path/
done < "$map"

Obviously, it's fairly complex and I don't know how to pass the second path in the same line as a variable.

Any input is appreciated. thank you.

EDIT: I meant to put map.txt where I mention running the script. Also, I should have put It's fairly complex to me. A lot of learning in Linux I have to do ^_^

closed as unclear what you're asking by muru, G-Man, Isaac, Archemar, h3rrmiller Feb 9 '18 at 19:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Just realized IFS=/ read -a arr <<< "$VAR_STR" should have come before aLeng=${#VAR_STR[@]}. I know there are other things I'm missing. – obidyne Feb 8 '18 at 5:44
  • So your input text file is also zipped? And you have to extract files from the files.zip to some other path? Is /different/path/to/ the same for all files in the same input file? – muru Feb 8 '18 at 5:51
  • I will be using two input files: the .zip file that contains the actual files to copy/map, and the text file that shows 2 path strings on each line, where the 1st path lists what file to copy/map, and the 2nd path is the destination for that file. – obidyne Feb 8 '18 at 16:40
2

Since your question is an open-ended request for input rather than a specific question, here are some comments.

  1. You seem to be under-utilizing the read command. Review the man page documentation and see how you can trivially use it to read multiple fields to unique variables in one go, eg. read path1 path2.
  2. Why are you assigning VAR_STR=$line? It seems to me that you could operate directly on $line.
  3. Your method of trimming the path out of the second field is clever, but bash and most other shells have a straightforward, simpler, and more efficient method, using the # idiom to remove a prefix pattern from a variable, eg. FILE=${path2##*/}. Again, see the man page for documentation of other cool things the shell can do natively very efficiently.
  4. Standardize your personal naming convention for bash variables; either make them all lower case or all upper case, but don't mix.
  5. Your concluding statement beginning "Obviously..." was not obvious at all to me, in that I have no idea what you mean.

Not the answer you're looking for? Browse other questions tagged or ask your own question.