LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-12-2008, 07:39 AM   #1
smehra24
Member
 
Registered: Jan 2008
Location: mumbai
Distribution: red hat
Posts: 31

Rep: Reputation: 15
set command


Code:
set 'date'
What will this command do??
 
Old 03-12-2008, 08:25 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
In what context? An interactive shell? If so, what shell do you use?
 
Old 03-12-2008, 09:05 AM   #3
trashbird1240
Member
 
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
Why are you asking?
 
Old 03-13-2008, 01:51 AM   #4
smehra24
Member
 
Registered: Jan 2008
Location: mumbai
Distribution: red hat
Posts: 31

Original Poster
Rep: Reputation: 15
i'm doing shell programming and i want to set the date fields to some variables... somewhere i found that by using
Code:
set 'date'; m=$2; y=$6
i can set m as month and y as year..

but the program does not seem to do this

Can u help??
 
Old 03-13-2008, 02:03 AM   #5
leebrent
Member
 
Registered: Oct 2007
Location: Nanaimo
Distribution: Red Hat 5
Posts: 39

Rep: Reputation: 15
date=$(date '+%m-%d')

something like that?
 
Old 03-13-2008, 02:24 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You have got the wrong quotes. The apostrophe character which you provided in your example is used to quote literal strings. To execute a command and use the output in a shell command, you use backticks. Note the difference here:
Code:
'this is a literal string'
`this is a command`
Because it is so easy to confuse backticks with apostrophes, I prefer to use the $() syntax for command output substitution. For example, both of these do exactly the same thing:
Code:
myvar=`some command`
myvar=$(some command)
As for the set command... I usually only use it for processing positional parameters with getopt, and that's how I've most often seen it used. I find using set to split the output of some command to be a bit difficult to read.
Code:
set $(date)
echo "now we have the components of the date output in positional params:"
echo "1 = $1"
echo "1 = $2"
echo "1 = $3"
echo "1 = $4"
echo "1 = $5"
Note that the format of date with no parameters may be locale dependent. It is more robust to explicitly specify the format string which date should use, e.g.
Code:
set $(date "+%Y %m %d %H %M %S")
...which will put the year in $1, the month number in $2 etc. See the date manual page for the definitions of the date format strings.

My taste is to not use the set command for this purpose. One reason is that it is not very readable, although any reasonably experience shell script coder should know what it does. A more compelling reason is that you will lose your original positional parameters.

I'd probably do something like this:
Code:
eval $(date "+year=%Y month=%m day=%d")
echo "year=$year month=$month day=$day"
eval is also a little obscure, like set, but at least with this method you don't lose your original positional parameters.

I hope that helps.
 
Old 03-13-2008, 02:28 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
seems to work properly:
Code:
set -- $(date)
dayname=${1} month=${2} daynum=${3} time=${4} tz=${5} year=${6}
echo ${dayname} ${month} ${daynum} ${time} ${tz} ${year}
Thu Mar 13 15:26:26 PHT 2008
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
set command consty Programming 7 02-23-2007 02:55 AM
set/export command? blackzone Linux - Newbie 1 03-05-2005 07:11 PM
When to use the set and when to use export command? davidas Linux - Newbie 5 03-29-2004 04:01 AM
how to set command path okiyani Linux - General 1 03-04-2004 07:57 PM
Using set command Debian twantrd Linux - General 0 11-23-2003 03:10 AM


All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration