Sponsored Content
Top Forums Shell Programming and Scripting For loop and read from different directories Post 303001162 by rbatte1 on Friday 28th of July 2017 04:49:19 AM
Old 07-28-2017
Hello Boris/baris35,

I think I understand the principle, but as a starting point, let me indent your code for clarity:-
Code:
cd /directoryA
for loopa in *.txt;
do
   some codes 
   cd ../directoryB
   for loopb in *.txt;
   do
      some codes
      cd ../directoryC
      for loopc in *.txt;
      do
         some codes
      done
   done
done

As you can see, I have changed the variable for the loops else results will be unpredictable.

With this, you would be trying to read everything in directoryC 3,000~ish times (for every file in directoryA multiplied by every file in directoryB) Is this really what you want?

You also have the problem that you are changing directory just before a loop, but on leaving the loop you do not change back, so for the second loop and after (e.g. file b.txt) of directoryB, your shell would be in directoryC. When processing the second loop and after of files in directoryA (e.g. 2.txt), your shell would also be either in directoryC so your some codes statement would have to handle being in various places. The for loop will already have been formed, so the loop as a whole will process as you are telling it, but very likely in the wrong directory.

You you just want to process each file once, you need to move the done statements, which would give you this:-
Code:
cd /directoryA
for i in *.txt;
do
   some codes 
   cd ../directoryB
done

for i in *.txt;
do
   some codes
done

cd ../directoryC
for i in *.txt;
do
   some codes
done

If you really do want to process every file in directoryC 3,000~ish times, consider using pushd & popd to handle directory transitions like this:-
Code:
pushd /directoryA
for loopa in *.txt;
do
   some codes 
   pushd /directoryB
   for loopb in *.txt;
   do
      some codes
      pushd /directoryC
      for loopc in *.txt;
      do
         some codes
      done
      popd
   done
   popd
done
popd

They will handle the moving in and out of directories safely. It is better to use a fully qualified directory path rather than trying to assume where you are and issuing cd ../directoryX



Sorry I've gone on for a while, but I hope that this helps.


Can you tell us what you are actually trying to achieve? So sort of logical steps you want to do and we can work no it a bit better.

Kind regards,
Robin
These 2 Users Gave Thanks to rbatte1 For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to loop through all files and directories.

I'm trying to write a script that will loop through all files and directories down from a path I give it, and change the permissions and ACL. I was able to do the obvious way and change the files and folders on the same level as teh path...but I need it to continue on deeper into the file... (2 Replies)
Discussion started by: cheetobandito
2 Replies

2. Shell Programming and Scripting

How to loop through directories to touch files

Hi, Please help me on this. Suppose i have the following directory structure. /app/data /app/data/eng /app/data/med /app/data/bsc each of the directories data,data/eng,data/med,data/bsc holds files with date extension like a.20081230 b.20081230 and so on I need a script to loop... (9 Replies)
Discussion started by: sussane
9 Replies

3. Shell Programming and Scripting

Newbie: How to loop round sub-directories

I want a bit of shell script that will let me loop round all the sub-directories in a directory (i.e. ignoring any ordinary files in that directory). Let's say I just want to echo the names of the sub-directories. This sounds like it should be pretty easy - but not for me, it isn't! All help... (4 Replies)
Discussion started by: cjhancock
4 Replies

4. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

5. Shell Programming and Scripting

For Loop Range Create Directories

Hello, I am a bit stumped on this. I am attempting to create 24 empty directories with a loop. Seems like I have incorrect syntax. When I run the following command I get the error below. Command $ for i in {2..24}; do mkdir $i_MAY_2011 ; doneError x 24 mkdir: missing operand Try `mkdir... (2 Replies)
Discussion started by: jaysunn
2 Replies

6. Shell Programming and Scripting

To read directories and file

Hi ; I want to write a shell script to read all files and directories(recursively) given in path along with their user permissions and store that result in one file as File path Userpermissions ===== =========== I m new to linux and my dont kno much abt shell scripting. I will... (5 Replies)
Discussion started by: ajaypadvi
5 Replies

7. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

8. Shell Programming and Scripting

Accessing multiple directories in loop

Hi Guys, I need to access multiple directories whcih is following similar structure and need to copy those files in desitination path. for eg : if ] then cd ${DIR}/Mon/loaded echo "copying files to $GRS_DIR" cp * ${DIR}/Mon/ echo "Files of Monday are Copied" fi if ] then... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

9. Solaris

Giving read write permission to user for specific directories and sub directories.

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. This is for Solaris. Please help. (1 Reply)
Discussion started by: blinkingdan
1 Replies
All times are GMT -4. The time now is 07:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy