I am new to Gnu/Linux and bash, and I am trying, unsuccessfully, to write a simple bash script to test if date +%H
is within a predefined range of hours.
Example:
hour='date +%H'
if [[ $hour -ge 12 ]] || [[ $hour -lt 19 ]]
then echo "Good afternoon!"
Trying to isolate a line to this results in "integer expression expected":
test $hour -ge 12
It feels like I'm missing something simple to either have $hour return as integer or just handle it as a string.
Edit: Here's the completed script, any necessary improvements on the basic level?
!#/bin/bash
name=$(whoami)
hour=$(date +%H)
if [ $hour -lt 5 ] || [ $hour -ge 19 ]
then xmessage -center "Good evening $name!"
elif [ $hour -ge 5 ] && [ $hour -lt 12 ]
then xmessage -center "Good morning $name!"
else xmessage -center "Good afternoon $name!"
fi
date +%H
will return08
or09
-- and then bash arithmetic will complain about "value too great for base" because those are invalid octal numbers. Usedate +%_H
to get space-padded hours instead of zero-padded hours. – glenn jackman Dec 17 '20 at 17:42%k
(hour, 0-24). – Kusalananda Dec 17 '20 at 17:44zsh
:(){print Good ${argv[2+($1>11)+($1>18)]}.} ${(%):-%D{%H}} morning afternoon evening
– Stéphane Chazelas Dec 17 '20 at 18:03zsh -c 'what you just wrote'
from thebash
shell... – Kusalananda Dec 17 '20 at 18:04#!
at the start of the first line should read nothing but#!
. Yours is swapped. Also, I would quote all variable expansions, and useprintf
to output variable data. See here and here. – Kusalananda Dec 17 '20 at 18:26