1

For Monday, print Moanday For Friday, print Friyay For other days, print Foobar

input date: Fri July 20 10:02:15 IST 2018

Output: Friyay

See My Code Below:

day = $(date | cut -d' ' -f1)
if ["$day" == "Monday"];
then
    echo Moanday
elif ["$day" == "Friday"];
then
    echo Friyay
else
    echo Foobar
fi
Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    Paste your script with a shebang first at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Jul 16 '20 at 16:08

3 Answers3

1

With bash, GNU date and an array:

d=([1]=Moanday Foobar Foobar Foobar Friday Foobar Foobar)
echo "${d[$(date +%u)]}"
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0
#!/bin/bash
 
# From 'man date'
#-----------------------------------------
# %d     day of month (e.g., 01)
# %u     day of week (1..7); 1 is Monday
# %m     month (01..12) 
 #-----------------------------------------

DAY="$(date +%u)"
DATE="$(date +%d)"
MONTH="$(date +%m)"

case $DAY in
  "1")
    echo -e "Moanday"
    ;;
  "2")
    echo -e "Tueasday"
    ;;
  "3")
    echo -e "Wedneasday"
    ;;
  "4")
    echo -e "Thurasday"
    ;;
  "5")
    echo -e "Friday"
    [[ $DATE = "13" ]] && echo "It's friyay the 13th";exit 0
    ;;
  "6")
    echo -e "Saturday"
    ;;
  "7")
    echo -e "Sunday"
    ;;
esac

# Now you can customize this, my example for christmas:
[[ ${MONTH} = "12" && ${DATE} = "24" ]] && echo "It's christmas time" || echo "It's not christmas time :/"
wuseman
  • 1,259
  • 12
  • 20
0

I was able to solve the problem with the help of "shellcheck.net" as recommended by Cyrus. See below for the correct answer.

#!/bin/bash
day=$(date | cut -d' ' -f1)
if [ "$day" == "Monday" ];
then
    echo Moanday
elif [ "$day" == "Friday" ];
then
    echo Friyay
else
    echo Foobar
fi
Kara
  • 6,115
  • 16
  • 50
  • 57
  • This script will allways give you "Foobar" since your day command will allways be three characters if you will add all week days as you did, I guess I don't understand the point of this script. :) – wuseman Jul 16 '20 at 17:00
  • you are right bro @wuseman, but its not a real live project that i am working on. "am new to LINUX" I am stuck in a test that i was doing. where i am asked to solve this: WRITE A BASH SCRIPT TO FIND THE CURRENT DAY AND THEN GREET THE USER AS BELOW: For Monday, print Moanday For Friday, print Friyay For other days, print Foobar input date: Fri July 20 10:02:15 IST 2018 Output: Friyay This Was How I Met The Terminal Below: #!/bin/bash day=$(date | cut -d' ' -f1) if [ "$day" == "Mon" ] then elif [ "$day" == "Fri" ] then else fi PLS CAN YOU HELP ME SOLVE THIS – Fuhad Raheem Jul 16 '20 at 18:07
  • What is the problem, i dont understand I gave you an example already – wuseman Jul 16 '20 at 19:51