LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 06-30-2006, 02:59 PM   #1
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Rep: Reputation: 15
Cygwin shell programming question


Hello!

I'm trying out cygwin environment and I have the following problem. I wrote simple script test.sh and chmod it (755) so I will be able to execute it.

It's very simple (test.sh)

Code:
#!/bin/sh
pwd
cd /usr/include/
pwd
I placed it on the root folder (/) and executed it from there:

Code:
$./test.sh
result from scrip:

Code:
/
/usr/include
As you can see everything looks fine. But when script finishes I'm still in the root folder (/) and not in the /usr/include ?!? Why? Everything looks fine in the scope of the shell script, but when it's finished I'm back where I begun.

Best regards,
 
Old 06-30-2006, 03:07 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,185

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
The script kicks off a subshell. The cd is occuring in the subshell. Once your script completes it exits the subshell and returns you to your original (parent) shell.
 
Old 06-30-2006, 03:09 PM   #3
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
Is there anything I can do about it? Some specific way of executing the script? Is this behaviour the same on true linux platform?

Regards,
 
Old 06-30-2006, 03:15 PM   #4
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
I tried the same thing on VmWare virtual machine (Fedora Core 2) and the behaviour is the same. Not just that. Any exports I make in the shell script are not set anymore after script ends. How can I fix the problem?
 
Old 06-30-2006, 03:17 PM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,185

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
It has the same action in true Linux and Unix. This is the way shells work so is not something with cygwin.

You could do something like "exec /bin/sh" at the end of the script. This WILL leave you in the subshell (actually a replacement of the subshell as that is what exec does - replaces running process with the process following exec). At that point you will be in /usr/include but may not have the environment the way you want it.
 
Old 06-30-2006, 03:23 PM   #6
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
Ifound the solution. Script has to be executed the following way:

Code:
$. test.sh
So in the command line you write

Code:
<period><white space><shell script>
Regards,
 
Old 06-30-2006, 03:36 PM   #7
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,185

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
well duh - of course just "source it" with the dot. Why didn't I think of that?

This is how one gets environment variables from shell scripts into the current shell as well.

Must be late on Friday on a holiday weekend (in the US of course).
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about shell-script programming GSX Programming 11 11-22-2005 04:58 PM
Question about bash shell programming walterbyrd Linux - General 1 11-01-2005 09:42 AM
simple question on shell programming stupid_guy Programming 5 03-10-2004 02:07 PM
Linux Shell programming question ducka Programming 4 09-01-2003 09:24 AM
Cygwin:How do you turn the bash shell bell off? petercool Linux - Newbie 8 05-23-2003 10:48 AM


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