-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.sh
executable file
·41 lines (37 loc) · 948 Bytes
/
task1.sh
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
#!/bin/bash
# Function to calculate the Fibonacci number
fib() {
n=$1
if [ "$n" -le 0 ]; then
echo 0
elif [ "$n" -eq 1 ]; then
echo 1
else
a=0
b=1
for (( i=2; i<=n; i++ ))
do
temp=$b
b=$((a + b))
a=$temp
done
echo $b
fi
}
# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "Please provide a number."
else
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
if [[ "$1" =~ ^-?[0-9]+$ ]]; then
echo "Negative numbers are not allowed. Please enter a non-negative integer."
elif [[ "$1" =~ ^[0-9]+\.[0-9]+$ ]]; then
echo "Decimal numbers are not valid. Please enter a whole number."
else
echo "Invalid input. Please enter a valid number."
fi
else
result=$(fib $1)
echo "For your input number $1, the Fibonacci number is $result."
fi
fi