ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
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
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.