Linux - NewbieThis 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
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.
If it doesn't work then use "set -vx" top of your script (or equivalent for your shell) or execute it in verbose / debug mode to find out why. Most of the time it's paths or something. Also instead of '\ls -l | awk '/^d/ {print $9}'`;' consider using something like 'find $PWD/* -maxdepth 0 -type d;'.
What do you mean by "it doesn't work"? Error message, unexpected output?
What is your interactive shell? Your program uses tcsh, which I am not familiar with, but perhaps the problem lies there?
When I run the script, it correctly asks for the user to select a directory and it, correctly, sets the variable dir1 to the selected choice. Then it enters that chosen directory and asks the same question and, correctly, sets dir2. But, at the command line, it doesn't enters the chosen directories (lines 10 and 16 of the script). For example, if I run the script from my home directory, after the script runs, I'm still at the home directory level, instead of being at the directory level I had chosen during the script run.
I've tried to run with /debug, as unspawn suggested, but it didn't give any info. I've also run without the -f flag, as jpollard suggested, but it also didn't work.
The problem you are having is context. Each process has its own context - which includes the working directory.
When a command (or script) starts it runs with a new process which inherits that context. Any changes to that context within the new process remain within that process. When the process exits, the parent process resumes - with the context it started with.
Now changing the current context is possible - but you have to execute the script without starting a new process. Most shells use the syntax ". script_name" to do that. The current shell is redirected to execute commands contained with the the script - and any context changes will be made as you expect. If you want to hide the ". script_name" syntax, you might be able to do it with an alias. In bash, you can do it with a function instead. But it is up to the shell as to what works in that manner.
Now changing the current context is possible - but you have to execute the script without starting a new process. Most shells use the syntax ". script_name" to do that. The current shell is redirected to execute commands contained with the the script - and any context changes will be made as you expect. If you want to hide the ". script_name" syntax, you might be able to do it with an alias. In bash, you can do it with a function instead. But it is up to the shell as to what works in that manner.
Now I understand what went wrong! Any suggestions on how I could make it works as I was planning?
If you want a script to change the environment of your current interactive shell, such as the current directory, you have to run it using the source command. For example:
Code:
source myscript
Lazy typers appreciate the dot command. It does exactly the same thing:
Code:
. myscript
EDIT: Oops, jpollard already provided this. Sorry for intruding.
Last edited by berndbausch; 12-13-2015 at 10:36 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.