-1

Currently, working with rhel. Need to create a script that can use a mapping file (source and target need to be dynamic). What file type should be used for the mapping file (.csv, .txt, .json)?

  • End directory in a slash e.g. src/
  • -r can be used against a file e.g. cp -r src/file.fil tgt/
  • script will be kicked off by the spec file and files will be copied during build
  • source and target need to be dynamic

Script example

cp -r src/file.fil tgt/

Mapping file

<src>\t<tgt>\n e.g. src/file.fil \t tgt/ \n OR 'src/file.fil'\t'tgt/'\n

phil o.O
  • 406
  • 2
  • 4
  • 20
  • what is your shell? bash? from the man page -r is for recursive copying. – LMC Jan 16 '18 at 17:53
  • Bash. Yes, I know. I'm trying to figure out the file type for the mapping file that the bash script will read from. – phil o.O Jan 16 '18 at 17:59
  • What this has to do with the `rpmbuild` and `rhel` tag ? – iamauser Jan 17 '18 at 21:10
  • Tags have been removed. @iamauser – phil o.O Jan 17 '18 at 21:13
  • Maybe you should generate a bunch of `install` commands and write them into your `.spec` file's `%files` section by way of a script? It's not too uncommon to have a skeleton `.spec.in` template and then generate the final deliverable from that programmatically, perhaps by way of a simple `sed` script. – tripleee Jan 18 '18 at 05:23
  • @tripleee this sounds really great! My problem is finding resources to uncover this approach. This is the first time I'm hearing about the `.spec.in` template. No search results populate in the Fedora docs for `.spec.in`. Where can I read up on this? – phil o.O Jan 18 '18 at 13:24
  • @phil-o-o Here's an starting point https://stackoverflow.com/questions/2531827/what-are-makefile-am-and-makefile-in – LMC Jan 18 '18 at 18:10

1 Answers1

0

xargs can be used for that

xargs --arg-file your-file.txt cp

file should contain lines like

file1 tgt/
file2 tgt/

or a for loop reading from a file with just source file names provided that your target is constant

for f in $(cat yourfile.txt); do
    cp "$f" tgt/
done

or even

while read src tgt; do
    cp "$src" "$tgt"
done < yourfile.txt
LMC
  • 10,453
  • 2
  • 27
  • 52
  • this does not work as the `tgt` directory needs to be dynamic. `file2` may need to go to a different file path. – phil o.O Jan 17 '18 at 20:24
  • so your file should contain that mapping, file1 tgt1/ file2 tgt2/ and so on. Correct? – LMC Jan 17 '18 at 20:37
  • Downvote; still broken, as seen here: https://stackoverflow.com/questions/48308990/xargs-is-incorrectly-copying-my-src-dir-to-one-tgt-directory – tripleee Jan 18 '18 at 04:36
  • The loops are basically the way to go but both attempts suffer from newbie problems; run them through http://shellcheck.net/ for details. – tripleee Jan 18 '18 at 04:37
  • newbie?! @tripleee – LMC Jan 18 '18 at 15:17
  • Quoting errors are indicative of limited experience with shell programming. Many developers never get past the copy/paste stage with the shell, even if they manage to write serious applications in other languages. I encourage you to try the site I linked, and maybe review https://mywiki.wooledge.org/BashPitfalls which has many forward links to a FAQ etc. – tripleee Jan 18 '18 at 15:54
  • The Unix shell features many arcane idiosyncrasies which are based in the long, organic development of the Bourne family of shells; learning to recognize at least the most common ones will help you realize just how many poorly-written shell scripts there are even in critical production systems. – tripleee Jan 18 '18 at 15:55
  • I appreciate your comments, mostly because they move apart from the 'newbie' approach. I've been writing bash scripts like 15 years and read ABS guide completely. Next time if someone misses a couple of quotes don't rush to call him newbie, it's not enough evidence. – LMC Jan 18 '18 at 16:46