|
Home > Archive > Unix Shell > November 2005 > tricky file script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
tricky file script
|
|
| zaebos@gmail.com 2005-11-16, 6:03 pm |
| 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
| |
| Michael Heiming 2005-11-16, 6:03 pm |
| In comp.unix.shell zaebos@gmail.com:
> 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)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 126: it has Intel Inside
| |
| Chris F.A. Johnson 2005-11-16, 6:03 pm |
| On 2005-11-16, zaebos@gmail.com wrote:
> 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
| |
| zaebos@gmail.com 2005-11-17, 6:12 pm |
| 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?
Chris F.A. Johnson wrote:
> On 2005-11-16, zaebos@gmail.com wrote:
>
> 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
| |
| Chris F.A. Johnson 2005-11-17, 6:13 pm |
| On 2005-11-17, zaebos@gmail.com wrote:
>
> Chris F.A. Johnson wrote:
[please don't top post]
[vbcol=seagreen]
> 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
| |
| zaebos@gmail.com 2005-11-17, 6:13 pm |
| 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...
| |
| Chris F.A. Johnson 2005-11-17, 6:13 pm |
| On 2005-11-17, zaebos@gmail.com wrote:
> 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
| |
| zaebos@gmail.com 2005-11-18, 2:48 am |
|
Chris F.A. Johnson wrote:
> On 2005-11-17, zaebos@gmail.com wrote:
>
> First, please read: <http://cfaj.freeshell.org/google>.
>
>
> Second, use the snippet I posted as a guide.
>
>
> Third, please format your code so that it's structure is clear.
>
>
> What does "a bit different" mean?
>
>
> Did you? I haven't seen that.
>
>
> What makes you think that?
>
>
> 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
|
|
|
|
|