Display calendar in correct format using shell script

Hi All,

I'm trying to print calendar using shell script and i'm able to print it. But the format is not good.

Here is the script.

#!/bin/bash
echo $(date)
echo "Hello $USER"
echo Hostname $(hostname)
echo Working in $(pwd)
echo Here is this month calender
echo $(cal)

[cracha@cs ~]$ sh first.sh
Mon Sep 22 12:37:42 IST 2014
Hello cracha
Hostname cs
Working in /home/cracha
Here is this month calender
September 2014 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[cracha@cs ~]$

Please let me know how to print it in right format.

Regards,
Chandu

Hello Chandrakanth,

Welcome to forum, kindly use code tags while posting commands and codes in post. Following may help you in same.

#!/bin/bash
echo "hello" `whoami`
echo `hostname`
echo `pwd`
echo "Here is the calender for this month"
cal

Thanks,
R. Singh

You have some unnecessary echo commands and command substitutions.
Try the following instead:

#!/bin/bash
date
echo "Hello $USER"
echo "Hostname $(hostname)"
echo "Working in $PWD"
echo "Here is this month's calendar"
cal

Thanks Both,

Both the codes worked for me.
Can you guys help me understand when to use "" and ' ' and $() ?
Or direct me to a link that explains these.

(I'm new to linux and shell scripting)

Thanks in advance.

Regards,
Chandu

$(..) is the syntax that replaced ` command something ` the result is the execution of the code between the braces..
For the quotes and double quotes its best you experience with them and you tell us what you understood: e.g.

n12:/home/vbe/wks/test $ TITI=01
n12:/home/vbe/wks/test $ echo "$TITI"
01
n12:/home/vbe/wks/test $ echo '$TITI' 
$TITI

So?

So "" is used to print the value of a variable.

Regards,
Chandu

Is this a homework assignment?

If you don't know what $(cal) does, why did you use it in your script?

If you use echo $(cal) with command substitution and no quotes, all of the sequences of whitespace characters will be converted by the shell into single spaces before being passed to echo . The text between single quotes will be treated by the shell unchanged as a single operand. The text between double quotes will be treated by the shell as a single operand after performing variable expansions, arithmetic expansions, and command substitutions. Compare the results from the following commands:

printf '%s\n' $PWD $(cal)
printf '%s\n' "$PWD" "$(cal)"
printf '%s\n' '$PWD' '$(cal)'

Hi Don,

This is not a Homework assignment. I just started learning shell programming.

Thanks for your inputs.

Regards,
Chandu