You are not logged in.
Hey everyone, first let me preface this by saying...the following is probably the ugliest, mangled and long winded script. But it's the first complete script that I've written, and I know it's probably inefficient in terms of size an function. Which is why I'm posting it here. I'd love to get some of your far more expert eyes on it. Any suggestions to reduce the size, and syntax or explain why things would work better if written another way, would be appreciated vastly! I'm still trying to practice with my scripting...Without further ado..
#!/bin/bash
#Script to ping security now repo, and download latest episode. If a new ep has been downloaded, alert me with flashing text on the conky.
#Script should be run on start up, all printf output is here for merely debugging purposes.
clear
#Below: Make sure time is up to date incase VM has slewed from hibernation.
/usr/bin/expect<< EOD
#exp_internal 1
spawn sudo ntpdate -u pool.ntp.org
expect -re "kingghidorah:"
send "passwordhere\r"
#interact
EOD
#Below: Sets the current date to the variable "today".
today=$(date +%Y%m%d)
printf "\n\n\n"
printf "Today is $today %s\n\n"
sleep 2
#Makes sure you have your Base Directory created and populated
if [[ -d ~/SecurityNow ]]
then
cd ~/SecurityNow
#Sets the newest file to the formated date. We need to compare the last file you downloaded with the latest file on the server. Below will list all of the files in the SecurityNow directory. It first changes their listing format to Year-month-day. Thenarrange by date modified. This --sort=time stacks the newest file on top. Then the list is passed to awk which cuts out the top most file (the newest). The print, prints out the 6th field, which is the date and finally stores it in the variable "newest".
newest=$(ls --time-style=+%Y%m%d -lc --sort=time | awk '{if(NR==2)print $6}')
printf "Your last file is dated as $newest %s\n"
sleep 4
#Below: If the directory is missing or empty then quit on failure.
else
echo "Could not find needed files to intitiate sync with Server. Check your ~/SecurityNow directory"
exit 1
fi
# Ping google. to make sure internet and DNS connectivity are good.
ping -c 2 google.com > /dev/null
if [[ $? = 0 ]]
then
printf "Internet Connection Established %s\n\n"
#If the ping fails, quits on failure.
else
printf "No Direct Internet Connection Can Be Made...Exiting %s\n\n "
exit 1
fi
#Here is the actual logic...Compares the 'today'(today's date) value with the 'newest' (date of the newest episode) value of the newest file in the directoy. If the 'today' date is newer, initiate the check to see if a newer episode is available.
sleep 3
printf "Checking today's date.... \n "
sleep 2
#Below: If Today's date is greater than the date of the last fetched file
if [[ $(( $today - $newest )) -ge 0 ]]
then
printf "Let's check the server! \n "
#If if not, then exit but with success
else
printf "No new Episode will be available. \n"
exit 0
fi
#Checking Caching Server
cd ~/SecurityNow
#Gets the most current episode in the directory on your local machine.The ls -lc will print the date modified instead of when
lastep=$( ls | tail -1 | sed "s/.mp3//" )
#lastep=$( ls -lc | tail -1 | awk '{print $9}'| rename -n y/a-z./' '/ | awk '/renamed/ {print $4}')
#Below: if the lastep variable has a value (it exists) then...
if [[ -n $lastep ]]
then
#Increment the current episode by 1 so we can get the next episode
currentep=`expr $lastep + 1`
printf "Your current episode is $lastep \n\n"
sleep 3
printf "Fetching Episode: $currentep \n\n\n"
sleep 3
#Below: proceed to check server. Note that the variables are in the path name to be checked. If the wget returns a success, it means that there is a present file to grab.Proceed to wget the file from that location. If it doesn't return a success, it means no file is present, and will return some text to my conky.
wget --server-response --spider twit.cachefly.net/audio/sn/sn0"$currentep"/sn0"$currentep".mp3
if [[ $? -eq 0 ]]
then
printf "Congrats! New Episode Availible! Downloading... %s\n\n"
sleep 2
wget twit.cachefly.net/audio/sn/sn0"$currentep"/sn0"$currentep".mp3
sleep 3
else
printf "No new episode yet! \n"
~/Scripts/put_notext.sh
exit 1
fi
fi
#Below: Conky will display text if a new episode was recently downloaded
printf "New Episode ready to go! Enjoy! \n"
~/Scripts/put_text.sh
exit 0
Last edited by Horizon_Brave (2015-12-14 01:59:23)
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
Have not tested yet, but looks interesting, notes:
a. don't use ls in scripts
https://github.com/koalaman/shellcheck/wiki/SC2012
b. Use http://www.shellcheck.net/ to check your scripts
Offline
Have not tested yet, but looks interesting, notes:
a. don't use ls in scripts
https://github.com/koalaman/shellcheck/wiki/SC2012
b. Use http://www.shellcheck.net/ to check your scripts
Thanks for that! The wiki link clears up other `ls`-related mysteries as well.
Using the Openbox (3.5.2) session of Lubuntu 14.04 LTS but very interested in BL :)
Offline
Have not tested yet, but looks interesting, notes:
a. don't use ls in scripts
https://github.com/koalaman/shellcheck/wiki/SC2012
b. Use http://www.shellcheck.net/ to check your scripts
Bronto, thanks! I was looking for a way to check by mtime!! Looking at the syntax I can specify percisely to return files older than a certain number of days old... or last access (atime) to see if I actually listened to it after downloading!
Danke!!
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
Also, I'm liking the shellcheck...the documentation still seems a work in progress, but at least he's providing the rationale behind it!
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
Actually that wiki should probably say: Use globs instead of ls (find is just a consequence).
http://mywiki.wooledge.org/BashGuide/Patterns
Offline