LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2003, 12:25 PM   #1
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Rep: Reputation: 35
Bash programing problem


I'm learning some bash programing and have written a function which is similar to allready integrated function pushd.

that's the funciton:

#!/bin/bash

push () {
dirname=$1
DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}"
cd ${dirname:?"Vnesi pot!"}
echo "$DIRSTACK"
}

push $1

everything but the line with cd is working OK. Why does cd not work?
I tyed a test also:

#!/bin/bash

test ()
{
cd $1
}

test $1

and it isnt working.
why not?
 
Old 12-29-2003, 07:21 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Bash programing problem

Quote:
Originally posted by zWaR
everything but the line with cd is working OK. Why does cd not work?
I tyed a test also:
Code:
#!/bin/bash

test ()
{
    cd $1
}

test $1
and it isnt working.
why not?
When you start a script like this a new shell (sub-) process is created to run your script. The working directory is specific to a shell process. So changing the working directory with "cd" within a script only has effect for the shell process that is running the script. When the script ends, the shell process that was created for running the script also ends. And the environment of that process, including the environment variable "$PWD" which holds the working directory, is lost. When the script exits you are back in the shell (process) where you started the script, which has still the same environment as before startinjg the script, so it also still is in the same working directory.

There is a way to work around this. Say, for example, the above script is called, say, "chdir", and is located in /home/zwar/bin. You can then run the script in the current shell process by starting it with the "source" command (or the dot command (just a single " . "), which does the same as "source") :
Code:
bash$ source /home/zwar/bin/chdir

or:

bash$ .  /home/zwar/bin/chdir
If you want to run your script just by its name, i.e. without "source" or " . ", you can define an alias for it, like this:
Code:
alias chdir='/home/zwar/bin/chdir'
After entering this line at the shell prompt, you can run your script just by typing "chdir". If you want the alias permanently, you can put the alias command in your ~/.bashrc , ~/.profile , /etc/profile. Then the alias will be defined automatically when you log in or start a new shell, depending which file you put it into.

By the way, the name of the alias does not need to have the same name as the script itself, but it is OK.

Hope this helps.

Last edited by Hko; 12-29-2003 at 07:24 AM.
 
Old 01-01-2004, 05:07 PM   #3
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Original Poster
Rep: Reputation: 35
thank you very much! i tried to run the script with the source command, but now, something very funny happened. The test script works, but the push script doesn't i i don't get it. Where's the difference?
 
Old 01-01-2004, 05:57 PM   #4
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Original Poster
Rep: Reputation: 35
i managed it! i just had to rename the function. insted of push i wrote test and it works
 
  


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
bash programing Paxmaster Programming 1 08-23-2005 06:09 PM
A problem with CGI programing Bassam Programming 1 11-04-2004 11:38 PM
Programing in Linux jimbob1989 Programming 1 10-10-2004 10:53 AM
Bash programing using 2 variables PcHammer Linux - Software 1 04-21-2004 08:11 AM
telnet programing kobilevi Programming 2 02-26-2001 01:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 AM.

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