LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2007, 06:22 AM   #1
consty
Member
 
Registered: Feb 2004
Location: Douala-Cameroun
Distribution: RedHAt 9 ES
Posts: 85

Rep: Reputation: 15
set command


Hi all,
I am writing a shell script with positionnal parameters
and a set command (set `date`), the positionnal parameters take the value of fields of date command after the set command so that the arguments used when I execute my shell script are changed. How can I call them back inside my shell script (ksh).
Thanks for help
Consty
 
Old 02-22-2007, 07:08 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
It's not clear to me what you are asking. You have a shell script called "set"? You want to pass the output of the date command as arguments to a script?
 
Old 02-22-2007, 08:06 AM   #3
consty
Member
 
Registered: Feb 2004
Location: Douala-Cameroun
Distribution: RedHAt 9 ES
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks for your answer,
I execute my script with arguments, example : prog arg1 arg2 arg.
In my script prog there is the command "set `date`" which reset (change) the
values of $1, $2, $3, previously coreesponding to the value of arg1, arg2, arg3.
I would like to call back the values of arg1 arg2 and arg3 at the end of my program.
I hope this is clear.
Thanks
 
Old 02-22-2007, 08:36 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Do you mean you want to do something like this:
Code:
prog one two three
echo "one is $one"
echo "two is $two"
echo "three is $three"
...and expect to see:
Code:
one is The
two is Feb
three is 22
Is that what you mean?

You can't have a script which you call directly modify the environment of the script which called it. Environment variables are inheritied by child processes, but there is no mechanism for sending changes back to the parent environment (without using mechanism like shared memory anyhow).

What you can do is take the standard output of your program and assign that to variables. For example:
Code:
date |while read a b c; do echo a=$a b=$b c=$c; done
 
Old 02-22-2007, 09:03 AM   #5
consty
Member
 
Registered: Feb 2004
Location: Douala-Cameroun
Distribution: RedHAt 9 ES
Posts: 85

Original Poster
Rep: Reputation: 15
THanks a lot
Djessi
 
Old 02-22-2007, 07:06 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by matthewg42
What you can do is take the standard output of your program and assign that to variables. For example:
Code:
date |while read a b c; do echo a=$a b=$b c=$c; done
That will not work in most shells if you want to access the variables after the loop has finished. Elements of a pipeline are executed in subshells, and subsequent commands will not be able to access the assignments made in the pipeline.

This would work:

Code:
date | {
 read a b c
 echo a=$a b=$b c=$c
 ## All assignments made within the braces will be available
 ## only within the braces
}
A better way is to use eval. For example, I like to store the various date and time components in variables like this:

Code:
eval "$( date "+
   DATE=%Y-%m-%d
   YEAR=%Y
   MONTH=%m
   DAY=%d
   TIME=%H:%M:%S
   HOUR=%H
   MINUTE=%M
   SECOND=%S
   datestamp=%Y-%m-%d_%H.%M.%S
   DayOfWeek=%a
   DayOfMonth=%d
   DayOfYear=%j
   DayNum=%w
   MonthAbbrev=%b
   MonthFull=%B
   MonthName=%B
   DayName=%A
" )"
 
Old 02-23-2007, 02:32 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Pull your initial values out first:
ARG1=$1 ARG2=$2 ARG3=$3

#then use a for loop instead of while/until:

for arg in "$@" ; do
commands
done
 
Old 02-23-2007, 02:55 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you don't need the parenthesis which make it a sub shell, so
only inside will they be visible i.e:
Code:
$ (set `date`;echo $4 $3 $2 $1)      
08:52:48 23 Feb Fri
$ echo got:$1 $2
got:
just use the plain
set `date`

Code:
$ set `date`
$  echo $1 $2
Fri Feb
 
  


Reply



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
Problem with command set kamalesh_21 Programming 1 10-07-2006 01:57 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

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
Open Source Consulting | Domain Registration