Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greet a user differently on the time of day - Bash Script

Tags:

bash

shell

unix

I need to greet a user (using “Good morning”, “Good afternoon”, or “Good evening”) depending on the time of day.

I have already gained the users details ($userTitle $userName) however I am not sure how to greet someone differently depending on the time... any ideas?

like image 825
J Homard Avatar asked Jan 15 '13 20:01

J Homard


People also ask

What is the meaning of 2 >& 1 in bash?

The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").

What does $@ mean in bash scripting?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does $() mean in bash?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What is Echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


5 Answers

h=`date +%H`

if [ $h -lt 12 ]; then
  echo Good morning
elif [ $h -lt 18 ]; then
  echo Good afternoon
else
  echo Good evening
fi
like image 190
DigitalRoss Avatar answered Oct 20 '22 13:10

DigitalRoss


You could get the time like this:

TIME=$(date "+%H")

Then act on that value i.e

if [ $TIME -lt 12 ]; then
    echo "Good morning"
elif [ $TIME -lt 18 ]]; then
    echo "Good afternoon"
else
    echo "Good evening"
fi
like image 44
jgr Avatar answered Oct 20 '22 14:10

jgr


Try doing this :

TIME=$(date "+%k")

if ((TIME < 12 )); then
    echo "Good morning"
elif ((TIME < 18 )); then
    echo "Good afternoon"
else
    echo "Good evening"
fi

NOTE

  • with this syntax, no need to remember -ge and such. This is just like arithmetic
  • ((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for let, if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression
like image 22
Gilles Quenot Avatar answered Oct 20 '22 14:10

Gilles Quenot


hour=`date +%H`
if [ $hour -le 12 ]; then
    echo 'good morning'
elif [ $hour -ge 18 ]; then
    echo 'good evening'
else
    echo 'good afternoon'
fi
like image 1
Adam Sznajder Avatar answered Oct 20 '22 13:10

Adam Sznajder


All other answers is correct except one detail. Command date +%H return number hours in format XX ( for example if time is 09:00:00, then it return "09" ). In bash numbers started with zero is octal numbers. So this nuance can cause errors.

For example:

if [ 09 > 10 ] 
then
    echo "it's something strange here"
fi

will print "it's something strange here".

Probably you chose time intervals, who are not cause such behavior. But for insurance you can write:

hours=date +"%H" | sed -e 's/^0//g'

Be carefull.

like image 1
Nikolai Popov Avatar answered Oct 20 '22 13:10

Nikolai Popov