How do I pass a file in a string path and copy that file to another path on that same line? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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 ^_^







share|improve this question














closed as unclear what you're asking by muru, G-Man, Isaac, Archemar, h3rrmiller Feb 9 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.
    – Jason
    Feb 8 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 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.
    – Jason
    Feb 8 at 16:40















up vote
0
down vote

favorite












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 ^_^







share|improve this question














closed as unclear what you're asking by muru, G-Man, Isaac, Archemar, h3rrmiller Feb 9 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.
    – Jason
    Feb 8 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 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.
    – Jason
    Feb 8 at 16:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 ^_^







share|improve this question














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 ^_^









share|improve this question













share|improve this question




share|improve this question








edited Feb 8 at 16:55

























asked Feb 8 at 5:42









Jason

174




174




closed as unclear what you're asking by muru, G-Man, Isaac, Archemar, h3rrmiller Feb 9 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.






closed as unclear what you're asking by muru, G-Man, Isaac, Archemar, h3rrmiller Feb 9 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.
    – Jason
    Feb 8 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 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.
    – Jason
    Feb 8 at 16:40

















  • Just realized IFS=/ read -a arr <<< "$VAR_STR" should have come before aLeng=$#VAR_STR[@]. I know there are other things I'm missing.
    – Jason
    Feb 8 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 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.
    – Jason
    Feb 8 at 16:40
















Just realized IFS=/ read -a arr <<< "$VAR_STR" should have come before aLeng=$#VAR_STR[@]. I know there are other things I'm missing.
– Jason
Feb 8 at 5:44




Just realized IFS=/ read -a arr <<< "$VAR_STR" should have come before aLeng=$#VAR_STR[@]. I know there are other things I'm missing.
– Jason
Feb 8 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 at 5:51




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 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.
– Jason
Feb 8 at 16:40





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.
– Jason
Feb 8 at 16:40











1 Answer
1






active

oldest

votes

















up vote
2
down vote













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.





share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    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.





    share|improve this answer


























      up vote
      2
      down vote













      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.





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        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.





        share|improve this answer














        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.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 8 at 6:59









        muru

        33.4k577142




        33.4k577142










        answered Feb 8 at 6:25









        user1404316

        2,314520




        2,314520












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?