tricky file script

tricky file script

Post by zae.. » Fri, 18 Nov 2005 06:04:07



i have a list of directories, one per line in a file mainlist.txt
each directory has its own directories(any number) in a file called
44.txt, if for example the directory was called 44

i want to be able to cd to each directory in mainlist.txt, and then
load a seperate list for the directory and then change into the
directoryies and do something...cd back out and repeat.

for example in mainlist.txt:

40/
41/
42/
43/
44/

in ~ i have 40.txt, 41.txt, 42.txt, 43.txt and 44.txt

i want to be able to, using mainlist.txt, when i am in 40/ load 40.txt
and cd to each directory and do something, cd back out cd to 41/ load
41.txt etc..

an example of what 44.txt would have inside

fruit/
chairs/
disco/

i hope ive explained that well enough, everything ive tried has not
worked..., any suggestions most welcome

 
 
 

tricky file script

Post by Michael Heimin » Fri, 18 Nov 2005 06:21:16



Quote:> i have a list of directories, one per line in a file mainlist.txt
> each directory has its own directories(any number) in a file called
> 44.txt, if for example the directory was called 44

Still don't get it, wouldn't it be easier if you directly post
the question of your homework assignment?

[..]

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)

#bofh excuse 126: it has Intel Inside

 
 
 

tricky file script

Post by Chris F.A. Johnso » Fri, 18 Nov 2005 06:21:40



> i have a list of directories, one per line in a file mainlist.txt
> each directory has its own directories(any number) in a file called
> 44.txt, if for example the directory was called 44

> i want to be able to cd to each directory in mainlist.txt, and then
> load a seperate list for the directory and then change into the
> directoryies and do something...cd back out and repeat.

> for example in mainlist.txt:

> 40/
> 41/
> 42/
> 43/
> 44/

> in ~ i have 40.txt, 41.txt, 42.txt, 43.txt and 44.txt

> i want to be able to, using mainlist.txt, when i am in 40/ load 40.txt
> and cd to each directory and do something, cd back out cd to 41/ load
> 41.txt etc..

> an example of what 44.txt would have inside

> fruit/
> chairs/
> disco/

> i hope ive explained that well enough, everything ive tried has not
> worked..., any suggestions most welcome

    What part of the script is giving you problems?

    To operate on each directory from a file, use something like this:

while IFS= read -r dir
do
  (  ## Use a subshell so that the loop remains in the same directory
    cd "$dir"
    : do something
  )
done < mainlist.txt

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

tricky file script

Post by zae.. » Fri, 18 Nov 2005 21:29:22


its not a homework assignment...sheesh
i can do it fine for the first part, in fact I had something exactly
like what you have their, the problem was the second part, the subshell
to be exact..., im not too sure how to do that?


> > i have a list of directories, one per line in a file mainlist.txt
> > each directory has its own directories(any number) in a file called
> > 44.txt, if for example the directory was called 44

> > i want to be able to cd to each directory in mainlist.txt, and then
> > load a seperate list for the directory and then change into the
> > directoryies and do something...cd back out and repeat.

> > for example in mainlist.txt:

> > 40/
> > 41/
> > 42/
> > 43/
> > 44/

> > in ~ i have 40.txt, 41.txt, 42.txt, 43.txt and 44.txt

> > i want to be able to, using mainlist.txt, when i am in 40/ load 40.txt
> > and cd to each directory and do something, cd back out cd to 41/ load
> > 41.txt etc..

> > an example of what 44.txt would have inside

> > fruit/
> > chairs/
> > disco/

> > i hope ive explained that well enough, everything ive tried has not
> > worked..., any suggestions most welcome

>     What part of the script is giving you problems?

>     To operate on each directory from a file, use something like this:

> while IFS= read -r dir
> do
>   (  ## Use a subshell so that the loop remains in the same directory
>     cd "$dir"
>     : do something
>   )
> done < mainlist.txt

> --
>    Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
>    Shell Scripting Recipes:     |  My code in this post, if any,
>    A Problem-Solution Approach  |          is released under the
>    2005, Apress                 |     GNU General Public Licence

 
 
 

tricky file script

Post by Chris F.A. Johnso » Sat, 19 Nov 2005 00:39:53





>> > i have a list of directories, one per line in a file mainlist.txt
>> > each directory has its own directories(any number) in a file called
>> > 44.txt, if for example the directory was called 44

>> > i want to be able to cd to each directory in mainlist.txt, and then
>> > load a seperate list for the directory and then change into the
>> > directoryies and do something...cd back out and repeat.

>> > for example in mainlist.txt:

>> > 40/
>> > 41/
>> > 42/
>> > 43/
>> > 44/

>> > in ~ i have 40.txt, 41.txt, 42.txt, 43.txt and 44.txt

>> > i want to be able to, using mainlist.txt, when i am in 40/ load 40.txt
>> > and cd to each directory and do something, cd back out cd to 41/ load
>> > 41.txt etc..

>> > an example of what 44.txt would have inside

>> > fruit/
>> > chairs/
>> > disco/

>> > i hope ive explained that well enough, everything ive tried has not
>> > worked..., any suggestions most welcome

>>     What part of the script is giving you problems?

>>     To operate on each directory from a file, use something like this:

