-
Notifications
You must be signed in to change notification settings - Fork 0
/
degf2degc
executable file
·90 lines (88 loc) · 2.59 KB
/
degf2degc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# This script's framework was created using makescript
# version v1.0.2, dated 2019-07-18
# Makescript and its accompanying scripts are copyrighted
# by Aetesaki (c) 2018
#
# degf2degc (c) 2019 aetesaki
#
# ##################################################
# HISTORY:
# ##################################################
#
# * 2019-07-19 - v0.0.0 - First Creation
#
# ##################################################
# Initializing constants and variables
# ##################################################
# Setting constants
version="v0.0.0" # Sets version variable
lastEdited="2019-07-19" # Sets last edited variable" $scriptNamePath
[ ! -t 0 ] && pipe=$(cat) # if stdin is open dump stdin to $pipe
# Declaring variables
declare fahrenheit celcius # temperature variables
# Declare all variables to be used in the script
# ##################################################
# short help function
# ##################################################
shorthelp() {
# Edit as required
cat << EOF
Usage: degf2degc [fahrenheit]
degf2degc --help
degf2degc --version"
EOF
}
# ##################################################
# no arguments gives short help
# ##################################################
if [ "$#" == "0" ]; then
# check if $pipe is empty
if [ -z "$pipe" ]; then
# display shorthelp and exit
shorthelp
exit 1
else
# else set arguments to $pipe
set -- "$pipe"
fi
fi
# ##################################################
# -- help
# ##################################################
if [ "$1" == "--help" ]; then
shorthelp
# Write options and explanations
cat << EOF
--help Displays this text and exit.
--version Displays the version of the script and exit.
EOF
# Write information on arguments afterwards
# Write examples as required
exit 0
fi
# ##################################################
# -- version
# ##################################################
if [ "$1" == "--version" ]; then
cat << EOF
degf2degc ${version}, dated ${lastEdited}
(c) 2019 aetesaki
EOF
exit 0
fi
# ##################################################
# Main section
# ##################################################
# Write your script here
# copy first argument to $fahrenheit
fahrenheit=$1
# Check for valid argument
if ! [[ $fahrenheit =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]]; then
echo "Fahrenheit temperature measurement is not a number" >&2
exit 1
fi
# convert fahrenheit to celcius
celsius=$(awk "BEGIN {printf \"%.2f\n\", ($fahrenheit-32) * (5/9)}")
# echo the converted temperature
echo $celsius