Echoing only once for each subdir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echoing only once for each subdir
# 8  
Old 06-10-2014
It is producing multiple "the file is not correct" messages because that's what you have asked it to do.

Your inner loop ask it to consider every file with for f in `ls *.txt` This could be shortened to just for f in *.txt for a starter, but if you have 30 .txt files, you will get up to 30 the file is not correct messages displayed for that directory.

You then have a bit of an open loop with for i in * which will then drive the for f in loop again and again and again. What happens with any files in the current directory (i.e. not directories in themselves)? I expect that they will error on the cd and then carry on with the for f loop in the current directory.

What is the actual requirement of this part of your code? Maybe I'm missing the point, but you are collecting current date and then the same time for the previous three days and then compare this value to the file names that end .txt in a format I don't understand. Can you explain:-
Code:
if [ ! "$file" = "$DATEN" ] &&  .........

Is this an "If the value $file is not like the value of $DATEN" or something?

Apologies if I've not learned this syntax yet and it is correct.



Robin
# 9  
Old 06-10-2014
Yes. I am trying to compare file to DATEN. I want to get only one printout that says "file not there" and not a printout of "file not there" for each file in each directory. So I want one message per directory.
I'll check out your suggestions as well -- thanks.
# 10  
Old 06-10-2014
If you are trying to check if two variables match, the syntax would be like this:-
Code:
if [ "$file" != "$DATEN" ] .........



Robin
# 11  
Old 06-10-2014
That works as well, but I still get multiple messages for each file that does not match the criteria. How do I get one "the file is not there"? I want it to say that the file is not there once for each directory no matter how many files do not match the criteria. I've tried everything and can not figure it out.
# 12  
Old 06-10-2014
Could you change you code so that you keep the outer loop through the list of directories (considering my earlier comment about what happens when for i in * finds a file that it can't cd to)

Inside each directory, you could simply test if the files exist:-
Code:
if [ -f "$DATEN" ] && [ -f "$DATE2" ] && ........

.... which takes out the inner loop and will probably run faster too.

Does this help?




Robin
# 13  
Old 06-10-2014
This is how it looks since yesterday (sorry I did not update it earlier). I believe that the -f DATEN and -f DATE2 does not work because I need to compare what truss outputs to the DATEN and DATE2

All I want to do is have it print "file not found: for each directory in which it does not find any matching date. So far it will print that message when any file in any directory does not match the date, so I will get 10 -15 prints in each directory of the message

Code:
DATEN=$(perl -e 'print scalar(localtime(time -  0 * 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}')


]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 [ -f "$DATEN" ] && [ -f "$DATE2" ];
then echo "$file" >/dev/null
else echo "YES"
fi
done)
done[

---------- Post updated at 09:58 AM ---------- Previous update was at 09:47 AM ----------

Also, it would help if I could search on "does the file match the date". would this be
Code:
 [  "$file" = "$DATEN" ] || [  "$file" = "$DATE2" ]

would that mean if the file matchies date 1 or if it matches date2?

---------- Post updated at 10:17 AM ---------- Previous update was at 09:58 AM ----------

Is there a way to echo a message just once and send the rest to /dev/null ?
So that there would be just one message printed for each directory?

---------- Post updated at 11:03 AM ---------- Previous update was at 10:17 AM ----------

also the previous post you made appears to work somewhat:

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

but it echos yes for everything even though there are no matching files, this
is because I am trying to compare what is in truss command not "do the files exist or not"
# 14  
Old 06-10-2014
Quote:
Originally Posted by newbie2010
This is how it looks since yesterday (sorry I did not update it earlier). I believe that the -f DATEN and -f DATE2 does not work because I need to compare what truss outputs to the DATEN and DATE2

All I want to do is have it print "file not found: for each directory in which it does not find any matching date. So far it will print that message when any file in any directory does not match the date, so I will get 10 -15 prints in each directory of the message

Code:
DATEN=$(perl -e 'print scalar(localtime(time -  0 * 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}')


]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 [ -f "$DATEN" ] && [ -f "$DATE2" ];
then echo "$file" >/dev/null
else echo "YES"
fi
done)
done[

---------- Post updated at 09:58 AM ---------- Previous update was at 09:47 AM ----------

Also, it would help if I could search on "does the file match the date". would this be
Code:
 [  "$file" = "$DATEN" ] || [  "$file" = "$DATE2" ]

would that mean if the file matchies date 1 or if it matches date2?

---------- Post updated at 10:17 AM ---------- Previous update was at 09:58 AM ----------

Is there a way to echo a message just once and send the rest to /dev/null ?
So that there would be just one message printed for each directory?

---------- Post updated at 11:03 AM ---------- Previous update was at 10:17 AM ----------

also the previous post you made appears to work somewhat:

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

but it echos yes for everything even though there are no matching files, this
is because I am trying to compare what is in truss command not "do the files exist or not"
Yes, you can print one message per directory. But, only if your code only prints one message per directory. Your code prints one message per file.

Code:
[  "$file" = "$DATEN" ] || [  "$file" = "$DATE2" ]

exits with a zero exit status if the string to which $file expands is identical to the string to which $DATEN expands OR the string to which $file expands is identical to the string to which $DATE2 expands.

Please take a few steps backwards here. We understand that you want to print one message per directory indicating whether or not some condition is met by the files in that directory. What is the condition? Please explain in English what you are trying to determine!

Are you looking for a file whose name is the same as the date on which that file was last modified? (It seems EXTREMELY unlikely that the date on which a file was modified is going to end with the four characters .txt!)

For those of us who don't have truss on our system, what is the output from the command:
Code:
truss -vlstat -tlstat ls -l $f 2>&1

when f is set to the name of a text file in the current directory?
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