Echoing only once for each subdir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echoing only once for each subdir
# 1  
Old 06-02-2014
Echoing only once for each subdir

I have a script that runs from this:

Code:
for i in * ; do (cd $i && echo $i && /test1/execute/testb);done

this is testb:
Code:

for file in `ls *.txt`
do
if [ ! "$file" = "$Z" ] && [ ! "$file" = "$Z2" ] && [ ! "$file" = "$Z3" ] && [ ! "$file" = "$Z4" ] &&
[ ! "$file" = "$Z5" ] && [ ! "$file" = "$Z6" ] ; then echo "NO"; break 1;
else
echo "it is there"
fi
done

What is happening is that I can get it to run a command in each subdirectory, but when I do, it prints NO every time it does not find what it is looking for. This is accurate, but I only want it to print the message once for each subdir in which it does not find anything.

I've tried break 1 and break/continue, and it still prints the message many times for each subdirectory. Every time it does not find the file that is specified in the Z1/Z2/Z3 variables, it prints "NO". I only want it once for each directory.

Is there a way to do this?
# 2  
Old 06-02-2014
Why so complicated? Try
Code:
ls "$Z" "$Z2" "$Z3" "$Z4" "$Z5" "$Z6" && echo "it is there" || echo "NO"

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-02-2014
It's good to see that you've tried, but you logic is a little open. Your loop is looking at every file in a directory and then testing to see if the file name matches one of the six you list. You are therefore comparing perhaps thousands of things. You are reading the directory with ls *.txt already so what you could do it something more like:-
Code:
for searchfile in $Z $Z2 $Z3 $Z4 $Z5 $Z6
do
   if [ -f $searchfile ]
   then
      echo "$searchfile is in $PWD"
   fi
fi

You can add more if you like, but you are not clear if you actually need all the files to be present or not. Can you clarify if you just need to find any one or all?

Listing multiple files will get an error if any are missing, and you need to handle the output/errors from every list command too.

It might be simpler still to use the find command like this:-
Code:
find /start/directory -name $Z -o -name $Z2 -o -name $Z3 -o -name $Z4 -o -name $Z5 -o -name $Z6


Does this give you the output you need in an easier way?

Robin
# 4  
Old 06-02-2014
Thanks- want to learn about break/continue

Rudi:

The solution works great! Out of curiosity, I wonder is there a way to accomplish the same thing with break/continue even though it is not as efficient?
# 5  
Old 06-03-2014
break should work; I didn't analyze your code snippet where and why it would fail.
# 6  
Old 06-06-2014
Quote:
Originally Posted by newbie2010
Rudi:

The solution works great! Out of curiosity, I wonder is there a way to accomplish the same thing with break/continue even though it is not as efficient?
This is probably what you are looking for:

Code:
for i in *
do
   echo $i
   for f in "$Z" "$Z1" "$Z2" "$Z3" "$Z4" "$Z5" "$Z6"
   do
      if [ -f "$i/$f" ]
      then
          echo "it is there"
          continue 2
      fi
    done
    echo "NO"
done

# 7  
Old 06-09-2014
I have expanded this and am trying to get it to make a file that prints "lists are needed" just once under each directory. I have this so far:
Code:
DATEN=$(perl -e 'print scalar(localtime(time -  0 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')
DATE1=$(perl -e 'print scalar(localtime(time - 1 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')
DATE2=$(perl -e 'print scalar(localtime(time - 2 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')
DATE3=$(perl -e 'print scalar(localtime(time - 3 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')
for i in * ; do (cd $i; echo $i; for f in `ls *.txt`;
do
file=$(truss -vlstat -tlstat ls -l $f 2>&1 |awk 'NR==4'|gawk '{print $3, $4, $7}')
if [ ! "$file" = "$DATEN" ] && [ ! "$file" = "$DATE2" ] && [ ! "$file" = "$DATE3" ]; then
echo "$file" "the file is not correct" >/dev/null
else echo "IT is OK"
fi
done)
done

It will print "the file is not there multiple times if I do not redirect it to /dev/null. I just want it to print once " the file is not there?" I have also tried to tack on an if/then statement to see if I could get it to work, but I can't. I keep getting a message for each file "file is not correct" -- I just want one line with that message per directory. Is there a way to do this without rearranging the entire script?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getopts not echoing correctly

Hi, When I run the the following code: #!/bin/bash if ]; then usage fi if ] then echo "Do not execute this as root, use -s instead" fi SERVERFILE="servers" function usage { echo "USAGE: ${0} COMMAND" (4 Replies)
Discussion started by: mohca2020
4 Replies

2. Shell Programming and Scripting

Echoing silently?

I know, sounds mutually exclusive :-) I have a script where I ask for a password and store it in a variable, and then use it with sudo on an array of other hosts. The password winds up being choed back to my terminal as well as to the process on the remote host, like: Attempting to update... (2 Replies)
Discussion started by: jnojr
2 Replies

3. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

4. Shell Programming and Scripting

Copy files from subdir

Hey, How can I copy files from subdirectories without copy the subdir and copy it to a higher dir For example: /home/test/subdir/file1 copy file1 to /home or another dir thanx (11 Replies)
Discussion started by: Eclecticaa
11 Replies

5. Shell Programming and Scripting

moving dir subdir and files

I have created a directory structure and under the directory subdirectories and files are there.I need to move the entire thing to another path.How can i write a script to do that. currently the path of files is as below : /data1/serial/mcycle/archive : under this path differnt sub dir exist ... (6 Replies)
Discussion started by: dr46014
6 Replies

6. Shell Programming and Scripting

Grep in subdir, multiple files

Hi, I m trying to have script to grep a pattern in all files under a directory. I use Sun Solaris, the below want doesnt do exactly what I want. find /home/xxx/ tagHeu | while read FILE; do grep text $FILE && echo 1 grep text $FILE || echo 0 done I also tried running: find... (2 Replies)
Discussion started by: s3rro
2 Replies

7. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies

8. Shell Programming and Scripting

Echoing

I was just wondering how you would echo out different length variables but still have them all line up. I tried putting tabs between the variables but that didn't work as planned. For example this is in some loop, with different variables in it each time: echo "$1 $2 $3 $4 $5" Appears like... (3 Replies)
Discussion started by: Okema
3 Replies

9. UNIX for Dummies Questions & Answers

echo not echoing correctly

Here is the file named tuwork.......... 209 200 WZ 6529 SKTNCA01X4X C POI LODI LODI 738 SKTNCA0127T LOD Here is the scipt....... cat tuwork | while read rva do num=`echo $rva | cut -d" " -f1-2` reg=`echo $rva | cut -c10` ocn=`echo $rva | cut -c12-15` x=`echo $rva | cut -c29`... (3 Replies)
Discussion started by: shorty
3 Replies

10. Shell Programming and Scripting

Finding patterns through out all subdir

Hi all experts, here is a problem which i would appreciate ur expertise. I need to do this: Eg. Find a number: 1234567 which i dunno which file and which folder I do know which main folder it is in but it is hidden deep within a lot of subdir. Is it possible to find the file? + output... (4 Replies)
Discussion started by: unnerdy
4 Replies
Login or Register to Ask a Question