posted by 귀염둥이채원 2021. 4. 15. 04:42

 

---------------------------------------------------------

# array example

---------------------------------------------------------

#!/bin/bash

# 배열의 크기 지정없이 배열 변수로 선언

# 참고: 'declare -a' 명령으로 선언하지 않아도 배열 변수 사용 가능함

declare -a array

 

# 4개의 배열 값 지정

array=("hello" "test" "array" "world")

 

# 기존 배열에 1개의 배열 값 추가(순차적으로 입력할 필요 없음)

array[4]="variable"

 

# 기존 배열 전체에 1개의 배열 값을 추가하여 배열 저장(배열 복사 시 사용)

array=(${array[@]} "string")

 

# 위에서 지정한 배열 출력

echo "hello world 출력: ${array[0]} ${array[3]}"

echo "배열 전체 출력: ${array[@]}"

echo "배열 전체 개수 출력: ${#array[@]}"

 

printf "배열 출력: %s\n" ${array[@]}

 

# 배열 특정 요소만 지우기

unset array[4]

echo "배열 전체 출력: ${array[@]}"

 

# 배열 전체 지우기

unset array

echo "배열 전체 출력: ${array[@]}"

 

# output

$ bash test.sh

hello world 출력: hello world

배열 전체 출력: hello test array world variable string

배열 전체 개수 출력: 6

배열 출력: hello

배열 출력: test

배열 출력: array

배열 출력: world

배열 출력: variable

배열 출력: string

배열 전체 출력: hello test array world string

배열 전체 출력:

 

---------------------------------------------------------

# array example

---------------------------------------------------------

#!/bin/bash

MyArray=(HTML Javascript CSS JQuery Bootstrap)

 

# Print 5 values individually

echo "----------Print 5 values individually---------------"

echo ${MyArray[0]}

echo ${MyArray[1]}

echo ${MyArray[2]}

echo ${MyArray[3]}

echo ${MyArray[4]}

 

#Print all values by using *

echo "-----------------Print all values-------------------"

echo ${MyArray[*]}

 

# output

$ bash test.sh

----------Print 5 values individually---------------

HTML

Javascript

CSS

JQuery

Bootstrap

-----------------Print all values-------------------

HTML Javascript CSS JQuery Bootstrap

 

---------------------------------------------------------

# array에서 for문 사용

---------------------------------------------------------

#!/bin/bash

MyArray=(HTML Javascript CSS JQuery Bootstrap)

 

for i in "${MyArray[@]}"; do

#echo $i

if [[ "$i" == "CSS" ]]; then

echo $i

fi

done

 

# output

$ bash test.sh

CSS

 

---------------------------------------------------------

# array에서 for문 사용

---------------------------------------------------------

#!/bin/bash

# Declare array with 4 elements

ARRAY=('Debian Linux' 'Redhat Linux' Ubuntu Linux)

 

# get number of elements in the array

ELEMENTS=${#ARRAY[@]}

 

# echo each element in array

# for loop

for ((i = 0; i < $ELEMENTS; i++)); do

echo ${ARRAY[${i}]}

done

 

# output

$ bash test.sh

CSS

 

# output

# bash test.sh

Debian Linux

Redhat Linux

Ubuntu

Linux

 

---------------------------------------------------------

# 배열 요소 추가

---------------------------------------------------------

#!/bin/bash

MyArray=(HTML Javascript CSS JQuery Bootstrap)

 

# array append

MyArray+=("Go")

 

# Add new element at the end of the array

MyArray[${#MyArray[@]}]="Python"

 

# 한줄로 표시

for val in "${MyArray[@]}"; do echo $val; done

 

# output

$ bash test.sh

HTML

Javascript

CSS

JQuery

Bootstrap

Go

Python

 

---------------------------------------------------------

# 배열 끝에 여러 요소 추가

---------------------------------------------------------

#!/bin/bash

# Declare two string arrays

arrVar1=("John" "Watson" "Micheal" "Lisa")

arrVar2=("Ella" "Mila" "Abir" "Hossain")

 

# Add the second array at the end of the first array

arrVar=(${arrVar1[@]} ${arrVar2[@]})

 

# Iterate the loop to read and print each array element

for value in "${arrVar[@]}"; do

echo $value

done

 

# output

$ bash test.sh

John

Watson

Micheal

Lisa

Ella

Mila

Abir

Hossain

 

---------------------------------------------------------

# 참고사이트

---------------------------------------------------------

https://blog.gaerae.com/2015/01/bash-hello-world.html

https://linuxhint.com/bash_append_array/