Kevin Schultz
Kevin Schultz

Reputation: 884

Execute cal function in linux given month and year

I am writing a bash script for a beginning unix/linux class that has the user input the month and year that they want to view. I am not sure how to execute the calendar given the user input.

Here is what I have.

#!/bin/bash  
echo -n "Enter the month you wish displayed: "; read month  
echo -n "Enter the year you wish displayed: "; read year  
echo cal '$month' '$year'  

Upvotes: 1

Views: 996

Answers (1)

Floris
Floris

Reputation: 46425

When you use echo, the rest of the line (after possible processing by the shell) gets output to stdout (or possibly piped into the next process). This prevents cal from being treated as a command.

There are different kinds of quotation marks that are used in 'nix: the " double quote, and the ' single quote. There's also the ` backtick. Each has a very specific use.

Any command surrounded by backticks is expanded inline - that is, it is executed and the output is put in the line. Thus,

echo hello `whoami`

will result in hello john if your user name (the response to the whoami command) is john.

The single quote has the opposite effect: it "protects" any text from the shell, so no further attempts will be made to process. Thus

echo hello '`whoami`'

will result in

hello '`whoami`'

The double quote " is a bit less restrictive. The $ (variable), \ (escape), and ` backtick are still interpreted, but it's a good way to pass an argument with a space in it to a script. Thus

doSomething for me

passes two parameters to the function doSomething: for, and me. On the other hand

doSomething "for me"

only passes a single parameter, for me

I hope this clarifies things for you.

Upvotes: 1

Related Questions