Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Scripts! Need help Very Bad. 2

Status
Not open for further replies.

babbs

MIS
Mar 26, 2003
12
US
I need to put a line into a script that says

The current date is:

I thought it would be:

echo "The current date is:" date

this doesn't work

thanks ahead of time for your help
 
Sorry about this I found it! Thanks for your help

Looking in the wrong place!

Thanks again....
 
would you be able to post your answer here?

That way, if anyone else is looking for the same thing, it's here for all to find :)

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 
echo "The date is" `date`

put backticks around the commnad date. Backtick, being the key with the tilde ~.

The backticks mean that run this as a command. Everything inside the backticks gets evaluated as a command.

Also man date so that you get the format the you want.


>---------------------------------------Lawrence Feldman
SR. QA. Engineer SNAP Appliance
lfeldman@snapappliance.com

 
Thanks!

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 

Backticks have been deprecated since the release of ksh93.

If you are using a Bourne Shell derivative use $( command ).

It is more readable and nests politely.

Try doing this with backticks:

you: Hmm, I wonder what files are installed in the same RPM as whatever installed 'ls'...

rpm -ql $( rpm -qf $( which ls ) )

 
sorry, a blank seems to have got lost;
Code:
rpm -ql `rpm -qf \`which ls\``
 

Okay, now nest that again (contrived example)

echo $( rpm -ql $( rpm -qf $( which ls ) ) )

How many backslashes do you need on that second set of backticks?

Or try grepping for a string with backticks in it and using backtick expansion to place it on the command line.

echo $( grep 'your\'s truly, $name' letter )

I'm not saying it's impossible, but there's a reason they came up with a new construct, it's clearer, simpler and easier to read.
 
Hi Eric,

I partly agree that there's a reason they came up with a new construct, it's clearer, simpler and easier to read.
But I think the advantages of the new construct will only be visible when you have to write complex commands. When there is a short command containing only one pair of ``, or only one $(), I even feel that the backtick way is easier to read. A dollar sign may indicate a lot of other constructs as well (depending on what comes next), whereas a backtick sign is unique. That's my feeling, others are free to feel different.

From your first posting in this thread (Try doing this with backticks:) it was not clear whether you doubted that it could be done at all, or in one short command. So I felt obliged to respond to your challenge. ;-)

And your first new example:
Code:
echo `rpm -ql \`rpm -qf \\\`which ls\\\`\``
Yes, this shows that using backticks becomes the clumsier the deeper nesting goes.

And sorry, your second example does not work in my bash. So there's no need for converting.
Did you try yourself?
First, grep gives you a string containing one ' sign,
and then echo will fail when processing this argument.
But to me, it seems that a correction for this failure is far beyond the scope of this thread.

regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top