* [Sysadmins] bash | read
@ 2008-01-17 19:43 Michael Shigorin
2008-01-18 9:44 ` Michael Shigorin
0 siblings, 1 reply; 2+ messages in thread
From: Michael Shigorin @ 2008-01-17 19:43 UTC (permalink / raw)
To: sysadmins
Здравствуйте.
Хозяйке на заметку (спасибо raorn@ за снятие с ручника насчёт subshell):
$ zsh
$ echo test | read a; echo $a
test
$ bash
$ echo test | read a; echo $a
$ echo test | ( read a; echo $a )
test
$
При этом что-нить вроде такого работает, как ожидается:
| while read first second junk; do
# something with $first (first IFS-delimited word)
# ...and with $second too...
# the rest of the line goes to $junk
done
-- именно потому, что тут-то всё выполняется в одном сабшелле ;)
А, и ещё:
$ echo test:best | ( IFS=: read a b; echo $a/$b )
test/best
--
---- WBR, Michael Shigorin <mike@altlinux.ru>
------ Linux.Kiev http://www.linux.kiev.ua/
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Sysadmins] bash | read
2008-01-17 19:43 [Sysadmins] bash | read Michael Shigorin
@ 2008-01-18 9:44 ` Michael Shigorin
0 siblings, 0 replies; 2+ messages in thread
From: Michael Shigorin @ 2008-01-18 9:44 UTC (permalink / raw)
To: sysadmins
On Thu, Jan 17, 2008 at 09:43:44PM +0200, Michael Shigorin wrote:
> При этом что-нить вроде такого работает, как ожидается:
> | while read first second junk; do
> # something with $first (first IFS-delimited word)
> # ...and with $second too...
> # the rest of the line goes to $junk
> done
>
> -- именно потому, что тут-то всё выполняется в одном сабшелле ;)
При этом вынуть из него что-то, ессно, можно через stdout
сотоварищи или файлики.
hiddenman@ рассказал вот ещё что:
<hiddenman> re на твое сообщение в sysadmins@ про bash
<hiddenman> я когда etcnet патчил, столкнулся с этой и другими проблемами
<hiddenman> есть магический способ, почему-то мало где описан
<hiddenman> #!/bin/bash
A='test'
echo $A |
while read B; do
C=$B
echo "C in the subshell1 is $C"
done
echo "C out of the subshell1 is $C"
while read D; do
E=$D
echo "E in the subshell2 is $E"
done < <(echo "$A")
echo "E out of the subshell2 is $E"
<hiddenman> во втором варианте это не subshell. в результате мы снаруж имеем то, что изменили в этом while. да и работает он много быстрее, проверял
<hiddenman> такая вот magic :)
<hiddenman> можешь туда кинуть пока. я через nntp читаю, от рассылок отписался, но писать еще не пробовал через gmane.org :)
<hiddenman> упс, зачем я лишнего наделал....вот исправленный вариант:
<hiddenman> а, не , все правильно. внутри нужно присваивание сделать, переменные на время while read не экспортируются
<hiddenman> то бишь, вариант:
<hiddenman> #!/bin/bash
A='test'
echo $A |
while read B; do
echo "B in the subshell1 is $B"
done
echo "B out of the subshell1 is $B"
while read C; do
echo "C in the subshell2 is $C"
done < <(echo "$A")
echo "C out of the subshell2 is $C"
<hiddenman> работать не будет
--
---- WBR, Michael Shigorin <mike@altlinux.ru>
------ Linux.Kiev http://www.linux.kiev.ua/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-01-18 9:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-17 19:43 [Sysadmins] bash | read Michael Shigorin
2008-01-18 9:44 ` Michael Shigorin
ALT Linux sysadmins discussion
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/sysadmins/0 sysadmins/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 sysadmins sysadmins/ http://lore.altlinux.org/sysadmins \
sysadmins@lists.altlinux.org sysadmins@lists.altlinux.ru sysadmins@lists.altlinux.com
public-inbox-index sysadmins
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.sysadmins
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git