Bash:Array
bash 새 버젼부터는 1차원 배열을 지원합니다. variable[xx]
처럼 선언할 수도 있고 declare -a variable
처럼 직접적으로 지정해 줄 수도 있다. 배열 변수를 역참조하려면(내용을 알아내려면) ${variable[xx]}
처럼 중괄호 표기법을 쓰면 된다.
How to use
Bash arrays have numbered indexes only, but they are sparse, ie you don't have to define all the indexes. An entire array can be assigned by enclosing the array items in parenthesis:
Individual items can be assigned with the familiar array syntax (unless you're used to Basic or Fortran):
But it gets a bit ugly when you want to refer to an array item:
In addition the following funky constructs are available:
${arr[*]} # All of the items in the array
${arr[@]} # == ${arr[*]}
${!arr[*]} # All of the indexes in the array
${#arr[*]} # Number of items in the array
${#arr[0]} # Length of item zero
The ${!arr[*]}
is a relatively new addition to bash, it was not part of the original array implementation.
The following example shows some simple array usage (note the "[index]=value"
assignment to assign a specific index):
#!/bin/bash
array=(one two three four [5]=five)
echo "Array size: ${#array[*]}"
echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done
echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done
echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done
Append array
arrVar=("AC" "TV" "Mobile" "Fridge" "Oven" "Blender")
# Add new element at the end of the array
arrVar+=("Dish Washer")
# Add new element at the end of the array
arrVar[${#arrVar[@]}]="Python"
# Add new element at the end of the array
arrVar=(${arrVar[@]} "Jack Fruit")
Split to array
version='1.2.33'
a=( ${version//./ } ) # replace points, split into array
((a[2]++)) # increment revision (or other part)
version="${a[0]}.${a[1]}.${a[2]}" # compose new version
Multi-dimensional array
declare -A URLS
URLS[0,0]="MY IP #50 Profile1"
URLS[0,1]="rtsp://192.168.0.50:554/media/1/1/Profile1"
URLS[1,0]="Localhost RTSP Server"
URLS[1,1]="rtsp://0.0.0.0:8554/live.sdp"
URLS[2,0]="Smatii DRONE wjcam003"
URLS[2,1]="rtsp://106.244.179.242:554/wjcam003"
URLS[3,0]="Smatii DRONE wjcam004"
URLS[3,1]="rtsp://106.244.179.242:554/wjcam004"
URLS_COUNT=$(( ${#URLS[*]} / 2 ))
URLS_MAX_INDEX=$(( URLS_COUNT - 1 ))
function print_urls
{
for (( i = 0; i < URLS_COUNT; ++i )); do
name=${URLS[$i,0]}
url=${URLS[$i,1]}
echo "[$i] $name: '$url'"
done
}
print_urls
Example
내가 사용하는 wget 스크립트:
#!/usr/bin/env bash
# Don't check the server certificate against the available certificate authorities.
FLAG[0]=--no-check-certificate
# Continue getting a partially-downloaded file.
FLAG[1]=--continue
# Turn on options suitable for mirroring.
# Equivalent to -r -N -l inf --no-remove-listing.
FLAG[2]=--mirror
# Identify as agent-string to the HTTP server.
#FLAG[3]=--user-agent=Wget
# In certain cases, the local file will be clobbered, or overwritten, upon repeated download.
FLAG[4]=--no-clobber
# After the download is complete, convert the links in the document to make them suitable for local viewing.
FLAG[5]=--convert-links
# Do not ever ascend to the parent directory when retrieving recursively.
#FLAG[6]=--no-parent
wget ${FLAG[*]} "$@"