Quantcast

Bash scripting

canadmos

Cake Tease
May 29, 2011
21,943
21,465
Canaderp
Does anyone here write scripts in Bash?

Are there any tutorials or guides that are easy to read?

I have an assignment to do in an open systems automation class, but we've been thrown to the wolves pretty much and have to figure it out on our own.
 
Last edited:

canadmos

Cake Tease
May 29, 2011
21,943
21,465
Canaderp
Thanks for the links!

Not sure if you have to be authenticated to view this, but this is the assignment;

https://scs.senecac.on.ca/~john.selmys/subjects/ops435-123/ass01.html

For this assignment you will design and code a new BASH command (script) named "showtime". The purpose of "showtime" is to display the current date and time for a given city. Here is the Linux manual page for this command.
Basically, a user enters a city or a region/city combination and the script has to search for the place in the timezone directory.
 
Last edited:

canadmos

Cake Tease
May 29, 2011
21,943
21,465
Canaderp
Well after an hour of tinkering I got the easy part almost done....

Code:
#!/bin/bash
# OPS435 - Assignment1
# Written by Chris Gaston - October 2012
#
# Check for valid input
if [ $# -eq 0 ] #Checks for user input
	then
		echo -e "\nUsage: showtime location\n"
		exit 1
elif [[ $1 == '-a' ]] #Checks to see if -a was input to enter new city
	then
		echo -e "\n$1 was input.\n"
elif [ $# -eq 1 ]
	then
		location="$1"
		#echo -e "\nTesting purposes. Location variable = $location\n" 
		if [[ "$location" == *'/'* ]] #Check if there is a slash in $location
			then
				region=$(echo $location | cut -f1 -d'/')
				city=$(echo $location | cut -f2 -d'/')
				#echo -e "\nRegion variable = $region\n" #TAKE THIS OUT. TESTING ONLY
				#echo -e "\nCity variable = $city\n" #TAKE THIS OUT. TESTING ONLY
				case $region in #Select a case based on input region value
					Africa) citysearch=`find /usr/share/zoneinfo/Africa -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					America) citysearch=`find /usr/share/zoneinfo/America -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Antarctica) citysearch=`find /usr/share/zoneinfo/Antarctica -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Arctic) citysearch=`find /usr/share/zoneinfo/Arctic -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Asia) citysearch=`find /usr/share/zoneinfo/Asia -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Atlantic) citysearch=`find /usr/share/zoneinfo/Atlantic -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Australia) citysearch=`find /usr/share/zoneinfo/Australia -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Europe) citysearch=`find /usr/share/zoneinfo/Europe -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Indian) citysearch=`find /usr/share/zoneinfo/Indian -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					Pacific) citysearch=`find /usr/share/zoneinfo/Pacific -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0 
						fi ;;
					*) echo -e "\n$region is an invalid region.\n"
				   	   exit 2;;
				esac
		else
			citysearch=`find /usr/share/zoneinfo/ -name $location`
			if [[ $citysearch == "" ]]
				then
					echo -e "\n$location not in database.\n"
					exit 1
				else
					echo -e "\n`TZ="$citysearch" date`\n"
					exit 0
			fi #[[ $citysearch == "" ]]
					
		fi #[[ "$location" == *'/'* ]] #Check if there is a slash in $location
fi #[ $# -eq 0 ] #Checks for user input
And now the hard part... :confused::confused::confused:

Could probably be a lot more compact too...oh well
 
Last edited:

canadmos

Cake Tease
May 29, 2011
21,943
21,465
Canaderp
WELP. I finished it, but there are still validation errors and whatnot. If you run linux, save this as 'showtime', give it some chmod u+x permissions and see if you can get it to work.

