LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script - variables & arrays question (https://www.linuxquestions.org/questions/linux-software-2/bash-script-variables-and-arrays-question-231291/)

rblampain 09-16-2004 06:29 AM

bash script - variables & arrays question
 
I need to extract, rename and move a few files from a cascade of directories and subdirectories. Some names of files are known, others have to be found with the script.

It seems at some stage of the script, I need to save a directory name in a variable or a number of directory names in an array, then 'cut' a part of the name(s) and 'paste' it in following commands in order to rename and move the files to be saved to the top directory before removing everything else.

Found good tutorials on the net but no solution to this particular problem. Any help on what to include in script or hint about suitable 'howto' most welcome.

System is Red Hat 9
Here is the layout:
(all files relative to /home/user/job01/)
01 ~ no1/ -->keep
02 z_files/
03 important.html -->save as "no1/main.html"
04 a_dat/
05 important.gif -->save as 'no1/main.gif"
06 001_files/
07 sub_files/
08 some_da/
09 important.html -->save as "no1/001.html"
10 some_dat/
11 main.png -->save as "no1/001.png"
12 002_files
13 etc
14 100_files
15 ~no2/
16 etc
17 ~no300/

directory named 'no1' (on line 01) number about 300, all these names are known and are placed in an array

files to be saved on lines 03, 05, 09 and 11 are unique and easy to identify and process

directory named 001_files (on line 06) are unknown names but have a common ending (_files), the number of directories is also variable, the variable part of the name (001 in this case) will be used to rename the files to be saved

it is possible to issue the command "cd *_files from the ~no1/ directory which will enter any of these files, then save the first digits of the name ('001' in the example above) which can then be 'pasted' to the files to be saved from this directory, then remove this directory, then re-issue the same command that will enter the next random "*_files" directory and so on untill there is no more "_files" directory.

How to use and parse the portion of the name is what I don't know.

chrism01 09-16-2004 09:54 AM

You might find this useful:
name_start=`echo 001_files|cut -d'_' -f1`
$name_start now has '001'

as you can see, this will give you the leading digits/string. Note that's backquotes '`' around the whole cmd in the first line, not single quotes.
You then say something like:
cd 001_files
cp thisfile1 /newdir/${name_start}_thisfile1

HTH

rblampain 09-22-2004 01:28 AM

thanks to chrism01

Shortly after posting my question I had the script running using the wildcard option. It works well but it leaves a feeling
of uncertainty. I tried your elegant suggestion but I found my understanding of bash is far too little to adapt it safely. I also found I will need to use bash more than anticipated initially and therefore I will get a good book to help solve the problems as they come. Thank you again.

JZL240I-U 09-22-2004 08:08 AM

For some 500+ pages of bash scripting look here: http://www.tldp.org/guides.html#abs

rblampain 09-25-2004 09:57 AM

Thanks to JZL240I-U also.
I've downloaded the stuff and it looks like I'll find a lot of answers in that.
I should pay a bit more attention to the LDP.
Cheers


All times are GMT -5. The time now is 05:55 AM.