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
cd from a Bourne Shell Script - Please Help fawqati Shell Programming and Scripting 10 05-25-2006 03:26 AM
bourne script help Heedunk Shell Programming and Scripting 1 02-05-2004 08:49 PM
simple bourne script catbad Shell Programming and Scripting 2 03-24-2003 11:36 AM
Can i add colours to bourne Script ? XXXXXXXXXX Shell Programming and Scripting 22 02-25-2002 05:27 AM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 03:38 PM

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 09-06-2007
Pits Pits is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 4
Bourne Script help

Hey guys,

I am trying to do a bourne script to look for c files in the current directory. I had it working where it finds the files and asks you to delete them or not, which works, but if there i no files, then it comes up with errors, which iam trying to get rid of. So I thought I would do a if else statement but failing miserably. The if statement is the problem and I want the filename in *.c to check if there is less than 1 but yeah. Any tips or ideas will be greatly appreciated.

Code below:

#!/bin/sh
#Simple script to look for c programs.

date=$(date)
echo "Todays date is: $date"
echo "This program will help delete all files ending with the .c extension"
echo "Listing all files and the first 10 lines in each file now"
for filename in *.c ;

if [ filename in *.c -r] then

echo "no files found"


else

do

echo "=================================================================================================== ==============="
echo "Variable filename is set to $filename..."
ls -l $filename
head -10 $filename
done
echo "Would you like to delete these files?"

read YN
case $YN in
[yY]*) echo "Files deleted"
rm *.c
;;

[nN]*) echo "No files deleted."
echo "Thanks for using this shellscript."
;;

esac
fi
  #2 (permalink)  
Old 09-06-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
what you can do here store the number of c files in a variable like this

no_of_files=`find . -name "*.c" -print | wc -l`
echo $no_of_files

do your manipulations in if condition with this.
if $no_of_files -lt 0
then
------
else
------------
fi

Thanks
Namish
  #3 (permalink)  
Old 09-06-2007
Pits Pits is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 4
Hi thanks for the tips, it seems to have worked but now I just installed it as is and it does not get part the do part.

Todays date is: Thu Sep 6 18:15:04 EST 2007
This program will help delete all files ending with the .c extension
Listing all files and the first 10 lines in each file now
0
delC.sh: line 19: syntax error near unexpected token `do'
delC.sh: line 19: `do'


Code below:

#!/bin/sh
#Simple script to look for c programs.

date=$(date)
echo "Todays date is: $date"
echo "This program will help delete all files ending with the .c extension"
echo "Listing all files and the first 10 lines in each file now"
#for filename in *.c ;
no_of_files=`find . -name "*.c" -print | wc -l`
echo $no_of_files

if $no_of_files -lt 0
then

echo "no files found"

else

do

echo "=================================================================================================== ==============="
echo "Variable filename is set to $filename..."
ls -l $filename
head -10 $filename
done
echo "Would you like to delete these files?"

read YN
case $YN in
[yY]*) echo "Files deleted"
rm *.c
;;

[nN]*) echo "No files deleted."
echo "Thanks for using this shellscript."
;;

esac
fi

Any ideas?

Last edited by Pits; 09-06-2007 at 04:17 AM..
Closed Thread

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 On




All times are GMT -4. The time now is 07:32 AM.


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