| Author |
variable scope: is do-done a subshell ?
|
|
| Zbigniew Lisiecki 2004-03-21, 12:33 am |
| Hi,
could somebody explain why a variable $size in "myscript" loses it's value
outside of a loop and suggest how to repair it ?
put this in a file "myscript" to check it.
#!/bin/sh
ls -l | while read perm ln u g size date time file; do
case $file in
"myscript")
export size=$size
echo size=$size;;
esac
done
echo size=$size
thanx in advance, zbyszek
--
http://zbyszek.evot.org
| |
| Peter J. Acklam 2004-03-21, 7:34 am |
| Zbigniew Lisiecki <zbyszek@evot.org> wrote:
> could somebody explain why a variable $size in "myscript" loses
> it's value outside of a loop and suggest how to repair it ?
>
> put this in a file "myscript" to check it.
>
> #!/bin/sh
> ls -l | while read perm ln u g size date time file; do
> case $file in
> "myscript")
> export size=$size
> echo size=$size;;
> esac
> done
> echo size=$size
First of all, you're not reading the information correctly. The
date isn't one field, but two: month and day, like "Mar 21". The
following field, which you call "time", is the year if the mtime
is more than six months ago.
Peter
--
Peter J. Acklam - pjacklam@online.no - http://home.online.no/~pjacklam
| |
| Zbigniew Lisiecki 2004-03-21, 9:33 am |
| Hi Peter,
there are different outputs depending if it's this year, or not. here is one
i get:
drwxr-xr-x 7 zbyszek users 352 2004-01-06 01:02 man
drwx------ 3 zbyszek users 184 2004-01-06 01:06 money
drwxrwxrwx 7 zbyszek users 368 2004-03-05 21:08 net
drwxr-xr-x 380 zbyszek users 10456 2004-03-15 07:02 people
but it all doesn't matter for my problem, thanx anyway for your interest
best, zbyszek
--
http://zbyszek.evot.org
| |
| Peter J. Acklam 2004-03-21, 9:33 am |
| Zbigniew Lisiecki <zbyszek@evot.org> wrote:
> Hi Peter,
> there are different outputs depending if it's this year, or
> not. here is one i get:
>
> drwxr-xr-x 7 zbyszek users 352 2004-01-06 01:02 man
> drwx------ 3 zbyszek users 184 2004-01-06 01:06 money
> drwxrwxrwx 7 zbyszek users 368 2004-03-05 21:08 net
> drwxr-xr-x 380 zbyszek users 10456 2004-03-15 07:02 people
>
> but it all doesn't matter for my problem, thanx anyway for your
> interest best, zbyszek
Just out of curiosity. Which "ls" has this output format?
Peter
--
Peter J. Acklam - pjacklam@online.no - http://home.online.no/~pjacklam
| |
| Zbigniew Lisiecki 2004-03-21, 11:35 am |
| nie 21. of March 2004 15:19 Peter J. Acklam wrote:
> Zbigniew Lisiecki <zbyszek@evot.org> wrote:
>
>
> Just out of curiosity. Which "ls" has this output format?
zbyszek@jane:~> ls --version
ls (fileutils) 4.1.11
by Richard Stallman and David MacKenzie.
Copyright (C) 2002 Free Software Foundation, Inc.
on SuSE 8.2 Linux,
each ls can have this output
z
--
http://zbyszek.evot.org
| |
| those who know me have no need of my name 2004-03-21, 12:34 pm |
| in comp.unix.shell i read:
>Zbigniew Lisiecki <zbyszek@evot.org> wrote:
[color=darkred]
>Just out of curiosity. Which "ls" has this output format?
probably just an effect of the current locale. though perhaps i smell gnu
ls' --time-style option.
--
a signature
| |
| Peter J. Acklam 2004-03-21, 12:34 pm |
| those who know me have no need of my name <not-a-real-address@usa.net> wrote:
> in comp.unix.shell i read:
>
>
>
>
> probably just an effect of the current locale. though perhaps i
> smell gnu ls' --time-style option.
My initial guess was the "--time-style" option and GNU ls, but no
option was passed to ls, so I became curious. I didn't think of
locales.
Peter
--
Peter J. Acklam - pjacklam@online.no - http://home.online.no/~pjacklam
| |
| Philipp Buehler 2004-03-21, 1:34 pm |
| * Zbigniew (zbyszek@evot.org) wrote:
> but it all doesn't matter for my problem, thanx anyway for your interest
> best, zbyszek
Well, this is yet-another-POSIX-didnt-say-what-is-right ;)
The implementation can decide if a sub-shell is used
here, or not.
Obviously it does (it has to be documented in the manpage of your shell)
use a subshell, thus all variables are local to this one and not to
the upper shell.
Only way out: use a different [POSIX] shell, which doesnt use
subshells in such a construct (while).
ciao
--
"Unix was the first OS where you could carry the media and system
documentation around in a briefcase. This was fixed in BSD4.2."
| |
| those who know me have no need of my name 2004-03-22, 12:34 am |
| in comp.unix.shell i read:
>those who know me have no need of my name <not-a-real-address@usa.net> wrote:
[color=darkred]
>
>My initial guess was the "--time-style" option and GNU ls, but no
>option was passed to ls, so I became curious. I didn't think of
>locales.
also i think that gnu ls can take options from the environment, or some
systems (linuxen mostly) seem to love having loads of aliases by default.
--
a signature
|
|
|
|