IFS='.'; echo "eee.star.com" | (read a b c; echo $a; echo $b; echo "$b.$c")
eee
star
star.com
단, a, b, c 변수는 괄호안에서만 유효합니다. 이유는
BASH FAQ wrote:
E4) If I pipe the output of a command into `read variable', why doesn't
the output show up in $variable when the read command finishes?
This has to do with the parent-child relationship between Unix
processes. It affects all commands run in pipelines, not just
simple calls to `read'. For example, piping a command's output
into a `while' loop that repeatedly calls `read' will result in
the same behavior.
Each element of a pipeline runs in a separate process, a child of
the shell running the pipeline. A subprocess cannot affect its
parent's environment. When the `read' command sets the variable
to the input, that variable is set only in the subshell, not the
parent shell. When the subshell exits, the value of the variable
is lost.
Many pipelines that end with `read variable' can be converted
into command substitutions, which will capture the output of
a specified command. The output can then be assigned to a
variable:
This does not, unfortunately, work to split the text among
multiple variables, as read does when given multiple variable
arguments. If you need to do this, you can either use the
command substitution above to read the output into a variable
and chop up the variable using the bash pattern removal
expansion operators or use some variant of the following
approach.
Say /usr/local/bin/ipaddr is the following shell script:
[code:1]#!/bin/basha="www.yah
더 좋은 방법은 다른 분께서 달아주실겁니다.
IFS 환경 변수와 read를 이용하세요.
단, a, b, c 변수는 괄호안에서만 유효합니다. 이유는
댓글 달기