"리눅스 여러 줄 출력을 배열에 담기"의 두 판 사이의 차이

9번째 줄: 9번째 줄:
==실습==
==실습==
<source lang='dos'>
<source lang='dos'>
[root@jmnote ~]# df
Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      27678892  18857720  7392488  72% /
/dev/xvda1              101086    36768    59099  39% /boot
tmpfs                  524288        0    524288  0% /dev/shm
</source>
[root@jmnote ~]# IFS=$'\n' ARR=(`df`)
[root@jmnote ~]# IFS=$'\n' ARR=(`df`)
[root@jmnote ~]# for VALUE in "${ARR[@]}"; do echo "[$VALUE]"; done
[root@jmnote ~]# for VALUE in "${ARR[@]}"; do echo "<-- $VALUE -->"; done
[Filesystem          1K-blocks      Used Available Use% Mounted on]
<-- Filesystem          1K-blocks      Used Available Use% Mounted on -->
[/dev/mapper/VolGroup00-LogVol00]
<-- /dev/mapper/VolGroup00-LogVol00 -->
[                      27678892  18857712   7392496 72% /]
<--                      27678892  18857720   7392488 72% / -->
[/dev/xvda1              101086    36768    59099  39% /boot]
<-- /dev/xvda1              101086    36768    59099  39% /boot -->
[tmpfs                  524288        0    524288  0% /dev/shm]
<-- tmpfs                  524288        0    524288  0% /dev/shm -->
</source>
<source lang='dos'>
[root@jmnote ~]# echo ${ARR[0]}
Filesystem          1K-blocks      Used Available Use% Mounted on
[root@jmnote ~]# echo ${ARR[1]}
/dev/mapper/VolGroup00-LogVol00
</source>
</source>
==같이 보기==
==같이 보기==
*[[Bash 실행결과를 변수에 담기]]
*[[Bash 실행결과를 변수에 담기]]

2014년 7월 5일 (토) 02:06 판

1 개요

Store multi-line output into an array in a Linux bash script
Bash 여러 줄 출력을 배열에 담기
리눅스 여러 줄 출력을 배열에 담기
IFS=$'\n' 배열변수명=(`명령어`)

2 실습

[root@jmnote ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      27678892  18857720   7392488  72% /
/dev/xvda1              101086     36768     59099  39% /boot
tmpfs                   524288         0    524288   0% /dev/shm

[root@jmnote ~]# IFS=$'\n' ARR=(`df`) [root@jmnote ~]# for VALUE in "${ARR[@]}"; do echo "<-- $VALUE -->"; done <-- Filesystem 1K-blocks Used Available Use% Mounted on --> <-- /dev/mapper/VolGroup00-LogVol00 --> <-- 27678892 18857720 7392488 72% / --> <-- /dev/xvda1 101086 36768 59099 39% /boot --> <-- tmpfs 524288 0 524288 0% /dev/shm --> </source>

[root@jmnote ~]# echo ${ARR[0]}
Filesystem           1K-blocks      Used Available Use% Mounted on
[root@jmnote ~]# echo ${ARR[1]}
/dev/mapper/VolGroup00-LogVol00

3 같이 보기

4 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}