Monday, January 31, 2022

[SOLVED] Unix date program, nested if statement about what time it is now and echo's a line depending on the time

Issue

I have this code that is supposed to see whether it's the weekend or not and if it is not the weekend, then it will print a certain line. However, I am getting an error with the second if statement. here is my code

#!/bin/sh
# A daily reminder service
set `date`
echo "Remember for today:"
if [ $1 == "Sat" ] | [ $1 == 'Sun' ]
then
    echo "it is the weekend"
else
    if [ "5:55:00" < $4 < "6:05:59" ] 
    then 
        echo "get up"
    elif [ "6:55:00" < $4 < "7:05:59" ] 
    then
        echo "have breakfast"
    elif [ "7:55:00" < $4 < "8:05:59" ] 
    then
        echo "start the first class"
    elif [ "21:55:00" < $4 < "22:05:59" ] 
    then
        echo "time to do homework"
    else
        echo "There's nothing for you to do!"
    fi
fi

My error looks like this

[cpm1051@srvlp-dpt-cs01 labfifteen]$ ./dailyReminder
Remember for today:
./dailyReminder: line 9: 10:29:33: No such file or directory
./dailyReminder: line 12: 10:29:33: No such file or directory
./dailyReminder: line 15: 10:29:33: No such file or directory
./dailyReminder: line 18: 10:29:33: No such file or directory
There's nothing for you to do!

I am not sure what file it's looking for? it already captured the date. What am I doing wrong?

Any help will be greatly appreciated!


Solution

You can try splitting them and using "and" operator:

if [[ "14:55:00" < $4 ]] & [[ $4 < "18:05:59" ]]

Ensure you are using double square brackets[[]] and maintaining a space before and after them.



Answered By - Learner
Answer Checked By - Senaida (WPSolving Volunteer)