The Bash Scripting Tutorial, Part 3

Using tests and loops in Bash

Vickie Li
Vickie Li
May 14, 2020 · 5 min read

Our script so far

#!/bin/bashBIRTHDATE=$(date -jf “%Y-%m-%d” $2 +%s)if [ $3 ]
then
NEW_DATE=$(date -jf “%Y-%m-%d” $3 +%s)
DIFF_IN_SECONDS=$(expr $NEW_DATE — $BIRTHDATE)
else
NOW=$(date -jf “%a %b %d %T %Z %Y” “$(date)” +%s)
DIFF_IN_SECONDS=$(expr $NOW — $BIRTHDATE)
fi
DIFF=$(expr $DIFF_IN_SECONDS / 86400)case $1 in
days)
;;
weeks)
DIFF=$(expr $DIFF / 7)
;;
months)
DIFF=$(expr $DIFF / 30)
;;
years)
DIFF=$(expr $DIFF / 365)
;;
esac
echo "You are $DIFF $1 old."
./calculate_age.sh MODE BIRTHDATE (NEW_DATE)
$ ./calculate_age.sh days “2000–06–09” “2020–05–07”
You are 7272 days old.
$./calculate_age.sh weeks "2000-06-09" "2020-05-07"
You are 1038 weeks old.
$ ./calculate_age.sh months "2000-06-09" "2020-05-07"
You are 242 months old.

While Loops

./calculate_age.sh MODE BIRTHDATE (NEW_DATE) (NEW_DATE) ...
$ ./calculate_age.sh months "2000-06-09" "2020-05-07" "2020-06-08" 
On 2020-05-07, you are 7272 days old.
On 2020-06-08, you are 7304 days old.
while CONDITION 
do
DO SOMETHING
done
shift
shift
while test $# -gt 0
do
NEW_DATE=$(date -jf “%Y-%m-%d” $1 +%s)
DIFF_IN_SECONDS=$(expr $NEW_DATE - $BIRTHDATE)
DIFF=$(expr $DIFF_IN_SECONDS / 86400)
echo "On $1, you are $DIFF days old."
shift
done
$ ./calculate_age.sh days “2000–06–09” “2020–05–07” “2020–05–08”
man test

For loops

for i in LIST_OF_VALUES
do
DO SOMETHING
done
for i in "${@:3}"
do
NEW_DATE=$(date -jf “%Y-%m-%d” $i +%s)
DIFF_IN_SECONDS=$(expr $NEW_DATE — $BIRTHDATE)
DIFF=$(expr $DIFF_IN_SECONDS / 86400)
echo "On $1, you are $DIFF days old."
done
"${INPUT_ARRAY:START_INDEX:END_INDEX}"

Putting it all together

#!/bin/bashMODE=$1
BIRTHDATE=$(date -jf “%Y-%m-%d” $2 +%s)
case $MODE in
days)
UNIT=1
;;
weeks)
UNIT=7
;;
months)
UNIT=30
;;
years)
UNIT=365
;;
esac
if [ $3 ]
then
for i in "${@:3}"
do
NEW_DATE=$(date -jf “%Y-%m-%d” $i +%s)
DIFF_IN_SECONDS=$(expr $NEW_DATE — $BIRTHDATE)
DIFF=$(expr $DIFF_IN_SECONDS / 86400)
DIFF=$(expr $DIFF / $UNIT)
echo "On $i, you are $DIFF $MODE old."
done
else
NOW=$(date -jf “%a %b %d %T %Z %Y” “$(date)” +%s)
DIFF_IN_SECONDS=$(expr $NOW — $BIRTHDATE)
DIFF=$(expr $DIFF_IN_SECONDS / 86400)
DIFF=$(expr $DIFF / $UNIT)
echo "You are $DIFF $MODE old."
fi
$ ./calculate_age.sh months "2000-06-09" "2020-05-07" "2021-06-08"
On 2020-05-07, you are 242 months old.
On 2021-06-08, you are 255 months old.

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Vickie Li

Written by

Vickie Li

Professional investigator of nerdy stuff. Hacks and secures. Creates god awful infographics. https://twitter.com/vickieli7

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Vickie Li

Written by

Vickie Li

Professional investigator of nerdy stuff. Hacks and secures. Creates god awful infographics. https://twitter.com/vickieli7

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store