Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

BASH shell scripting

Options
  • 03-12-2007 1:19pm
    #1
    Closed Accounts Posts: 1,788 ✭✭✭


    Hi
    Im doing a little script in BASH for emailing files and then deleting them

    I have
    #! /bin/sh
    
    
    
    LOCAL=/root/em_files
    
    
    cd $LOCAL
    
    for f in *.tif
    
    do 
    	if [ $f = "*"]; then
    		break #because of no files
    	else 
    	if [mail_body.txt = "*"]; then
    		break #mail body doesn't exist
    	
    	else 
    	mutt -s "[$f]" -a $f emaddress@org.com  < mail_body.txt
    	rm -f $f
    	fi
    done
    


    How can i incorporate an OR in an if stmt ?
    and how can i check if a file doesn't exist what i have above
    if [mail_body.txt = "*"]; then
    		break #mail body doesn't exist
    


    im not sure , i only know that
    if [ $f = "*"]; then
    		break #because of no files
    

    checks to see if the other files exist ? but what about when i wanna check just one ?


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    There are plenty of tutorials on the net for bash scripting, give Google a try.
    How can i incorporate an OR in an if stmt ?

    There is a -o flag - look at http://tldp.org/LDP/abs/html/comparison-ops.html and compound comparison subsection.
    and how can i check if a file doesn't exist what i have above

    There are various flags for checking files. You might what to use -e (file exists) or -s (file is non-empty). Look at:
    http://tldp.org/LDP/abs/html/fto.html


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    There is a -o flag - look at http://tldp.org/LDP/abs/html/comparison-ops.html and compound comparison subsection.
    Just one thing to be careful of here is the (($a < $b)), the double bracket comparison, was introduced by the bash shell and may not be present in sh (original unix shell). And the OP has a shabang line referencing /bin/sh instead of /bin/bash, so that might cause some issues. Also the comparison "$a" < "$b" will only work in bash, and not sh.


  • Registered Users Posts: 868 ✭✭✭brianmc


    jackdaw wrote: »
    Hi
    Im doing a little script in BASH for emailing files and then deleting them

    I have

    etc..
    #!/bin/bash
    
    LOCAL=/root/em_files
    
    if [[ -f $LOCAL/mail_body.txt ]]
    then
    	echo "Mail Body does not exist" >&2
    	exit 1
    fi
    
    for FILENAME in $(ls $LOCAL/*.tif)
    do
    	mutt -s "[$FILENAME]" -a $FILENAME emaddress@org.com < $LOCAL/mail_body.txt
    	rm -f $LOCAL/$FILENAME
    done
    
    exit 0
    
    

    I find it's much better in scripts to avoid using "cd" since it's effectively a global attribute of the process and should the script become more complex this will only make it trickier to work with.

    As was pointed out earlier, you've added a #! reference to the Bourne shell and not to the Bash shell. For Bourne there would be very few changes from above... feck it actually... here...

    I've used "ls" to get the list of filenames for the for loop rather than file name matching so that if the directory is empty we end up with nothing to be processed rather than a value of "*.tif".
    #!/bin/sh
    
    LOCAL=/root/em_files
    
    if [ -f $LOCAL/mail_body.txt ]
    then
    	echo "Mail Body does not exist" >&2
    	exit 1
    fi
    
    for FILENAME in `ls $LOCAL/*.tif`
    do
    	mutt -s "[$FILENAME]" -a $FILENAME emaddress@org.com < $LOCAL/mail_body.txt
    	rm -f $LOCAL/$FILENAME
    done
    
    exit 0
    
    

    Watch out for spacing around the square brackets in the "if" statement. The first bracket isn't really a trivial bit of syntax it's effectively a command that does comparisons and tests so the remaining part of the line " -e $LOCAL/mail_body.txt ] " is actually a sequence of arguments to a command.

    -f is a test to see that a file (specifically a file - not a directory or anything else) exists in the file system.


Advertisement