Using bc in bash script

Charles Howse chowse at charter.net
Thu Aug 14 14:23:35 PDT 2003


OK, I've been playing with the time command to get the elapsed time of
my daily_report script reported in 2 decimal places.

If I do:
# \time -ha ~/daily.log ~/bin/daily_report (append output of time to
~/daily.log)
I get:
Time: /root/daily.log permission denied
(time output)

If I do:
# chmod 666 daily.log
# \time -ha ~/daily.log ~/bin/daily_report (append output of time to
~/daily.log)
I get:
Time: /root/daily.log permission denied
(time output)

If I do:
# \time -ho ~/tmp.time ~/bin/daily_report
That works OK

The problem with all of this is that time must wait for the daily_report
script to finish before it writes tmp.time.
By then, in it's existing form, daily_report has already mailed
daily.log to charles.
I'm gonna hafta re-write and re-arrange some things to make this work
properly.
I don't object to that in principle, but there must be a more elegant
way to do this.




For reference:

----------------------------------------------

#!/usr/local/bin/bash
# Daily Report

# Declare variables
start_time=`date +%s`
time1=`date +%R`
month=`date +%b`
day=`date +%e`

# Cleanup files
if [ -a /root/daily.log ] ; then
    rm /root/daily.log
fi

# Main report header
echo "Daily Report for Larry for" `date '+%A, %B %d %Y'`"." >>
/root/daily.log
echo "********************************************" >> /root/daily.log
echo >> /root/daily.log 

# OS header
echo "Current Operating System" >> /root/daily.log
echo "********************************************" >> /root/daily.log
uname -sr >> /root/daily.log 
echo >> /root/daily.log 

# Uptime Header
echo "Uptime" >> /root/daily.log
echo "********************************************" >> /root/daily.log
uptime >> /root/daily.log 
echo >> /root/daily.log

# Crontab Header
echo "Cron Jobs" >> /root/daily.log
echo "********************************************" >> /root/daily.log
crontab -l >> /root/daily.log 
echo >> /root/daily.log

# Last Header
echo "Logins today" >> /root/daily.log
echo "********************************************" >> /root/daily.log
last | grep "$month $day" >> /root/daily.log 
echo >> /root/daily.log

# Superuser Header
echo "Accounts with uid = 0 (Superusers)" >> /root/daily.log
echo "********************************************" >> /root/daily.log
awk -F: '( $3 == 0 ) { print $1 }' /etc/passwd >> /root/daily.log 
echo >> /root/daily.log

# /etc/passwd Header
echo "Accounts that have a valid shell" >> /root/daily.log
echo "********************************************" >> /root/daily.log
cat /etc/passwd | egrep -v "("nologin"|"uucico"|"\#")" >>
/root/daily.log 
echo >> /root/daily.log

# DF Header
echo "Disk Free space" >> /root/daily.log
echo "********************************************" >> /root/daily.log
df -h >> /root/daily.log 
echo >> /root/daily.log

# netstat Header
echo "Netstat -an results" >> /root/daily.log
echo "********************************************" >> /root/daily.log
netstat -an >> /root/daily.log
echo >> /root/daily.log

# ifconfig
echo "Status of network interfaces" >> /root/daily.log
echo "********************************************" >> /root/daily.log
ifconfig >> /root/daily.log
echo >> /root/daily.log

# Compute the elapsed time
echo "Elapsed Time" >> /root/daily.log
echo "********************************************" >> /root/daily.log
echo "Report completed at:" `date +%R` >> /root/daily.log
echo "    Report begun at:" $time1 >> /root/daily.log
end_time=`date +%s`
et=`echo $end_time - $start_time | bc`
if [ $et -lt 1 ] ; then
    echo "       Elapsed Time: less than 1 second" >> /root/daily.log
else
    echo "       Elapsed Time: " $et "seconds" >> /root/daily.log
fi
echo >> /root/daily.log

# File Modification Date
echo "Last modified" >> /root/daily.log
echo "********************************************" >> /root/daily.log
ls -l /root/bin/daily_report | cut -d" " -f9,10,11 >> /root/daily.log

# Mail to Chalres
cat /root/daily.log | mail -s "Daily Report from Larry" charles




More information about the freebsd-questions mailing list