How to write a bash script that takes user input

Id10t

New Member
Joined
Nov 4, 2018
Messages
7
Reaction score
2
Credits
0
I need help writing 2 different Scripts the first is a Greeting Script based on the time of day. If the script is executed for example ./myscript 8 I should get a response of "Good Morning" if I execute ./myscript 14 I should get a response of "Good Afternoon'' and so on. I found some scripts online that automatically look up the current time and display the corresponding message by only entering ./myscript, but my Teacher wants us to have to enter the number representing the current time manually following the script command as stated above.

The second Script is a LeapYear script, you input a year and the script returns an answer whether it is a Leap year or not. Again I found a script online for this that ask you to enter the Year after the script is executed and again the Teacher wants the year entered manually following the script command like so:
./myleapYearscript 2016 These are the first scripts I have ever written/copied so I have no idea how to write it so they will take user input off the command line following the script execution, any help is always appreciated. Thank you

Here is the automatic Greeting Script that I am using that needs to be altered to accept manual input:
h=`date +%H`
if [ $h -lt 11 ]; then
echo Good morning
elif [ $h -lt 17 ]; then echo Good afternoon
else echo Good evening
fi
 


I figured it out, Linux makes me want to pull my hair out sometimes, hopefully, as time goes on it will get easier. Here are the two solutions I came up with if anyone is interested. P.S. If anyone is wondering what the Error line is about on the Leapyear script, its because Leap Years were not observed until 1582. Also, I know there are 24 hours in a day but the hour scheme displayed is the way my Teacher wanted it.

myGreeting Script

arg=$1
if [ $1 -le 11 ]; then
echo Good Morning
elif [ $1 -le 17 ]; then echo Good Afternoon
elif [ $1 -le 23 ]; then echo Good Evening
else echo Error: Invalid Time Specified
fi

Leapyear Script


Arg=$1
if [ $1 -lt 1582 ]; then
echo Error: Invalid year && exit
fi
if [ $(($1 % 4)) -eq 0 ]
then
if [ $(($1 % 100)) -eq 0 ]
then
if [ $(($1 % 400)) -eq 0 ]
then
echo "It is a leap year"
else
echo "It is not a leap year"
fi
else
echo "It is a leap year"
fi
else
echo "It is not a leap year"
fi
 
Well Done !!
 


Top