Code:
#!/bin/bash
# OPS435 - Assignment1
# Written by Chris Gaston - October 2012
# Showtime script used to display a specific cities time and date information
#
#
#
TZDIR=/home/$USER/showtimedir
if [ $# -eq 0 ] #Checks for user input
	then
		echo -e "\nUsage: showtime location\n"
		exit 1
else
	
		showtimedircheck=`find /home/$USER/showtimedir -maxdepth 0 -print0 2> /dev/null`
		if [[ "$showtimedircheck" != "/home/$USER/showtimedir" ]]
			then
				mkdir "/home/$USER/showtimedir"
				cp --no-preserve=ownership -r /usr/share/zoneinfo/{zone.tab,Africa,America,Antarctica,Arctic,Asia,Atlantic,Australia,Europe,Indian,Pacific} /home/$USER/showtimedir
		fi
if [[ $1 == '-a' ]] #Checks to see if -a was input to enter new city
	then
		newcity=$2
		latitude=$3
		longitude=$4
		if [ "$longitude" = "" ]
			then
				echo -e "\nUsage: showtime -a CityName latitude longitude\n"
			else		
				checkcity=`find /home/$USER/showtimedir/{Africa,America,Antarctica,Arctic,Asia,Atlantic,Australia,Europe,Indian,Pacific} -name $newcity`
				if [[ "$checkcity" != "" ]] #Check if new city is already in database
					then
						echo -e "\n$newcity is already in database.\n"
						exit 2
					else
						cutlatitude=`echo $latitude | cut -b -2`
						cutlongitude=`echo $longitude | cut -b -3`						
						newlocationregion=`grep -m1 "$cutlatitude.*$cutlongitude.*" /home/$USER/showtimedir/zone.tab | cut -f 3 | cut -f 1 -d'/'`
						newlocationcity="/home/$USER/showtimedir/`grep -m1 "$cutlatitude.*$cutlongitude.*" /home/$USER/showtimedir/zone.tab | cut -f 3`"						
						cp $newlocationcity "/home/$USER/showtimedir/$newlocationregion/$newcity"
						#Now append information to zone.tab file
						appendinfo=`grep -m1 "$cutlatitude.*$cutlongitude.*" /home/$USER/showtimedir/zone.tab | cut -f1`
						appendinfo="$appendinfo		$latitude$longitude	$newlocationregion/$newcity"	
						echo $appendinfo >> /home/$USER/showtimedir/zone.tab
						echo -e "\n$newcity added to $newlocationregion region.\n"
						exit 0
				fi #[[ "$checkcity" != "" ]]
		fi #[[ "$newcity|$latitude|$longitude" == "" ]]
elif [ $# -eq 1 ]
	then
		location="$1"
		if [[ "$location" == *'/'* ]] #Check if there is a slash in $location
			then
				region=$(echo $location | cut -f1 -d'/')
				city=$(echo $location | cut -f2 -d'/')
				case $region in #Select a case based on input region value
					Africa) citysearch=`find /home/$USER/showtimedir/Africa -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					America) citysearch=`find /home/$USER/showtimedir/America -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Antarctica) citysearch=`find /home/$USER/showtimedir/Antarctica -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Arctic) citysearch=`find /home/$USER/showtimedir/Arctic -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Asia) citysearch=`find /home/$USER/showtimedir/Asia -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Atlantic) citysearch=`find /home/$USER/showtimedir/Atlantic -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Australia) citysearch=`find /home/$USER/showtimedir/Australia -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Europe) citysearch=`find /home/$USER/showtimedir/Europe -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Indian) citysearch=`find /home/$USER/showtimedir/Indian -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					Pacific) citysearch=`find /home/$USER/showtimedir/Pacific -name $city`
						if [[ $citysearch == "" ]]
							then
								echo -e "\n$city was not found in $region\n"
								exit 2
							else
								echo -e "\n`TZ="$region//$city" date`\n"
								exit 0
						fi ;;
					*) echo -e "\n$region is an invalid region.\n"
				   	   exit 2 ;;
				esac
		else
			citysearch=`find /home/$USER/showtimedir/{Africa,America,Antarctica,Arctic,Asia,Atlantic,Australia,Europe,Indian,Pacific} -name $location`			
			if [[ $citysearch == "" ]]
				then
					echo -e "\n$location not in database.\n"
					exit 2
				else
					echo -e "\n`TZ="$citysearch" date`\n"
					exit 0
			fi #[[ $citysearch == "" ]]
					
		fi #[[ "$location" == *'/'* ]] #Check if there is a slash in $location
	fi
fi #[ $# -eq 0 ] #Checks for user input