15 개 Bash Array 배열 튜 토리 얼
22738 단어 링크 ux 명령 / 셸
1. 배열 설명, 할당
변수 가 다음 형식 일 때 Bash 는 자동 으로 배열 을 만 듭 니 다.
name[index]=value
$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
echo ${Unix[1]}
$./arraymanip.sh
Red hat
배열 요소 에 접근 하려 면 괄호 를 사용 하 십시오. 예 를 들 어 ${name [index]}
2. 설명 할 때 배열 초기 화
지정 한 요소 목록 을 통 해 배열 을 설명 합 니 다. 각각 배열 요 소 를 초기 화하 지 않 고 괄호 안에 빈 칸 으로 분리 합 니 다.
Syntax:
declare -a arrayname=(element1 element2 element3)
배열 요소 에 빈 칸 이 포함 되 어 있 으 면 따옴표 로 묶 습 니 다.
#! /bin/bash
$cat arraymanip.sh
declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');
declare - a 는 배열 과 괄호 의 모든 요 소 를 배열 요소 로 설명 합 니 다.
3. 전체 Bash 배열 출력
전체 배열 을 출력 하 는 여러 가지 방법 이 있다.색인 이 @ 또는 * 이면 배열 의 모든 요 소 를 참조 합 니 다.순환 을 사용 하여 배열 의 모든 요 소 를 옮 겨 다 니 며 출력 할 수도 있 습 니 다.
echo ${Unix[@]}
# Add the above echo statement into the arraymanip.sh
#./t.sh
Debian Red hat Ubuntu Suse
배열 요 소 를 참조 하고 색인 을 제공 하지 않 으 면 배열 의 첫 번 째 요 소 를 참조 하 는 것 입 니 다. 즉, 색인 이 0 인 요소 입 니 다.
4. Bash 배열 길이
특수 매개 변수 $\# 를 사용 하여 배열 길 이 를 얻 을 수 있 습 니 다.
${#arrayname[@]} gives you the length of the array.
$ cat arraymanip.sh
declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');
echo ${#Unix[@]} #Number of elements in the array
echo ${#Unix} #Number of characters in the first element of the array.i.e Debian
$./arraymanip.sh
4
6
5. 배열 n 번 째 요소 의 길이
${\# arrayname [n]} 은 배열 n 번 째 요소 의 길이 입 니 다.
$cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
echo ${#Unix[3]} # length of the element located at index 3 i.e Suse
$./arraymanip.sh
4
6. 오프셋 과 길이 출력 배열 지정
다음 예 는 두 개의 배열 요 소 를 출력 하 는 것 입 니 다. 세 번 째 색인 이 3 인 요소 부터 시작 합 니 다.
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}
$./arraymanip.sh
Suse Fedora
위의 예 는 색인 이 3 과 4 인 요 소 를 되 돌려 줍 니 다.색인 은 영원히 0 으로 시작 합 니 다.
7. 오프셋 과 길이 에 따라 배열 이 지정 한 요소 의 일부분 을 출력 합 니 다.
배열 요소 의 앞 네 글 자 를 출력 합 니 다.예 를 들 어 우 분투 는 배열 색인 이 3 인 요소 로 배열 이 지정 한 요소 의 일 부 를 가 져 올 오프셋 과 길 이 를 지정 할 수 있 습 니 다.
$cat arraymanip.sh
#! /bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}
./arraymanip.sh
Ubun
위의 예 는 색인 이 2 인 배열 요소 의 앞 4 자 를 꺼 냅 니 다.
8. 배열 요 소 를 검색 하고 교체 합 니 다.
다음 예, 배열 요소 에서 Ubuntu 를 검색 하여 SCO Unix 로 바 꿉 니 다.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]/Ubuntu/SCO Unix}
$./arraymanip.sh
Debian Red hat SCO Unix Suse Fedora UTS OpenLinux
그러나 이 예 는 배열 내용 을 영구적 으로 바 꾸 지 않 았 다.
9. 존재 하 는 Bash Array 에 요소 추가
다음 예 는 존재 하 는 배열 에 요 소 를 추가 하 는 방법 을 보 여 줍 니 다.
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}
$./arraymanip.sh
AIX
'AIX' and 'HP - UX' are added in 7th and 8th index respectively. 배열 유 닉 스에 서 요소 'AIX' 와 'HP - UX' 를 7 위 와 8 위 에 추가 했다.
10. 배열 요소 삭제
unset 는 배열 요 소 를 제거 하 는 데 사 용 됩 니 다. unset 는 배열 요소 에 null 로 할당 하 는 것 과 같은 효과 입 니 다.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix[3]
echo ${Unix[3]}
위의 예 는 색인 이 3 인 값 에 null 을 출력 합 니 다.다음 예 는 배열 에서 완전히 삭제 되면 보 여 줍 니 다.
$ cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}
$./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
이 예 에서 ${Unix [@]: 0: $pos} 은 색인 0 에서 3 개의 요 소 를 출력 합 니 다.이상 의 출력 을 합 칩 니 다.이것 은 배열 요 소 를 삭제 하 는 방법 이다.
11. 사용 모드 (Patterns) 에서 배열 요소 삭제
검색 조건 에 서 는 패턴 (Patterns) 을 제시 하고 나머지 요 소 를 다른 배열 에 저장 할 수 있 습 니 다.
$ cat arraymanip.sh
#!/bin/bash
declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
declare -a patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}
$ ./arraymanip.sh
Debian Ubuntu Suse Fedora
위의 예 에서 빨간색 과 같은 배열 요소 삭제 *
12. 배열 복사
배열 요 소 를 확장 하여 새로운 배열 에 저장 합 니 다.
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Linux=("${Unix[@]}")
echo ${Linux[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
13. 두 Bash 배열 연결
두 개의 배열 을 확장 하여 새 배열 에 값 을 부여 합 니 다.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14
배열 'Unix' 와 'Shell' 의 모든 요 소 를 출력 합 니 다. 새 배열 은 모두 14 개의 요소 가 있 습 니 다.
14. 전체 배열 삭제
unset 를 사용 하여 전체 배열 을 삭제 합 니 다.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}
$ ./arraymanip.sh
0
unset 배열 후 배열 의 길 이 는 0 입 니 다.
15. 배열 에 파일 내용 불 러 오기
배열 에 파일 내용 을 한 줄 씩 추가 할 수 있 습 니 다.
#Example file
$ cat logfile
Welcome
to
thegeekstuff
Linux
Unix
$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)
for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"
$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
Read file content!
상기 예 에서 배열 의 모든 요 소 는 순환 출력 을 이용 합 니 다.
http://blog.maxiang.net/15-bash-array-tutorial/148/