The UNIX and Linux Forums  


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



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2009
Posts: 5
Error with the following script

Greetings again.
I made a program that tells the size of a dictionary and stores it into a temp.txt file.

Code:
temp="temp.txt"

if [ $# -ne 1 ]; then
      echo "Wrong parameter"
 else
      if [ -d "$1" ]; then
             rm "$temp"                        #Delete any previous temp file
             find "./$1" -maxdepth 1 -type f >> "$temp"           
             #save the found files to the temp file

IFS="           #set for breaking characters
"
size=0
files=`cat "$temp"`     #read the temp file
rm "$temp"                 
#delete the temp file (making sure its size wont be read)

#size counting
for i in $files; do
   if [ -f $i ]; then
      size=$(expr $size + $(stat -c%s "$i"))
   fi
done

echo "The directory's size is : $size"
else 
echo "A non directory parameter was given"
fi
fi

the problem is the script doesn't create the temp.txt file. if i create one then it deletes it so the deleting part is okay.
the script - as it is at the moment - also tells the correct size of the dictionary, so that part seems to be okay aswell.
i just cannot fix it:/
Thanks in advance!
  #2 (permalink)  
Old 11-30-2009
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 7,127
My impression is that you are posting homework in the main forums.

To post homework, you must follow homework posting rules in the proper forum.

Also, you need to write more descriptive subjects.

Here is a link to the rules. Please read them and follow them. Thanks!

The UNIX and Linux Forums - Forum Rules
  #3 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Mar 2008
Posts: 1,350
The script deletes temp.txt after creating and reading back reading the file.

Quote:
files=`cat "$temp"` #read the temp file
rm "$temp"
#delete the temp file (making sure its size wont be read)
This causes temp.txt to be not present the next time the script is run and then this "rm" line will fail:

Quote:
if [ -d "$1" ]; then
rm "$temp" #Delete any previous temp file
Actually the "Delete any previous temp file" line is not needed.
Consider replacing ">>" with ">" in the "find" line such that it creates the file if it does not exist or overwrites the file if it does exist.


Code:
find "./$1" -maxdepth 1 -type f > "$temp"


Last edited by methyl; 12-01-2009 at 11:36 AM.. Reason: typo
Sponsored Links
Reply

Bookmarks

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting error while calling a script within a script sriniu190 Shell Programming and Scripting 6 10-07-2009 07:39 AM
Script with error output but continuation in script? daem0n Shell Programming and Scripting 3 08-23-2009 11:10 PM
FTP script error Lenora2009 Shell Programming and Scripting 21 07-23-2009 06:15 PM
script error xipiska Shell Programming and Scripting 4 06-26-2008 02:36 PM
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 11:16 AM



All times are GMT -4. The time now is 02:37 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