>> while IFS= read -r dir
>> do
>>   (  ## Use a subshell so that the loop remains in the same directory
>>     cd "$dir"
>>     : do something
>>   )
>> done < mainlist.txt

[please don't top post]

Quote:> its not a homework assignment...sheesh
> i can do it fine for the first part, in fact I had something exactly
> like what you have their, the problem was the second part, the subshell
> to be exact..., im not too sure how to do that?

   See the example; the subshell is invoked by enclosing the code in
   parentheses.

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

tricky file script

Post by zae.. » Sat, 19 Nov 2005 02:18:11


Hi chris,

WHat I have come up with so far is this:

while IFS='$\n' read mydir;
do cd "/home/$mydir"; for file in lists/*;
do if [[ -f $file ]]; then while IFS=$'\n' read subdir;
do ls "$subdir";
done < "$file"; fi; done;
done < mainlist

however..i had it a bit different before and it was working fine, now
it just exits instantly for no apparant reason. I did have it as a
subshell, but was having some problem.., mainly i dont think it was
cding to the right directory. what would be the easiest way to make it
so if it is in directory 22, to load 22.txt? and to change each
directory in 22.txt and do something?

i know this sounds strange, but hopefully you unerstand what i mean...

 
 
 

tricky file script

Post by Chris F.A. Johnso » Sat, 19 Nov 2005 02:37:27



> Hi chris,

   First, please read: <http://cfaj.freeshell.org/google>.

Quote:> WHat I have come up with so far is this:

   Second, use the snippet I posted as a guide.

Quote:> while IFS='$\n' read mydir;
> do cd "/home/$mydir"; for file in lists/*;
> do if [[ -f $file ]]; then while IFS=$'\n' read subdir;
> do ls "$subdir";
> done < "$file"; fi; done;
> done < mainlist

   Third, please format your code so that it's structure is clear.

Quote:> however..i had it a bit different before and it was working fine,

   What does "a bit different" mean?

Quote:> now it just exits instantly for no apparant reason. I did have it as
> a subshell, but was having some problem..,

   Did you? I haven't seen that.

Quote:> mainly i dont think it was cding to the right directory.

   What makes you think that?

Quote:> what would be the easiest way to make it so if it is in directory
> 22, to load 22.txt? and to change each directory in 22.txt and do
> something?

    Presumably, you have the name of the directory in a variable; use
    it. E.g.:

< $dir.txt

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

tricky file script

Post by zae.. » Sat, 19 Nov 2005 17:26:54




> > Hi chris,

>    First, please read: <http://cfaj.freeshell.org/google>.

> > WHat I have come up with so far is this:

>    Second, use the snippet I posted as a guide.

> > while IFS='$\n' read mydir;
> > do cd "/home/$mydir"; for file in lists/*;
> > do if [[ -f $file ]]; then while IFS=$'\n' read subdir;
> > do ls "$subdir";
> > done < "$file"; fi; done;
> > done < mainlist

>    Third, please format your code so that it's structure is clear.

> > however..i had it a bit different before and it was working fine,

>    What does "a bit different" mean?

> > now it just exits instantly for no apparant reason. I did have it as
> > a subshell, but was having some problem..,

>    Did you? I haven't seen that.

> > mainly i dont think it was cding to the right directory.

>    What makes you think that?

> > what would be the easiest way to make it so if it is in directory
> > 22, to load 22.txt? and to change each directory in 22.txt and do
> > something?

>     Presumably, you have the name of the directory in a variable; use
>     it. E.g.:

> < $dir.txt

> --
>    Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
>    Shell Scripting Recipes:     |  My code in this post, if any,
>    A Problem-Solution Approach  |          is released under the
>    2005, Apress                 |     GNU General Public Licence

Hi there,

Sorry about the google top posting, i wasnt aware..

This is the ocde I am using now and works fine, just posting in case
anyone els emight sometime need it. Once again many thanks.

while IFS= read -r dir
do
  (  ## Use a subshell so that the loop remains in the same directory
    cd "/home/$dir"
    while IFS= read -r subdir
do
cd "$subdir"
ls -la 2>/dev/null; pwd;
done < /home/21/s4108444/lists/$dir.txt
  )
done < boo.txt

 
 
 

1. alter file names, tricky scripting problem...

Hello, I'm having a bash scripting problem.

I have a many directories with a few thousand files each, called:

dataA1.img, dataA2.img, dataA3.img, .....

I will need to change the names of the files such as:

dataB1.img, dataB2.img, dataB3.img, .....

I tried to figure out how to use sed in a for loop, but could not figure
out how to feed the filename in a variable to sed. I thought about using
awk to get each file name but don't know how to use the result with a mv
command or a sed command.

Any help would be very much appreciated, thank you.

Jim

2. Learning UNIX on a Macintosh

3. A tricky scripting problem. Any takers?

4. DVD-ROM

5. tricky shell script?

6. Division by zero crashes on DEC should be avoidable!

7. Tricky Born shell script with a here document...

8. Umax S6E and Linux

9. tricky login script -

10. Copy files using filenames from text files with shell script or bash script

11. Reformat file, tricky.

12. REALLY tricky question: different files depending on system

13. again on rm files with funny names (tricky)