- To: SLUG list <slug@xxxxxxxxxxx>
- Subject: Re: [SLUG] shell scripting help
- From: Gonzalo Servat <gservat@xxxxxxxxx>
- Date: Sat, 19 Sep 2009 01:08:49 +1000
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=250wxHdk8SdFdYcy/HkLkflbAzF1btL+H2x0zSDUx/A=; b=jm+jgJXNuihYSJwHaJdlKjjpP+NkFC/ZLEkZ3FcJZHNkYyvH0RJ61YyRoxgH3/1UXi qudkvCjKjNmW5cuU2//INDUOedg3HnLHelHqDPfiO61gzOwIzWlLDfiWLF/4c96Vb8nu zqsEQf1s9vqPCsMYDNiW3sIq/pov1ZxR7demk=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=lMN7e8ndN6EOgTZLWer+B5BIkxgnLfIulHmsSxXNDSBJJOzA8/2w7DwzitLTSUwfaA AgBPdxoWW8RKgoOQBrDjmFmoaFisb5yT2Vbh1ZJgQ6WgF4bp8mVgkbXiBiF+jPWySpUv cP2kpJ5YQhplFNYGBoEZEBFi6nUsHkvqWJHGE=
2009/9/19 Rodolfo Martínez <rmtzcx@xxxxxxxxx>:
> Yes, but the last instruction is doing "f=$s"
Right, but f should be set to 'bar' since it won't go into the while
loop after the 2nd read (as it's a non true value). Well, it won't be
set to 'bar' in the OP's script because of the subshell.
> About the sub-shelling stuff... in this case that is not why 'f' is blank
>
> The 'while' is executed in the same shell
>
> [martir@amartir01 ~]$ echo $$ ; echo -n "foo|bar" | awk
> 'BEGIN{RS="|"}{ print $1 }' | while read s; do f=$s ; echo "f=$f" ;
> echo $$ ; done ; echo "f=$f"
> 5997 <== Same shell
> f=foo
> 5997 <== Same shell
> f=bar
> 5997 <== Same shell
> f=
I believe $$ gives the pid of the parent pid. Quoting:
"Within a script, inside a subshell, $$ returns the PID of the script,
not the subshell."
You could replace $$ with $BASH_SUBSHELL (boolean indicating if you're
in a subshell). Output:
$ echo $BASH_SUBSHELL ; echo -n "foo|bar" | awk 'BEGIN{RS="|"}{ print
$1 }' | while read s; do f=$s ; echo "f=$f"; echo $BASH_SUBSHELL;
done; echo "f=$f"
0
f=foo
1
f=bar
1
f=
- Gonzalo