반응형
쉘에서 배열의 길이를 찾는 방법은 무엇입니까?
유닉스 쉘에서 배열 길이를 찾는 방법은 무엇입니까?
$$ a=(1 2 3 4)
$$ echo ${#a[@]}
4
bash 가정 :
~> declare -a foo
~> foo[0]="foo"
~> foo[1]="bar"
~> foo[2]="baz"
~> echo ${#foo[*]}
3
따라서 ${#ARRAY[*]}
배열의 길이로 확장됩니다 ARRAY
.
Bash 매뉴얼에서 :
$ {# parameter}
확장 된 매개 변수 값의 문자 길이가 대체됩니다. 매개 변수가 ' '또는 '@'인 경우 대체 된 값은 위치 매개 변수의 수입니다. 매개 변수가 ' '또는 '@'가 첨자 인 배열 이름 인 경우 대체 된 값은 배열의 요소 수입니다. 매개 변수가 음수로 첨자 화 된 색인 배열 이름 인 경우 해당 숫자는 매개 변수의 최대 색인보다 큰 값에 상대적인 것으로 해석되므로 음수 색인은 배열의 끝부터 다시 카운트되고 -1 색인은 마지막을 참조합니다. 요소.
문자열, 배열 및 연관 배열의 길이
string="0123456789" # create a string of 10 characters
array=(0 1 2 3 4 5 6 7 8 9) # create an indexed array of 10 elements
declare -A hash
hash=([one]=1 [two]=2 [three]=3) # create an associative array of 3 elements
echo "string length is: ${#string}" # length of string
echo "array length is: ${#array[@]}" # length of array using @ as the index
echo "array length is: ${#array[*]}" # length of array using * as the index
echo "hash length is: ${#hash[@]}" # length of array using @ as the index
echo "hash length is: ${#hash[*]}" # length of array using * as the index
산출:
string length is: 10
array length is: 10
array length is: 10
hash length is: 3
hash length is: 3
다루기 $@
, 인수 배열 :
set arg1 arg2 "arg 3"
args_copy=("$@")
echo "number of args is: $#"
echo "number of args is: ${#@}"
echo "args_copy length is: ${#args_copy[@]}"
산출:
number of args is: 3
number of args is: 3
args_copy length is: 3
tcsh 또는 csh에서 :
~> set a = ( 1 2 3 4 5 )
~> echo $#a
5
에서 물고기 쉘 배열의 길이를 찾을 수 있습니다 :
$ set a 1 2 3 4
$ count $a
4
이것은 나를 위해 잘 작동합니다
arglen=$#
argparam=$*
if [ $arglen -eq '3' ];
then
echo Valid Number of arguments
echo "Arguments are $*"
else
echo only four arguments are allowed
fi
배열의 길이를 변수에 넣는 방법을 찾는 사람들을 위해 :
foo=$(echo ${'ARRAY[*]}
참고 URL : https://stackoverflow.com/questions/1886374/how-to-find-the-length-of-an-array-in-shell
반응형
'program tip' 카테고리의 다른 글
Objective C 메시지 디스패치 메커니즘 (0) | 2020.12.08 |
---|---|
이것이 null인지 확인 (0) | 2020.12.08 |
참조 DLL 파일이 배포 프로젝트로 bin에 복사되지 않아 오류가 발생합니다. (0) | 2020.12.08 |
Windows에서 Postgresql 구성 파일 'postgresql.conf'는 어디에 있습니까? (0) | 2020.12.08 |
Newtonsoft는 속성을 무시합니까? (0) | 2020.12.08 |