Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-06 Thread Gilberto Villani Brito
Try:
#!/bin/sh

export PWD

echo $PWD

Gilberto


On Thu, 5 Jan 2006 14:25:10 -0500
Luis Finotti <[EMAIL PROTECTED]> wrote:

> Dear all,
> 
> I realize that this should be "way off topic", so sorry about that...
> 
> I've been working on a bash script, but when I run something like:
> ---
> #!/bin/bash
> 
> echo $(pwd)
> 
> I always get $HOME, not the current working directory.  I'd need to
> continue in the working directory from where the script was called to
> work on the files there.  (I'd like it to be portable too, so although
> it seems to work in my home machine running sarge, it doesn't work
> with Fedora or OSX...)
> 
> I look at my "Classic Shell Scripting" book and glanced over the
> "Advanced Bash-Scripting Guide" (by Mendel Cooper), but could not find
> any reference to this.  Any help would be greatly appreciated!
> 
> Best to all,
> 
> Luis
> 
> P.S.: feel free to CC me back.
> 
> 


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-06 Thread Luis Finotti

Dear all,


Roland wrote:
/bin/sh exists for compatability with the legacy Bourne shell and does 
not provide bash-specific features. In APP 1 I had a ~bash_cmd_rc file 
with some output in the BASH_ENV, and it is ignored by /bin/sh, even 
if /bin/sh is a softlink to /bin/bash on my machine. 

()

Interesting!  Thanks for the example!

Roland wrote:
I think BASH_ENV is unset by default. It allows you to have a common 
initialisation file for every non-interactive bash invokation. Setting it 
to the same rc file as for interactive shells should be a call for 
trouble IMHO. 


And John Hasler wrote:

Luis writes:

That does answer my question, but it did not work, likely because I have
$BASH_ENV set to my .bashrc..


Why?


That was the difference between my home computer (running debian) and 
this one at work (Fedora).  I thought it was the difference in some more 
"general" configuration, but it was just that BASH_ENV was set in the 
.bash_profile that was default here.  Since it had some local things 
like $PRINTER, etc., I never changed it much...  For some reason, that I 
do not know, they had also "export BASH_ENV=$HOME/.bashrc".  I've 
commented it out now.  Thanks!



I just wanted to thank all for the help!  Not only solved my problem, 
but I also learned quite a few new things.


Best to all,

Luis


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]




Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread John Hasler
Luis writes:
> /bin/sh is a link to /bin/bash, so I was expecting to see no difference
> with the change...

When called as sh bash attempts to be POSIX compliant.
-- 
John Hasler


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread John Hasler
Luis writes:
> That does answer my question, but it did not work, likely because I have
> $BASH_ENV set to my .bashrc..

Why?
-- 
John Hasler


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread John Hasler
Luis Finotti writes:
> I have
> ...
< ...

You have this in what file?  Post all of whatever file it is in.
-- 
John Hasler


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Dear Wesley,

