The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copying multiple folders to local machine (don't know folder names) leenyburger UNIX for Dummies Questions & Answers 5 06-12-2008 04:38 AM
Parse the .txt file for folder name and FTP to the corrsponding folder. MeganP Shell Programming and Scripting 3 07-03-2007 10:54 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 02:25 AM
To get the file names. surjyap Shell Programming and Scripting 1 10-10-2005 04:11 AM
File names giantflameeater UNIX for Dummies Questions & Answers 9 11-24-2004 04:03 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-28-2001
Registered User
 

Join Date: Jun 2001
Posts: 8
Exclamation Getting all file names in a folder.

Hi All,
I need help to create a shell script that does following:

- do ls -1 and let all file names in the current folder
- do wc for each file and if wc > 0 then send a mail.

I am new to unix and do not know how to get filename one by one in a loop, so that wc can be done. I have done rest of the things but just looping all files is left.

Please help!

Thanks
Forum Sponsor
  #2  
Old 06-28-2001
Registered User
 

Join Date: May 2001
Location: NY
Posts: 39
can you try this:
Code:
ls -1 > flist
for f in `cat flist`
do
  x=`wc $f | cut -c1-8`
  if [ x -gt 0 ]
  then
    echo "send mail"
  else
    echo "do nothing"
 fi
done
this leads me to ask what is the better way to
extract the character column from a wc command,
instead of cut -c1-8... any idea, anyone ??

HTH....any comment is appreciated. thanks.

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 07:27 AM.
  #3  
Old 06-28-2001
Registered User
 

Join Date: Jun 2001
Posts: 8
Wonderful.

Quite not all, but your code gave me enough idea to implement this. Following is what I have done and its working great.

Thanks a lot for your help.

For your thinking about using cut, well I used awk as its easier and will give me complete field regardless of its size.
Code:
#!/bin/ksh

strPath=`echo "/app70s/d001/staging/Test/BadFiles/"$1`

for file in `ls -l $strPath | grep -v drwx | awk '{print $9}'`
do
   echo ":" $file
   intCount=`wc $strPath/$file | awk '{print $1}'`
   if [ $intCount != 0 ]
     then
       echo $file " contains bad records. Rows are failing." | mailx -s "Session Failed" mail.address@mail.server.com;
   fi
done
added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 07:27 AM.
  #4  
Old 06-28-2001
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
a problem with what you have writen is if the file/dir in question is not rwx for the owner. ie drx-


i would try something to the effect of
Code:
case "$1" in
'-d')
DIR=${2:?"bad or missing Directory"}
for filelist in `ls $DIR`;do
        filesize=`ls -l $DIR/$filelist|awk '{ print $5 }'`
        if [[ $filesize == 0 ]];then
                echo "$filelist is $filesize"|mailx -s "SUBJECT" user@host
        fi
done
;;

*)
        echo ""
        echo "Usage: for $0 [-d] /directory/to/search"
        echo ""
        ;;
esac

Last edited by Optimus_P; 06-28-2001 at 01:31 PM.
  #5  
Old 06-28-2001
Registered User
 

Join Date: Jun 2001
Posts: 8
Totally agree. Actually my application is custom for my environment and so I did not bothered about finding any dir because BadFiles folder should not have any sub-folders.

But what you told me is totally acceptable and I'm sure going to implement this. Just in case if someone creates a folder in future!

Thanks a lot for your inputs.

Adarsh
  #6  
Old 06-28-2001
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
ok i posted something completely diffearnt and works for any env.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0