More Debt Watching from the Command Line
Those of you who watch our Tech Tip Videos may have seen my video on how to fetch the US National Debt at the command line. The script contained here uses the idea I developed there and expands it to fetch the debt twice, with an optional pause in between, and then display the amount that the debt has increased during the pause.
You can use the script in the following fashion:
bash ndebt.sh 10
bash ndebt.sh 3:10
bash ndebt.sh 1:15:10
The argument to the script is the pause time, which can be given in seconds, or minutes:seconds, or hours:minutes:seconds. The script fetches the debt at the start, pauses for the requested amount of time, then fetches the debt again and prints out the amount the debt has increased.
For example, here we run the script and specify a time of 20 seconds:
$ bash ndebt.sh 20
During the last 20 seconds the US National Debt has increased by 901128.13
The script itself follows:
#!/bin/bash
# Check for pause time.
pause_time=0
if [[ "$1" ]]; then
if [[ $1 =~ ^([0-9]+):([0-9]+):([0-9]+)$ ]]; then
pause_time=$(((${BASH_REMATCH[1]}*60*60) + (${BASH_REMATCH[2]}*60) + (${BASH_REMATCH[3]})))
elif [[ $1 =~ ^([0-9]+):([0-9]+)$ ]]; then
pause_time=$(((${BASH_REMATCH[1]}*60) + (${BASH_REMATCH[2]})))
elif [[ $1 =~ ^([0-9]+)$ ]]; then
pause_time=$1
else
echo "Bad pause time: $1" >&2
exit 1
fi
fi
# Get national debt.
function get_debt()
{
local t=$(wget --quiet http://brillig.com/debt_clock -O - | grep debtiv.gif)
t=$(sed -e 's/.*ALT="\$//' -e 's/".*//' -e 's/[ ,]//g' <<<$t)
echo $t
}
# Print time item.
# Pass time value and one of 'hour', 'minute', 'ssecond'.
function fmt_time()
{
local t=$1
local p=$2
if [[ $t -gt 0 ]]; then
if [[ $t -eq 1 ]]; then
printf '%d %s' $t $p
else
printf '%d %ss' $t $p
fi
fi
}
# Print the elapsed time between the first and second argument.
# Times given in seconds.
# Earliest time is first arugment.
function elapsed_time()
{
local st=$1
local et=$2
local dt=$((et - st))
local ds=$((dt % 60))
local dm=$(((dt / 60) % 60))
local dh=$((dt / 3600))
echo $(fmt_time $dh 'hour') $(fmt_time $dm 'minute') $(fmt_time $ds 'second')
}
######################################################################
stime=$(date +%s)
sdebt=$(get_debt)
if [[ $pause_time -gt 0 ]]; then sleep $pause_time; fi
etime=$(date +%s)
edebt=$(get_debt)
t=$(elapsed_time $stime $etime)
d=$(bc <<<"scale=2; $edebt - $sdebt")
printf "During the last %s the US National Debt has increased by %s\n" "$t" "$d"
## vim: tabstop=4: shiftwidth=4: noexpandtab:
## kate: tab-width 4; indent-width 4; replace-tabs false;
The first part of the script uses bash's regular expression comparison operator (=~) to see if the passed argument is a valid time. Then come a few functions.
The first function, get_debt(), fetches the debt page from brillig.com and then extracts and cleans up the debt number. The debt number is extracted from the alt attribute of the image at the top of the page. The string there contains spaces and commas and a dollar sign, those are removed to give us a usable number.
The second function fmt_time() formats a time component for our output. For example, fmt_time 2 second prints "2 seconds", fmt_time 0 second prints "" (an empty string).
The third function, elapsed_time() takes two time values in seconds and calculates the difference between the two. It then formats the elapsed time in a readable fashion. For example elapsed_time 0 3659 prints "1 hour 59 seconds".
After that comes the main code. It records the start time, fetches the starting debt and then pauses. After the pause it records the end time and fetches the ending debt. Then bc is used to calculate the difference and the result is printed. Note that the time printed is the actual elapsed time and so it may be slightly different than the elapsed time you request on the command line, but usually only by a second or two.
| Attachment | Size |
|---|---|
| ndebt.sh_.txt | 1.66 KB |
Mitch Frazier is an Associate Editor for Linux Journal.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- seo services in india
1 hour 32 min ago - For KDE install kio-mtp
1 hour 32 min ago - Evernote is much more...
3 hours 33 min ago - Reply to comment | Linux Journal
12 hours 18 min ago - Dynamic DNS
12 hours 52 min ago - Reply to comment | Linux Journal
13 hours 51 min ago - Reply to comment | Linux Journal
14 hours 41 min ago - Not free anymore
18 hours 43 min ago - Great
22 hours 30 min ago - Reply to comment | Linux Journal
22 hours 38 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
bash version
Mitch,
There is a discrepancy in my bash version:
[amos@localhost ~]$ bash --version
GNU bash, version 3.1.17(1)-release (i586-mandriva-linux-gnu)
Synaptics says my bash version is 3.1-8.
No It's OK
When the version number contains a -NUMBER that's a package release number, which is the number of times the package was built with the same version of the software. So, 3.1-8 means the package itself has been built 8 times with version 3.1 of bash. So, the "fuller" version, 3.1.17, is still version 3.1.
See the answer to the comment below as to how you can fix the script so it should run with that version of bash.
Mitch Frazier is an Associate Editor for Linux Journal.
Hi Mitch, I'm running PCLos
Hi Mitch,
I'm running PCLos 2009.1 with bash 3.1.17
Amos
Quotes Needed with Bash 3.1
My system has bash 3.2. One of the improvements in version 3.2 was that you no longer needed to include quotes around regular expressions used with =~. Moreover, in version 3.2, regular expressions with quotes don't seem to work as you expect. Therefore, for 3.1 try adding quotes around the regular expressions in the =~ comparisons near the top of the file:
Mitch Frazier is an Associate Editor for Linux Journal.
bash script
Thanks a lot Mitch, it works perfectly.
National Debt Script
I copied and pasted your script and then ran it. This is the error message I received:
[amos@localhost 0]$ ./natldebt.sh 10
./natldebt.sh: line 8: syntax error in conditional expression: unexpected token `('
./natldebt.sh: line 8: syntax error near `^(['
./natldebt.sh: line 8: ` if [[ $1 =~ ^([0-9]+):([0-9]+):([0-9]+)$ ]]; then'
Here's hoping it is my fault. I didn't check your script against the copied text.
Regards,
Amos
Bash Version
What version of bash do you have?
Mitch Frazier is an Associate Editor for Linux Journal.