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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-03-2007, 09:18 PM
|
#1
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Rep:
|
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.
|
|
|
|
02-03-2007, 09:29 PM
|
#2
|
|
Member
Registered: Apr 2004
Location: North America
Distribution: Kubuntu 7.04 - Feisty Fawn
Posts: 296
Rep:
|
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.
|
|
|
|
02-03-2007, 09:37 PM
|
#3
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
02-04-2007, 12:22 AM
|
#4
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Original Poster
Rep:
|
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.
|
|
|
|
02-04-2007, 01:43 AM
|
#5
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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
|
|
|
|
02-04-2007, 02:17 AM
|
#6
|
|
Member
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 170
Rep:
|
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.
|
|
|
|
02-04-2007, 04:19 AM
|
#7
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Original Poster
Rep:
|
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
|
|
|
|
02-04-2007, 11:36 AM
|
#8
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
You can't modify the program to achieve that, you'll need to
invoke it differently. I already posted "how"
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
|
|
|
|
02-05-2007, 12:21 AM
|
#9
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Original Poster
Rep:
|
"./script" executes without any error, however ". script" gives me:
bash: ELF: command not found
Did I do something wrong?
|
|
|
|
02-05-2007, 12:24 AM
|
#10
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
you still have to supply the path to the script
. ./script
|
|
|
|
02-05-2007, 12:57 AM
|
#11
|
|
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,573
|
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.
|
|
|
|
02-05-2007, 03:17 AM
|
#12
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Original Poster
Rep:
|
Yes, it worked perfectly. Thank you very much.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:42 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.
|
Latest Threads
LQ News
|
|