On 1/5/06, Wesley J. Landaker <[EMAIL PROTECTED]> wrote:
> On Thursday 05 January 2006 13:16, Luis Finotti wrote:
> > I have
> >
> > ---
> >   AUTOHOME=$PWD
> >
> >   # Clean up stupid automounter directory
> >   case $PWD in
> > $AUTOHOME*) cd $HOME${PWD#$AUTOHOME} ;;
> >   esac
> >   PS1='\h[\w]% '
> > -
> >
>
> Well, this certainly appears to be your problem.

In fact it was...

> What exactly are you trying to accomplish with this snippet? It certainly
> doesn't look like a normal thing to do.

Like I said, I had that in my first .bashrc (in UNIX) and I had no
idea what anything in that file meant.  As I've learned new stuff, I
altered the file, but kept what I wasn't sure what it was, like that
line.  So, I guess I'm trying to say "I have no idea". :-)  I've
commented it out for now...

> Also, do you have the same problem if you use #!/bin/sh instead of
> #!/bin/bash? You mentioned wanting to be portable, so that's a good idea
> anyway.

That's how the script is right now.  I thought it would be nice to use
a few extra features of bash, changed the first line and found this
"change of directory" problem...  I thought it was funny, since in the
Linux machines (where I had that problem) /bin/sh is a link to
/bin/bash, so I was expecting to see no difference with the change...

Thanks!

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Dear Michael,

> You could change
>
> #! /bin/bash
>
> to
>
> #! /bin/bash --norc

That does answer my question, but it did not work, likely because I
have $BASH_ENV set to my .bashrc..

Thanks!

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Hi Jan,

Thanks for the reply again!

> > open("/home/finotti/.bashrc", O_RDONLY|O_LARGEFILE) = 3
>
> There it is. From the bash manpage I gather that a non-interactive
> bash doesn't read _any_ startup files, except for any file that
> is mentioned in the environment variable $BASH_ENV. Does this
> variable happen to be set, and to what value?

You've got it:

babbage[~/tmp]% echo $BASH_ENV
/home/finotti/.bashrc

This is set in the default ".bash_profile" here (at work).  I've never
changed it.  So, that is not the usual case?  What is the usual value?

> This one is probably sourced from your .bashrc, I presume...

Yep.  They did not have it installed here, so I copied from my home
computer and sourced it in my .bashrc.

Thanks for your help!

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Wesley J. Landaker
On Thursday 05 January 2006 13:16, Luis Finotti wrote:
> I have
>
> ---
>   AUTOHOME=$PWD
>
>   # Clean up stupid automounter directory
>   case $PWD in
> $AUTOHOME*) cd $HOME${PWD#$AUTOHOME} ;;
>   esac
>   PS1='\h[\w]% '
> -
>
> To be honest, I had this for 10 years and never thought about it.
>
> In any case, is there a way to avoid the change.  I'd like the script
> to work even if someone also has something like that...

Well, this certainly appears to be your problem. 

What exactly are you trying to accomplish with this snippet? It certainly 
doesn't look like a normal thing to do.

Also, do you have the same problem if you use #!/bin/sh instead of 
#!/bin/bash? You mentioned wanting to be portable, so that's a good idea 
anyway.

-- 
Wesley J. Landaker <[EMAIL PROTECTED]> 
OpenPGP FP: 4135 2A3B 4726 ACC5 9094  0097 F0A9 8A4C 4CD6 E3D2


pgpKEGYKTvS8c.pgp
Description: PGP signature


Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Michael Marsh
On 1/5/06, Luis Finotti <[EMAIL PROTECTED]> wrote:
> Maybe is the issue with my .bashrc, as it's been pointed out...  But
> is there a way around it?

You could change

#! /bin/bash

to

#! /bin/bash --norc

--
Michael A. Marsh
http://www.umiacs.umd.edu/~mmarsh
http://mamarsh.blogspot.com



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Dear Jan,


> Hm, this is a bit redundant - it's not necessary to wrap pwd into
> process substitution and echo the results... (...)

Thanks for the comment!  It was just something to show the change...

> Besides, the snippet is procuding the desired result here ($pwd, not
> $HOME). AFAICT, bash doesn't read any startup files when called as
> script interpreter in this fashion, so I've got no idea why your bash
> is behaving that way... just out of curiosity, what does
>
> bash -c "readlink /proc/self/cwd"

babbage[~]% bash -c "readlink /proc/self/cwd"
/home/finotti

> return? And which files does bash read on startup? (Try
>
> strace -eopen bash -c "echo 'X'"

Many repeated lines, but, after "uniq":

open("/etc/ld.so.cache", O_RDONLY)  = 3
open("/lib/libtermcap.so.2", O_RDONLY)  = 3
open("/lib/libdl.so.2", O_RDONLY)   = 3
open("/lib/libc.so.6", O_RDONLY)= 3
open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
open("/etc/mtab", O_RDONLY) = 3
open("/proc/meminfo", O_RDONLY) = 3
open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
open("/home/finotti/.bashrc", O_RDONLY|O_LARGEFILE) = 3
--- SIGCHLD (Child exited) @ 0 (0) ---
open("/home/finotti/local/bash_completion/bash_completion",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
--- SIGCHLD (Child exited) @ 0 (0) ---
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
--- SIGCHLD (Child exited) @ 0 (0) ---
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
--- SIGCHLD (Child exited) @ 0 (0) ---
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
open("/usr/share/locale/en_US/LC_MESSAGES/bash.mo", O_RDONLY) = -1
ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT
(No such file or directory)
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
--- SIGCHLD (Child exited) @ 0 (0) ---
open("/home/finotti/local/bash_completion/bash_completion.d/",
O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/inkscape",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/m-a",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/make_kpkg",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/openoffice",
O_RDONLY|O_LARGEFILE) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/pon",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
open("/home/finotti/local/bash_completion/bash_completion.d/wajig",
O_RDONLY|O_LARGEFILE) = 3
open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
X

> for example (calling bash with a command so it doesn't open an interactive
> session); or, even more interesting perhaps,
>
> strace -echdir bash -c "echo 'X'"

babbage[~]% strace -echdir bash -c "echo 'X'"
--- SIGCHLD (Child exited) @ 0 (0) ---
chdir("/home/finotti")  = 0
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
X

I guess there it is...

I commented out the lines:
  # Clean up stupid automounter directory
#  case $PWD in
#   $AUTOHOME*) cd $HOME${PWD#$AUTOHOME} ;;
#  esac

and now it works...

babbage[~/tmp]% strace -echdir bash -c "echo 'X'"
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
X

babbage[~/tmp]% test.sh
/home/finotti/tmp

Is there a way to tell the script NOT to read .bashrc?

Thanks,

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Jan C. Nordholz
Hi Luis!

> > strace -eopen bash -c "echo 'X'"
> 
> Many repeated lines, but, after "uniq":
> 
> open("/etc/ld.so.cache", O_RDONLY)  = 3
> open("/lib/libtermcap.so.2", O_RDONLY)  = 3
> open("/lib/libdl.so.2", O_RDONLY)   = 3
> open("/lib/libc.so.6", O_RDONLY)= 3
> open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
> open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
> open("/etc/mtab", O_RDONLY) = 3
> open("/proc/meminfo", O_RDONLY) = 3
> open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
> open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
> open("/home/finotti/.bashrc", O_RDONLY|O_LARGEFILE) = 3

There it is. From the bash manpage I gather that a non-interactive
bash doesn't read _any_ startup files, except for any file that
is mentioned in the environment variable $BASH_ENV. Does this
variable happen to be set, and to what value?

> --- SIGCHLD (Child exited) @ 0 (0) ---
> open("/home/finotti/local/bash_completion/bash_completion",
> O_RDONLY|O_LARGEFILE) = 3

This one is probably sourced from your .bashrc, I presume...

... and the rest seems quite normal.


Regards,

Jan


signature.asc
Description: Digital signature


Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Dear Wesley,

> The script you've shown works fine, just the way you want. Nothing in there
> is going to change the working directory.
>
> For example:
>
> $ pwd
> /home/wjl/tmp
> $ cat  #!/bin/bash
> echo $(pwd)
> EOF
> $ chmod a+x test.sh
> $ ./test.sh
> /home/wjl/tmp
>
> If you think you're having magic changing directories, I think you'll need
> to post more information, like the exact script you're using, and EXACTLY
> how you are running it. =)

OK,  I put "test.sh" in my $HOME/bin, which is in my $PATH.

Now:

babbage[~]% cd tmp
babbage[~/tmp]% test.sh
/home/finotti

Maybe is the issue with my .bashrc, as it's been pointed out...  But
is there a way around it?

Thanks!

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Luis Finotti
Dear David,

> Do you have some sort of "cd $HOME" type statement in your .bashrc or
> .bash_profile or .profile file (or similar)?


I have

---
  AUTOHOME=$PWD

  # Clean up stupid automounter directory
  case $PWD in
$AUTOHOME*) cd $HOME${PWD#$AUTOHOME} ;;
  esac
  PS1='\h[\w]% '
-

To be honest, I had this for 10 years and never thought about it.

In any case, is there a way to avoid the change.  I'd like the script
to work even if someone also has something like that...

Thanks!

Luis



Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Jan C. Nordholz
Hi!

On Thu, Jan 05, 2006 at 02:25:10PM -0500, Luis Finotti wrote:
> Dear all,
> 
> I realize that this should be "way off topic", so sorry about that...
> 
> I've been working on a bash script, but when I run something like:
> ---
> #!/bin/bash
> 
> echo $(pwd)
> 

Hm, this is a bit redundant - it's not necessary to wrap pwd into
process substitution and echo the results...

---
#!/bin/bash
pwd
---

should do it.

Besides, the snippet is procuding the desired result here ($pwd, not
$HOME). AFAICT, bash doesn't read any startup files when called as
script interpreter in this fashion, so I've got no idea why your bash
is behaving that way... just out of curiosity, what does

bash -c "readlink /proc/self/cwd"

return? And which files does bash read on startup? (Try

strace -eopen bash -c "echo 'X'"

for example (calling bash with a command so it doesn't open an interactive
session); or, even more interesting perhaps,

strace -echdir bash -c "echo 'X'"


HTH,

Jan

-- 
Jan C. Nordholz



signature.asc
Description: Digital signature


Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread Wesley J. Landaker
On Thursday 05 January 2006 12:25, Luis Finotti wrote:
> Dear all,
>
> I realize that this should be "way off topic", so sorry about that...
>
> I've been working on a bash script, but when I run something like:
> ---
> #!/bin/bash
>
> echo $(pwd)
> 
> I always get $HOME, not the current working directory.  I'd need to
> continue in the working directory from where the script was called to
> work on the files there.  (I'd like it to be portable too, so although
> it seems to work in my home machine running sarge, it doesn't work
> with Fedora or OSX...)

The script you've shown works fine, just the way you want. Nothing in there 
is going to change the working directory. 

For example:

$ pwd
/home/wjl/tmp
$ cat  
OpenPGP FP: 4135 2A3B 4726 ACC5 9094  0097 F0A9 8A4C 4CD6 E3D2


pgpHAOU92C3WP.pgp
Description: PGP signature


Re: [Way OT] "#!/bin/bash" changes working directory

2006-01-05 Thread David Kirchner
On 1/5/06, Luis Finotti <[EMAIL PROTECTED]> wrote:
> Dear all,
>
> I realize that this should be "way off topic", so sorry about that...
>
> I've been working on a bash script, but when I run something like:
> ---
> #!/bin/bash
>
> echo $(pwd)
> 
> I always get $HOME, not the current working directory.  I'd need to
> continue in the working directory from where the script was called to
> work on the files there.  (I'd like it to be portable too, so although
> it seems to work in my home machine running sarge, it doesn't work
> with Fedora or OSX...)

Do you have some sort of "cd $HOME" type statement in your .bashrc or
.bash_profile or .profile file (or similar)?