Hello Monks!
I'm trying to create a perl script that creates bash script "on the fly" (bare with me!) that will mimic the system in another environment (inside a container).
I have an array of paths. Those paths could be their hard paths or links. The link does not have to be the ending file/directory in the path - it could be some part on the way.
For example if I have path /a/b/c and the there is a link /a/b -> /d/e.
Also those paths does not have to be realpaths (they could contain .. and . and other combinations).
Based on those paths, I want to mimic the system I have. The input of my perl script is an a file of paths (that I read into an array) and the output is a bash script that will mimic the system.
By mimic I mean that it will do the following things:
1. Create the same directory hierarchy.
2. Copy the files.
3. Create the same links.
For that I can do:
1. I can use mkdir -p to create the full hierarchy based on the path.
2. I can use scp/rsync for copying (as it's inside container).
3. I can use ln -s to create the links.
Note that I want to create directories before copying the files. I don't want to copy the directories because those directories could have files or other directories that are not located inside the array of paths.
But my question is not about this part. My question is what is the best way to parse the array of paths, find out which one of them are paths, files and links and then use it for each one of those three stages. I thought of having a hash structure and each time load a path into that structure. Sort of "mapping" the system paths into a hash structure. For each path I will have another field which will say the type of it - file, link, directory. For link I will have to keep the source and destination. Then I could iterate over the hash and create three arrays - one for mkdir, one for ln, and one for copy. After that, I can iterate over the mkdir array and print into the bash script file:
mkdir -p <path-to-dir>
Iterate over the files array and print into the bash script file:
scp user@machine:<path-to-file> <path-to-file>
Iterate over the links array and print into the bash script file:
ln -s <target> <link-name>
Between reading the paths file and creating the bash script itself I have a black box in my mind which I'm trying to implement and the question is about that black box. If there was a module which you could just "load" the paths into it and then query for what you need (like give me all of the directories...) it could be awesome, but I could not find one. What would be the best strategy here?
Hope my question makes sense. If you want me to explain some part, I will glad to.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.