-1

I have txt file contains names of jar , I want to read this txt file and for each line I want to find and move jar with same name and version number appended (version number is subject to change) from a folder X to folder JarSequence. Text file is jar.txt which has -

  • bbb
  • aaa
  • ccc

now for each line in txt file I want to copy jar in folder JarSequence so that folder JarSequence has -

  • bbb-1.0.1.jar
  • aaa-1.0.1.jar
  • ccc-1.0.1.jar

If possible I also want to maintain same sequence in JarSequence folder as in text file.

Thanks for any help .

techwelll
  • 33
  • 3
  • 1
    ccc has 1.0.2 whereas the others have 1.0.1. Is that intentional? –  Jul 16 '19 at 22:55
  • If intentional, how do you determine whether to append `"-1.0.2.jar"` instead of `"-1.0.1.jar"`?? – David C. Rankin Jul 17 '19 at 00:02
  • @DavidC.Rankin sorry version number is same for each jar(updated the Question).But I need it varies when I move from one build to another but still same for all jars. e.g aaa- ${buildNumber}.jar bbb-${buildNumber}.jar we have buildNumber stored in a variable – techwelll Jul 17 '19 at 09:32

1 Answers1

1
#!/bin/bash

file="your_txt_file"
suffix="-1.0.1.jar"

while IFS= read -r jarfile
do
    #echo "$jarfile"
    mv "$jarfile" JarSequence/"$jarfile"${suffix}
done < $file
  • thanks for the quick reply, will it retain the sequence as well same it is being copied ? – techwelll Jul 16 '19 at 22:57
  • if this just tags on *the same text* (suffix) at the end of each filename, then yes, it will retain the same order. –  Jul 16 '19 at 22:59
  • Always test scripts like this. I usually do: `echo mv "$jarfile" JarSequence/"$jarfile"${suffix}` and execute the script, just to see what would be executed (a sort of 'dry-run'). If that looks good, I remove the echo and run the script. –  Jul 16 '19 at 23:01
  • But note that this will create `JarSequence/ccc-1.0.1.jar`, not `JarSequence/ccc-1.0.2.jar`. In other words, suffix is *static* in this script. If you need the suffix to vary *within* the execution of the script, things become more complex. –  Jul 16 '19 at 23:05
  • thanks @Roadowl, Sorry I have corrected the question every jar will have same version (build number). yes I need suffix to be varied, I need to pass to the script. – techwelll Jul 17 '19 at 08:44
  • suffix is basically release build number which should append to every jar and provided at run time. – techwelll Jul 17 '19 at 09:41