LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-13-2015, 05:15 PM   #1
ncfc
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Rep: Reputation: Disabled
problems with scripting


Hi,

I have this script that works line by line in the command line but doesn't work in a executable batch file:

[CODE]
#!/bin/tcsh -f

set dirlist = `\ls -l | awk '/^d/ {print $9}'`
set dir1 = `kdialog --combobox "directories:" $dirlist:q`

echo $dir1
cd $dir1

set dirlist = `\ls -l | awk '/^d/ {print $9}'`
set dir2 = `kdialog --combobox "directories:" $dirlist:q`

echo $dir2
cd $dir2
[CODE]

Does anyone have any ideas why it's not working?

thanks very much in advance.

best regards.
ncfc

Last edited by ncfc; 12-13-2015 at 05:16 PM.
 
Old 12-13-2015, 05:59 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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;'.
 
Old 12-13-2015, 06:02 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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?
 
Old 12-13-2015, 06:45 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Have you also tried it without the -f option?
 
Old 12-13-2015, 09:39 PM   #5
ncfc
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi, thanks for the promptly reply.

Sorry I was not specific on what the problem was.

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.

thanks very much for your help.

best regards,

ncfc
 
Old 12-13-2015, 09:59 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
No it worked just like it was supposed to.

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.

Last edited by jpollard; 12-13-2015 at 10:17 PM.
 
Old 12-13-2015, 10:16 PM   #7
ncfc
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi, jpollard.

thanks very much for the reply.

Now I understand what went wrong! Any suggestions on how I could make it works as I was planning?

Again, thanks very much.

best regards,

ncfc
 
Old 12-13-2015, 10:19 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
You caught me mid update :-)

Quote:
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.
 
Old 12-13-2015, 10:25 PM   #9
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Try to source it.
Code:
. script.sh
That's a dot followed by the name of the script.
 
Old 12-13-2015, 10:25 PM   #10
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by ncfc View Post
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.
 
Old 12-13-2015, 10:31 PM   #11
ncfc
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi, jpollard.

thanks very much for the reply.

But now you got me. How could I do that?
 
Old 12-14-2015, 05:20 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
If your current shell is tcsh, you can run it in the current context
Code:
source yourscript
You can shorten that by means of an alias
Code:
alias . source
. yourscript
Or
Code:
alias yourscript "source yourscript"
yourscript
 
Old 12-14-2015, 10:08 AM   #13
ncfc
LQ Newbie
 
Registered: Dec 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Oh, now I got it.

After sourcing it, it worked nicely!

thanks very much, jpollard, Aia, berndbausch and MadeInGermany, for your help and promptly replies.

best regards,

ncfc
 
  


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
Scripting problems (ARG!!!) abharsair Linux - Newbie 1 02-02-2009 09:04 AM
Scripting problems - I'm sure this should be easy! webamoeba Linux - Newbie 6 02-03-2004 09:58 AM
problems with scripting in expect wedgeworth Linux - Software 1 10-07-2003 03:34 PM
Scripting Problems dolvmin Linux - Software 4 08-15-2003 12:33 AM
scripting problems.. imnna2 Linux - Newbie 2 04-02-2001 09:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:38 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
Open Source Consulting | Domain Registration