5

I'm trying write a script which shows right statement based on which day of the week it is. Two examples:

  1. If today is 4th day of the week. echo Today is a working day.
  2. If today is 6th day of the week. echo Today is a weekend.

I wrote this however it doesn't work

echo Hello!
echo Today's date is: date
DAY=$(date +"%u")
if [ "${DZIEN}" -ge 1 && "${DZIEN}" -le 5 ]
then 
   echo WORKING DAY;
else
   echo WEEKEND;
fi
  • 1
    you set DAY, then check DZIEN... pick one. And perhaps try -a instead &&. – frostschutz Oct 20 '16 at 16:20
  • You need [..] && [..] or [[.. && ..]] - why don't you google for shell multiple conditions in if ?? It's much shorter to type than all the above... – don_crissti Oct 20 '16 at 16:21
  • @frostschutz I made a mistake in copying variable name. – Ensz Oct 20 '16 at 16:26
  • @don_crissti I have to do this in that way, this is a task from the teacher :/ Thanks for help, now it's working. – Ensz Oct 20 '16 at 16:29
7

Try this :

echo "Today's date is: $(date)"
day=$(date +"%u")

if ((day > 5)); then
   echo "WEEKEND"        
else
   echo "WORKING DAY"
fi

I use (( )) bash arithmetic

Or less readable :

echo "Today's date is: $(date)"
day=$(date +"%u")

if [[ day -gt 5 ]]; then
   echo "WEEKEND"        
else
   echo "WORKING DAY"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.