Week 12/13: Assignment - Loops and Functions
This commit is contained in:
parent
49dc6e805f
commit
c9cfa9f8e4
67
week-13/add.sh
Executable file
67
week-13/add.sh
Executable file
|
@ -0,0 +1,67 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
# Champlain College SDEV-415-81
|
||||||
|
#
|
||||||
|
# Linux/Unix Programming Week 13: Loops and Function - (2020/12/06)
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Write a script named add that takes two command line arguments and displays as output their sum.
|
||||||
|
# The script is required to contain a function named add that takes
|
||||||
|
# these two arguments and returns their sum.
|
||||||
|
#
|
||||||
|
# Your script should also verify that the user entered two and only
|
||||||
|
# two arguments at the command line.
|
||||||
|
#
|
||||||
|
# If the user does not execute the program correctly,
|
||||||
|
# an error should be display describing the correct usage.
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
|
||||||
|
# Copyright (C) 2020. All Rights Reserved
|
||||||
|
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
|
||||||
|
# addition function
|
||||||
|
function add() {
|
||||||
|
# do the sum ;)
|
||||||
|
num=$(echo $1 + $2 | bc)
|
||||||
|
# we return the sum
|
||||||
|
echo "The sum is $num"
|
||||||
|
}
|
||||||
|
|
||||||
|
# help function
|
||||||
|
function show_help {
|
||||||
|
echo "================================================================"
|
||||||
|
echo $1
|
||||||
|
cat << EOF
|
||||||
|
================================================================
|
||||||
|
Usage: ${0##*/:-} [OPTION...]
|
||||||
|
|
||||||
|
Provide two numbers as arguments
|
||||||
|
|
||||||
|
Example run:
|
||||||
|
${0##*/:-} 5 8
|
||||||
|
The sum is 13
|
||||||
|
===============================================================
|
||||||
|
EOF
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# check enough arguments were passed
|
||||||
|
if [ $# -eq 2 ]; then
|
||||||
|
# check input is numbers
|
||||||
|
regex='^[0-9]+([\.][0-9]+)?$'
|
||||||
|
if [[ $1 =~ $regex && $2 =~ $regex ]]; then
|
||||||
|
# run main function
|
||||||
|
add "$1" "$2"
|
||||||
|
else
|
||||||
|
show_help "Error: Not a number"
|
||||||
|
fi
|
||||||
|
elif [ $# -gt 2 ]; then
|
||||||
|
show_help "Error: Too many arguments"
|
||||||
|
else
|
||||||
|
show_help "Error: Not enough arguments"
|
||||||
|
fi
|
137
week-13/foodmenu.sh
Executable file
137
week-13/foodmenu.sh
Executable file
|
@ -0,0 +1,137 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
# Champlain College SDEV-415-81
|
||||||
|
#
|
||||||
|
# Linux/Unix Programming Week 13: Loops and Function - (2020/12/06)
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# The Program will display a menu that a user can select an option from and get a little note ;)
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Write a script that will do the following:
|
||||||
|
# Provide a comment section at the top of the script,
|
||||||
|
# with your name, the date, and the purpose of the program.
|
||||||
|
#
|
||||||
|
# Use the select loop to produce a menu of foods.
|
||||||
|
# Produce output to resemble the following:
|
||||||
|
# 1) Steak and potatoes
|
||||||
|
# 2) Fish and chips
|
||||||
|
# 3) Soup and salad
|
||||||
|
# --------------------------
|
||||||
|
# Please make a selection. 1
|
||||||
|
# Stick to your ribs
|
||||||
|
# Watch your cholesterol.
|
||||||
|
# Enjoy your meal.
|
||||||
|
# --------------------------
|
||||||
|
# Please make a selection. 2
|
||||||
|
# British are coming!
|
||||||
|
# Enjoy your meal.
|
||||||
|
# --------------------------
|
||||||
|
# Please make a selection. 3
|
||||||
|
# Health foods…
|
||||||
|
# Dieting is so boring.
|
||||||
|
# Enjoy your meal.
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
|
||||||
|
# Copyright (C) 2020. All Rights Reserved
|
||||||
|
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
|
||||||
|
# basic main function ;)
|
||||||
|
function main() {
|
||||||
|
# some house cleaning
|
||||||
|
PS3_old=$PS3
|
||||||
|
# some defaults
|
||||||
|
export PS3="Please make a selection: "
|
||||||
|
# Start our little Menu
|
||||||
|
select menu in \
|
||||||
|
'Steak and potatoes' \
|
||||||
|
'Fish and chips' \
|
||||||
|
'Soup and salad' \
|
||||||
|
'Done'
|
||||||
|
do
|
||||||
|
case $REPLY in
|
||||||
|
1 ) ribsNotice;;
|
||||||
|
2 ) britishNotice;;
|
||||||
|
3 ) healthNotice;;
|
||||||
|
4 )
|
||||||
|
# final greeting
|
||||||
|
echo
|
||||||
|
echo "Goodbye! ${USER^}"
|
||||||
|
echo
|
||||||
|
exit 0;;
|
||||||
|
* )
|
||||||
|
echo "Error select option 1..4";;
|
||||||
|
esac
|
||||||
|
# we can add a normal break here if the loop must just run once
|
||||||
|
# break;
|
||||||
|
done
|
||||||
|
# restore the default
|
||||||
|
export PS3=$PS3_old
|
||||||
|
}
|
||||||
|
|
||||||
|
# Welcome...
|
||||||
|
function welcomeNotice() {
|
||||||
|
# get the current date
|
||||||
|
current_date=$(date +"%A %-d %b %Y");
|
||||||
|
# set at the top of the menu (script)
|
||||||
|
echo "My name is Llewellyn"
|
||||||
|
echo "Today is $current_date"
|
||||||
|
echo "Welcome to the Café Prestige"
|
||||||
|
echo
|
||||||
|
echo "Here is our menu for the day."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just to show the menu again (not ideal)
|
||||||
|
function reminderNotice(){
|
||||||
|
echo
|
||||||
|
echo "Would you like another?"
|
||||||
|
echo "1) Steak and potatoes"
|
||||||
|
echo "2) Fish and chips"
|
||||||
|
echo "3) Soup and salad"
|
||||||
|
echo "4) Done"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Show the welcome notice
|
||||||
|
welcomeNotice
|
||||||
|
|
||||||
|
# The menu notice for
|
||||||
|
# Steak and potatoes
|
||||||
|
function ribsNotice() {
|
||||||
|
echo
|
||||||
|
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
||||||
|
echo "Stick to your ribs"
|
||||||
|
echo "Watch your cholesterol."
|
||||||
|
echo "Enjoy your meal."
|
||||||
|
echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
||||||
|
reminderNotice
|
||||||
|
}
|
||||||
|
# The menu notice for
|
||||||
|
# Fish and chips
|
||||||
|
function britishNotice() {
|
||||||
|
echo
|
||||||
|
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
||||||
|
echo "British are coming!"
|
||||||
|
echo "Enjoy your meal."
|
||||||
|
echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
||||||
|
reminderNotice
|
||||||
|
}
|
||||||
|
# The menu notice for
|
||||||
|
# Soup and salad
|
||||||
|
function healthNotice() {
|
||||||
|
echo
|
||||||
|
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
||||||
|
echo "Health foods…"
|
||||||
|
echo "Dieting is so boring."
|
||||||
|
echo "Enjoy your meal."
|
||||||
|
echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
||||||
|
reminderNotice
|
||||||
|
}
|
||||||
|
|
||||||
|
# run main.. lol
|
||||||
|
main
|
91
week-13/loops_functions.log
Normal file
91
week-13/loops_functions.log
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
Script started on 2020-12-07 06:17:57+02:00 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="130" LINES="27"]
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » exit./add.sh 3 64.2r[K[K[5@foodmenu[C[C[C[Cmchecker[C[C[C[C[8Pls -la./mchecker.sh
|
||||||
|
Llewellyn, You have new mail
|
||||||
|
Llewellyn, You have new mail
|
||||||
|
^C
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » ./mchecker.sh exit[K./add.sh 3 64.2r[K[K[5@foodmenu[C[C[C[C
|
||||||
|
My name is Llewellyn
|
||||||
|
Today is Monday 7 Dec 2020
|
||||||
|
Welcome to the Café Prestige
|
||||||
|
|
||||||
|
Here is our menu for the day.
|
||||||
|
1) Steak and potatoes
|
||||||
|
2) Fish and chips
|
||||||
|
3) Soup and salad
|
||||||
|
4) Done
|
||||||
|
Please make a selection: 6
|
||||||
|
Error select option 1..4
|
||||||
|
Please make a selection: 1
|
||||||
|
|
||||||
|
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||||
|
Stick to your ribs
|
||||||
|
Watch your cholesterol.
|
||||||
|
Enjoy your meal.
|
||||||
|
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||||
|
|
||||||
|
Would you like another?
|
||||||
|
1) Steak and potatoes
|
||||||
|
2) Fish and chips
|
||||||
|
3) Soup and salad
|
||||||
|
4) Done
|
||||||
|
Please make a selection: 2
|
||||||
|
|
||||||
|
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||||
|
British are coming!
|
||||||
|
Enjoy your meal.
|
||||||
|
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||||
|
|
||||||
|
Would you like another?
|
||||||
|
1) Steak and potatoes
|
||||||
|
2) Fish and chips
|
||||||
|
3) Soup and salad
|
||||||
|
4) Done
|
||||||
|
Please make a selection: 3
|
||||||
|
|
||||||
|
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||||
|
Health foods…
|
||||||
|
Dieting is so boring.
|
||||||
|
Enjoy your meal.
|
||||||
|
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||||||
|
|
||||||
|
Would you like another?
|
||||||
|
1) Steak and potatoes
|
||||||
|
2) Fish and chips
|
||||||
|
3) Soup and salad
|
||||||
|
4) Done
|
||||||
|
Please make a selection: 4
|
||||||
|
|
||||||
|
Goodbye! Llewellyn
|
||||||
|
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » ./foodmenu.sh mchecker[C[C[C[Cexit[K./add.sh 3 64.2r[K[K[5@foodmenu[C[C[C[C[5Padd[C[C[C[C
|
||||||
|
================================================================
|
||||||
|
Error: Not enough arguments
|
||||||
|
================================================================
|
||||||
|
Usage: ./add.sh [OPTION...]
|
||||||
|
|
||||||
|
Provide two numbers as arguments
|
||||||
|
|
||||||
|
Example run:
|
||||||
|
./add.sh 5 8
|
||||||
|
The sum is 13
|
||||||
|
===============================================================
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » ./add.sh [5@foodmenu[C[C[C[Cmchecker[C[C[C[Cexit[K./add.sh 3 64.2r[K
|
||||||
|
================================================================
|
||||||
|
Error: Not a number
|
||||||
|
================================================================
|
||||||
|
Usage: ./add.sh [OPTION...]
|
||||||
|
|
||||||
|
Provide two numbers as arguments
|
||||||
|
|
||||||
|
Example run:
|
||||||
|
./add.sh 5 8
|
||||||
|
The sum is 13
|
||||||
|
===============================================================
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » ./add.sh 3 r[K[5@foodmenu[C[C[C[Cmchecker[C[C[C[Cexit[K./add.sh 3 64.2
|
||||||
|
The sum is 67.2
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » ./add.sh 3 64.2[K342 64.34. 64.343 64.34
|
||||||
|
The sum is 96.64
|
||||||
|
]2; ~/Champlain/C/2_Week13_1[32;1m[0m2_Week13_1[0m » exit
|
||||||
|
exit
|
||||||
|
|
||||||
|
Script done on 2020-12-07 06:19:57+02:00 [COMMAND_EXIT_CODE="0"]
|
58
week-13/mchecker.sh
Executable file
58
week-13/mchecker.sh
Executable file
|
@ -0,0 +1,58 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
# Champlain College SDEV-415-81
|
||||||
|
#
|
||||||
|
# Linux/Unix Programming Week 13: Loops and Function - (2020/12/06)
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Write a program called mchecker to check for new mail and
|
||||||
|
# write a message to the screen if new mail has arrived.
|
||||||
|
# The program will get the size of the mail spool file for the user.
|
||||||
|
# (The spool files are found in /var/spool/mail/$USER on Linux,
|
||||||
|
# Use the find command if you cannot locate the file.)
|
||||||
|
# The script will execute in a continuous loop, once every 30 seconds.
|
||||||
|
# Each time the loop executes, it will compare the size of the mail spool
|
||||||
|
# file with its size from the previous loop. If the new size is greater than
|
||||||
|
# the old size, a message will be printed on your screen,
|
||||||
|
# saying “Username, You have new mail”.
|
||||||
|
# The size of a file can be found by looking
|
||||||
|
# at the output from ls –l, wc –c or from the find command.
|
||||||
|
#
|
||||||
|
# Note: To test this, you can simply edit the spool file to add a new entry.
|
||||||
|
# (echo "More data" >> /var/spool/mail/$USER)
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
#
|
||||||
|
# Written by Llewellyn van der Merwe <llewellyn.vandermerw@mymail.champlain.edu>, December 2020
|
||||||
|
# Copyright (C) 2020. All Rights Reserved
|
||||||
|
# License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
#
|
||||||
|
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
|
||||||
|
# path to the spool mail file
|
||||||
|
FILENAME=/var/spool/mail/$USER
|
||||||
|
# check if this file exist
|
||||||
|
if [ ! -f $FILENAME ]; then
|
||||||
|
# give console error
|
||||||
|
echo "The mail spool file for $USER does not exist in [$FILENAME]."
|
||||||
|
# break out here
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# the size tracker
|
||||||
|
LASTFILESIZE=0
|
||||||
|
# start the continuous loop
|
||||||
|
while true; do
|
||||||
|
FILESIZE=$(stat -c%s "$FILENAME")
|
||||||
|
if [[ $FILESIZE -gt $LASTFILESIZE ]]; then
|
||||||
|
# only give message if LASTFILESIZE is greater than 0
|
||||||
|
if [[ $LASTFILESIZE -gt 0 ]]; then
|
||||||
|
# give notice to console
|
||||||
|
echo "${USER^}, You have new mail"
|
||||||
|
fi
|
||||||
|
# set the new size
|
||||||
|
LASTFILESIZE=$FILESIZE
|
||||||
|
fi
|
||||||
|
# run this check again every
|
||||||
|
# 30 seconds
|
||||||
|
sleep 30s
|
||||||
|
done
|
Loading…
Reference in New Issue
Block a user