Visit Our UNIX and Linux User Community


Problems with Syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with Syntax
# 1  
Old 08-05-2009
Problems with Syntax

I'm an experienced programmer that is new to shell scripting. I'm putting together a shell script that erases all files of a certain age. These files are in directories provided on lines of a file that is provided via command line argument and takes any errors(permissions, etc) and emails them to a user. If someone could help me clean up my syntax it would be greatly appreciated.
My idea of what the script should look like: (assuming $file and $days passed in from command line)
Code:
foreach($line of $file)
  find $line -mtime +$days -exec r -f{}\; > errors.txt
x=(wc -l < errors.txt)
if(x>0)
  mail -s "errors" someone@email.com < errors.txt
fi

# 2  
Old 08-05-2009
I don't know what the r command does, but try this:
Code:
#!/bin/sh
file=directories.txt
days=100
while read line; do
    find $line -mtime +$days -exec r -f {} \; > errors.txt
    test -s errors.txt  &&  mail -s "errors" someone@email.com < errors.txt
done < $file

# 3  
Old 08-06-2009
replace ....

Code:
if(x>0)
  mail -s "errors" someone@email.com < errors.txt
fi

by

Code:
if [ $x>0 ]; then  mail -s "errors" someone@email.com < errors.txt;fi

# 4  
Old 08-06-2009
Code:
#!/bin/ksh
# or bash
# cleanscript
# arg1 = file, arg2= days

[ $# != 2 ] && echo "usage:$0 dirlistfile days" >&2  && exit 1

file="$1"
days="$2"
cat "$file" | while read line 
do
  find $line -mtime +$days -exec rm -f  {} \; 2> errors.txt
  # do cmd and put stdout to the var
  x=$( wc -l < errors.txt )
  # numeric testing builtin
  if  ((  x>0  ))
  # or using test or [  builtin command
  # if [ "$x" -gt 0 ]
  then
         mail -s "errors" someone@email.com < errors.txt
  fi
done

Code:
chmod a+rx cleanscript
./cleanscript dirlistfile  7


Last edited by kshji; 08-06-2009 at 07:05 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #389
Difficulty: Medium
Rocky Mountain BASIC (RMB) is a dialect of the BASIC programming language created by AIX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

EVAL syntax problems

Hi there As part of a larger script I need to put the output of an ls into a variable which has an incremental number. ie nathan@nathan-Vostro-1700:~$ eval 'proc'$val='`ls -ld /proc/9467`' nathan@nathan-Vostro-1700:~$ echo $proc0 dr-xr-xr-x 8 nathan nathan 0 2012-05-02 09:21... (3 Replies)
Discussion started by: nathan.harris
3 Replies

2. Shell Programming and Scripting

Problems with variables syntax

Hi there I am really struggling :eek: to place a value in a variable with the following loop, having run out of ideas please can someone point me in the right direction? We first read two PIDs of a program (say calc) into an array, then we loop reading the details of those processes into a... (6 Replies)
Discussion started by: nathan.harris
6 Replies

3. UNIX for Dummies Questions & Answers

Syntax Help

Hi everyone, I got some code here that I've been working on, but I've gotten stuck. I cant figure out what is wrong with the syntax. sub cksum{ my $uint32_t = \buffer; my $word_count; my $bias; ( #uint32_t checksum = $bias $word_count = $bias while ($word_count>=0) ... (2 Replies)
Discussion started by: TeamUSA
2 Replies

4. Shell Programming and Scripting

Problems with syntax in a loop (AWK)

Hi guys, I'm trying to loop through a number of files that is set by whatever is in a field. eg. The idea is to split FILELIST down into fields, it could contain 1 - 999 fields and it's bar delimited. I thought simple, count the number of fields in the field and then loop... (1 Reply)
Discussion started by: Peejay
1 Replies

5. Shell Programming and Scripting

Regarding syntax

Hi All, What does this mean ${#var} (2 Replies)
Discussion started by: krishna_gnv
2 Replies

6. Shell Programming and Scripting

what is the right syntax ??

IN the book below example showed find /home/tolstoy -type d -print | Find all directories sed 's;/home/tolstoy/;/home/lt/;' | Change name, note use of semicolon delimiter while read newdir Read new directory name do mkdir $newdir ... (7 Replies)
Discussion started by: convenientstore
7 Replies

7. Shell Programming and Scripting

Help with the syntax

can anyone explain the code for me... i am new to shell programming while getopts ":S:D:U:" OPTION "$@" do case $OPTION in S) SRVR=$OPTARG;; D) DB="$OPTARG"; USEDB="use $OPTARG";; U) UID=$OPTARG;; :) MISSINGOPTARG="$MISSINGOPTARG -$OPTARG";; ?) if then ... (2 Replies)
Discussion started by: chandhar
2 Replies

8. Shell Programming and Scripting

Help with the syntax

export check=$(expandname $(dirname $(which $0))) (2 Replies)
Discussion started by: chandhar
2 Replies

9. UNIX for Dummies Questions & Answers

What does this syntax mean...

OK, the title is a little vague, but basically i was gonig through some files and ran into some strange syntax... heres what it looks like: ... 1&>~/<file extension> where ... is a chain of commands (the 1&>~ is part of the arguments) and the file extension is just a pointer to a file... (1 Reply)
Discussion started by: DrAwesomePhD
1 Replies

10. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies

Featured Tech Videos