LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-03-2007, 09:18 PM   #1
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Rep: Reputation: 33
Unable to change directory on terminal using bash script


Hi. I'm new to bash scripting, and I have a newbie question:

This is my script:

#!/bin/bash
cd /mnt/windows


It runs, but does not change my working directory to /mnt/windows.
I add another line.

#!/bin/bash
cd /mnt/windows
ls


It works properly and displays the content of /mnt/windows as expected, but it still does not change my working directory to /mnt/windows; and I'm still in the directory I used to execute the program. How do I change the directory on my terminal?


Another problem:

I type kwrite on the terminal. The program starts, but it closes as soon as I click the close button on the terminal. So, I type kwrite& , but the program still closes when I close the terminal. My question is: How do you make a program run in the background even if the terminal is closed, the way system background processes run?

Thank you for your time.
 
Old 02-03-2007, 09:29 PM   #2
sumguy231
Member
 
Registered: Apr 2004
Location: North America
Distribution: Kubuntu 7.04 - Feisty Fawn
Posts: 296

Rep: Reputation: 30
Quote:
I type kwrite on the terminal. The program starts, but it closes as soon as I click the close button on the terminal. So, I type kwrite& , but the program still closes when I close the terminal. My question is: How do you make a program run in the background even if the terminal is closed, the way system background processes run?
Use 'nohup kwrite &' to keep it from closing with the terminal.
 
Old 02-03-2007, 09:37 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
The shell script is CD'ing as you ask but in the environment that the subshell of the script is running.

You can precede the command with nohup to prevent the wordprocessor from exiting when the calling shell does. This command allows the calling shell to exit because the wordprocessor is now a child of the nohup command instead of the shell directly.

Also look at using screen instead. For shell scripts, you can use nohup and even log out and the script will still keep running.
 
Old 02-04-2007, 12:22 AM   #4
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Yes, the nohup command solved the second problem.

Quote:
The shell script is CD'ing as you ask but in the environment that the subshell of the script is running.
Isn't there any way in which you can 'cd' from within the main shell itself?

Last edited by gregorian; 02-04-2007 at 12:25 AM.
 
Old 02-04-2007, 01:43 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Quote:
Originally Posted by gregorian
Yes, the nohup command solved the second problem.



Isn't there any way in which you can 'cd' from within the main shell itself?
Only by sourcing it you can achieve a change
directory (or "persistent" shell variables) ....

. your_script



Cheers,
Tink
 
Old 02-04-2007, 02:17 AM   #6
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
Quote:
Originally Posted by gregorian
Yes, the nohup command solved the second problem.



Isn't there any way in which you can 'cd' from within the main shell itself?
Depending on what you are ultimately trying to do, you may be better off creating an alias.
 
Old 02-04-2007, 04:19 AM   #7
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Quote:
Depending on what you are ultimately trying to do, you may be better off creating an alias.
I suppose I can do that, but I still want to know how to do it using a script.

Quote:
Only by sourcing it you can achieve a change
directory (or "persistent" shell variables) ....
Could you tell me what you meant by 'sourcing' and 'persistent'? Better still, can you tell me how to modify this program to achieve it?

#!/bin/bash
cd /mnt/windows
 
Old 02-04-2007, 11:36 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
You can't modify the program to achieve that, you'll need to
invoke it differently. I already posted "how"
Code:
. my_script
Persistent in this case only means "still available after your
script finished". If you, for example, were to set a shell
variable in your script, you could echo it, and get correct
content, while the script is running. If you run it normally
(e.g. ./my_script) you'll see the content echoed but once it's
run to completion your variable is gone all together.
If you source it (here, a third time: . my_script), the variable
will stay available.


Cheers,
Tink
 
Old 02-05-2007, 12:21 AM   #9
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
"./script" executes without any error, however ". script" gives me:

bash: ELF: command not found

Did I do something wrong?
 
Old 02-05-2007, 12:24 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
you still have to supply the path to the script

. ./script
 
Old 02-05-2007, 12:57 AM   #11
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,784

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
To clarify things a bit for gregorian: 'source' equals '.' followed by a space before the path to the script. When you make a change in the shell environment, you want the change to take place immediately. In the m$windows world, you have to re-boot for that to happen. In the *nix world, you can re-boot (if you don't mind the wait), or you can restart the X server with ctrl-alt-backspace, or you can do it the easy way by sourcing the shell config (or shell script) by using the 'source' command. In this case, the word source equals the dot which precedes the file name.

source ./<scriptname> and . ./<scriptname> are the same command. One is shorthand syntax for the other.
 
Old 02-05-2007, 03:17 AM   #12
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Yes, it worked perfectly. Thank you very much.
 
  


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
change default bash directory in gnome-terminal ming0 Linux - Software 5 10-01-2009 06:54 PM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM
bash script to resize gnome terminal ? smoker Linux - General 1 03-17-2006 03:32 PM
How can a bash script findout the width of the terminal window alex.e.c Linux - Software 2 10-14-2004 11:20 AM
Unable to Change File/Directory Protection. rvijay Linux - Newbie 2 09-15-2003 08:23 AM


All times are GMT -5. The time now is 02:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration