The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk continue anhtt Shell Programming and Scripting 3 03-11-2008 12:08 PM
Continue Script when File Found Jose Miguel Shell Programming and Scripting 7 11-07-2006 08:43 PM
Continue an 'scp' tranfser? deckard UNIX for Advanced & Expert Users 3 12-19-2005 05:08 PM
Will the continue function work ???? kamlesh_p Shell Programming and Scripting 2 10-12-2005 09:27 AM
continue the suspended jobs killerserv UNIX for Advanced & Expert Users 6 01-10-2002 03:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-11-2007
xramm xramm is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
continue example

The following code for search a pattern in file name (or entire file name) and look at its size, code is derived from an ebook about scripting. It is working in HP Unix but I am unable to run in Linux (Ubuntu) Please advise me what is wrong for Linux?
And besides , how can I get rid of errors in case of the file is not available?

Code:
pattern=core
total=0

for f in *
  do
     [ ! -f $f ] && continue
     if grep $f $pattern > /dev/null
     then
        size=`cat $f|wc -c`
        total=èxpr $total + $size`
     fi
  done

echo Total size of $pattern is $total
  #2 (permalink)  
Old 06-11-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
do you have a typo? cause what i saw is this
Code:
total=èxpr $total + $size`
if should be
Code:
total=`expr $total + $size`
  #3 (permalink)  
Old 06-11-2007
xramm xramm is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
yes exactly a typo, thanks.
  #4 (permalink)  
Old 06-17-2007
xramm xramm is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
it is better way:

if grep $f $pattern 2> /dev/null
  #5 (permalink)  
Old 06-18-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
Quote:
Originally Posted by xramm View Post
The following code for search a pattern in file name (or entire file name) and look at its size, code is derived from an ebook about scripting. It is working in HP Unix but I am unable to run in Linux (Ubuntu) Please advise me what is wrong for Linux?
And besides , how can I get rid of errors in case of the file is not available?

Code:
pattern=core
total=0

for f in *
  do
     [ ! -f $f ] && continue
     if grep $f $pattern > /dev/null
     then
        size=`cat $f|wc -c`
        total=èxpr $total + $size`
     fi
  done

echo Total size of $pattern is $total
The grep command doesn't search the pattern in the file name, the search is made in the file content.
Another point about grep, the right syntax is :
Code:
grep $pattern $f
You can replace the grep command by an expr command :
Code:
     if expr "$f" : "$pattern" > /dev/null
or if you are using ksh or bash :
Code:
     if [[ "$f" == "$pattern" ]]
  #6 (permalink)  
Old 06-18-2007
xramm xramm is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
You are hundred percent right about grep, how I did mistake is my test file consist of file name what a bad coincidence. I wondered that how my code didn't work today, whereas it was working yesterday

Thanks a million,

I hope, I will learn more in time
  #7 (permalink)  
Old 06-18-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Xramm,
According to what you said, you are looking for:
1) Search for a pattern into a file name.
2) At the end, display the total size of all files with pattern in their name.
Based on your specification, here is one solution not having to read the
entire file to find how many characters it has -- very useful for large files.
Code:
typeset -i mSize
typeset -i mTotal=0
mPattern='core'
for mFile in `find . -type f -name "*${mPattern}*"`
do
  mSize=`ls -l $mFile | tr -s ' ' | cut -d' ' -f5`
  mTotal=${mTotal}+${mSize}
done
echo 'Total size of '$pattern' is '${mTotal}
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0