24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

DISKFUL=$(df -h $WEBDIR | grep -v File | awk '{print $5 }' | cut -d "%" -f1 -)<br />

until [ $DISKFUL -ge "90" ]; do<br />

DATE=`date +%Y%m%d`<br />

HOUR=`date +%H`<br />

mkdir $WEBDIR/"$DATE"<br />

while [ $HOUR -ne "00" ]; do<br />

DESTDIR=$WEBDIR/"$DATE"/"$HOUR"<br />

mkdir "$DESTDIR"<br />

mv $PICDIR/*.jpg "$DESTDIR"/<br />

sleep 3600<br />

HOUR=`date +%H`<br />

done<br />

DISKFULL=$(df -h $WEBDIR | grep -v File | awk '{ print $5 }' | cut -d "%" -f1 -)<br />

done<br />

TOREMOVE=$(find $WEBDIR -type d -a -mtime +30)<br />

for i in $TOREMOVE; do<br />

rm -rf "$i";<br />

done<br />

done<br />

Note the initialization of the HOUR and DISKFULL variables and the use of options with ls and date in order<br />

to obtain a correct listing for TOREMOVE.<br />

9.4. I/O redirection and loops<br />

9.4.1. Input redirection<br />

Instead of controlling a loop by testing the result of a command or by user input, you can specify a file from<br />

which to read input that controls the loop. In such cases, read is often the controlling command. As long as<br />

input lines are fed into the loop, execution of the loop commands continues. As soon as all the input lines are<br />

read the loop exits.<br />

Since the loop construct is considered to be one command structure (such as while TEST-COMMAND; do<br />

CONSEQUENT-COMMANDS; done), the redirection should occur after the done statement, so that it<br />

complies with the form<br />

command < file<br />

This kind of redirection also works with other kinds of loops.<br />

9.4.2. Output redirection<br />

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

In the example below, output of the find command is used as input for the read command controlling a while<br />

loop:<br />

[carol@octarine ~/testdir] cat archiveoldstuff.sh<br />

#!/bin/bash<br />

# This script creates a subdirectory in the current directory, to which old<br />

Chapter 9. Repetitive tasks 113

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!