-1

I trying to display the current years and the calendar of 2000s but I run the script it alone display the calendar of 2000s. I want both to be display. need some help

clear
#!/bin/bash
echo This is the current year.
Year="date '+%Y'"
$Year
echo This is the time:
date "+%r"
Date='cal 2000'
echo  This is the calendar list for the year 2000
$Date

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

1

Initial answer

The shebang must be the first line to be useful; otherwise, it's just an odd-ball comment.

You should simply run cal (with its output going to the terminal — standard output) rather than capturing the output of the command in a variable which you then try to execute.

Your script contains the lines:

Date='cal 2000'
echo  This is the calendar list for the year 2000
$Date

The last line tries to execute a program 2000 with 462 arguments. To make that work vaguely sanely, you'd need:

Date='cal 2000'
echo "This is the calendar list for the year 2000"
echo "$Date"

The double quotes are crucial to getting a well-formatted output for the value in $date. See also Capturing multiple line output into a Bash variable.

This should do the job as I understand it:

#!/bin/bash
clear
echo "This is the current year."
Year="$(date '+%Y')"
cal $Year
echo "This is the time:"
date '+%r'
echo "This is the calendar for the year 2000"
cal 2000

From the comments

I don't need the calendar for year 2020. All I need is the four-digit number of 2020 but I still need the calendar for year 2000.

Oh! Well, that requires some trivial changes to the code above:

#!/bin/bash
clear
echo "This is the current year: $(date '+%Y')"
echo "This is the time: $(date '+%r')"
echo "This is the calendar for the year 2000"
cal 2000

Or, if you want the values on a separate line from the descriptions:

#!/bin/bash
clear
echo "This is the current year"
date '+%Y'
echo "This is the time"
date '+%r'
echo "This is the calendar for the year 2000"
cal 2000

There are endless possible variations on this — the choice is yours. Why the year 2000 is so important is mysterious to the outsider, but it is easily managed, as shown.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • The results is still the same. it only display the cal 2000 only. –  Apr 24 '20 at 15:12
  • I don't see how that's possible if you copy'n'paste the code in the answer. Copy it to `cal89.sh` (or any other filename that doesn't exist). Run `bash cal89.sh` — you should have both 2020 and 2000 calendars scroll past. Rerun with `bash cal89.sh | less` to try again with the output shown a screen page at a time. You could use `bash -x cal89.sh` to see what's going on — the commands that are executed as they are executed. If you had a function called `cal` that did something weird that was created in your `.bashrc` file, or something like that, then all bets are off. – Jonathan Leffler Apr 24 '20 at 15:17
  • I have a front-end script for `cal` also called `cal` — it maps names to numbers, basically. It starts off by trying to locate the executable. That code is: `CAL=""; for cal in /bin/cal /usr/bin/cal /usr/local/bin/cal; do [ -x $cal ] && CAL=$cal && break; done; [ -z "$CAL" ] && { echo "Cannot find cal in /bin or /usr/bin" 1>&2; exit 1; }` (with semicolons added so that it should work even if you copy'n'paste from the comment where newlines are removed — the original is spread over 6 lines. It ends up running `exec $CAL $month $year`. You could add the finding code into the script. – Jonathan Leffler Apr 24 '20 at 15:22
  • I don't need the calendar for year 2020. all I need is the four digit numbers of 2020 but I still need the calendar for year 2000. –  Apr 24 '20 at 15:43