![Unix Bourne Shell 中的数组](https://linux22.com/image/38284/Unix%20Bourne%20Shell%20%E4%B8%AD%E7%9A%84%E6%95%B0%E7%BB%84.png)
我正在尝试在 Bourne shell ( /bin/sh
) 中使用数组。我发现初始化数组元素的方法是:
arr=(1 2 3)
但它遇到了一个错误:
syntax error at line 8: `arr=' unexpected
现在,我发现此语法的帖子说它是 for bash
,但我找不到 Bourne shell 的任何单独语法。语法/bin/sh
也一样吗?
答案1
/bin/sh
如今,在任何系统上几乎都没有 Bourne shell(即使是最后一个包含它的主要系统之一的 Solaris,现在也已将 Solaris 11 中的 /bin/sh 切换为 POSIX sh)。/bin/sh
是70年代初的汤普森炮弹。 1979 年,Bourne shell 在 Unix V7 中取代了它。
/bin/sh
此后多年一直是 Bourne shell(或 Almquist shell,BSD 上的免费重新实现)。
如今,/bin/sh
更常见的是 POSIXsh
语言的解释器或其他解释器,它本身基于 ksh88 语言的子集(以及 Bourne shell 语言的超集,但存在一些不兼容性)。
Bourne shell 或 POSIX sh 语言规范不支持数组。或者更确切地说,它们只有一个数组:位置参数($1
、$2
、$@
,因此每个函数也有一个数组)。
ksh88 确实有您使用 设置的数组set -A
,但没有在 POSIX sh 中指定,因为语法很尴尬并且不太可用。
其他带有数组/列表变量的 shell 包括:csh
/ tcsh
、rc
、es
、bash
(主要以 ksh93 方式复制 ksh 语法)、yash
、zsh
,fish
每个都有不同的语法(rc
Unix 的未来继承者的 shell,fish
并且zsh
是最一致的那些)...
在标准中sh
(也适用于现代版本的 Bourne shell):
set '1st element' 2 3 # setting the array
set -- "$@" more # adding elements to the end of the array
shift 2 # removing elements (here 2) from the beginning of the array
printf '<%s>\n' "$@" # passing all the elements of the $@ array
# as arguments to a command
for i do # looping over the elements of the $@ array ($1, $2...)
printf 'Looping over "%s"\n' "$i"
done
printf '%s\n' "$1" # accessing individual element of the array.
# up to the 9th only with the Bourne shell though
# (only the Bourne shell), and note that you need
# the braces (as in "${10}") past the 9th in other
# shells (except zsh, when not in sh emulation and
# most ash-based shells).
printf '%s\n' "$# elements in the array"
printf '%s\n' "$*" # join the elements of the array with the
# first character (byte in some implementations)
# of $IFS (not in the Bourne shell where it's on
# space instead regardless of the value of $IFS)
(请注意,在 Bourne shell 和 ksh88 中,$IFS
必须包含空格字符才能"$@"
正常工作(一个错误),并且在 Bourne shell 中,您无法访问上面的元素$9
(${10}
不起作用,您仍然可以执行shift 1; echo "$9"
或循环他们))。
答案2
正如其他人所说,Bourne Shell 没有真的数组。
但是,根据您需要执行的操作,分隔字符串应该足够了:
sentence="I don't need arrays because I can use delimited strings"
for word in $sentence
do
printf '%s\n' "$word"
done
如果典型的分隔符(空格、制表符和换行符)不够,您可以设置IFS
循环之前您想要的任何分隔符。
如果您需要以编程方式构建数组,则只需构建一个分隔字符串即可。
答案3
普通 Bourne shell 中没有数组。可以使用以下方式创建数组并遍历它:
#!/bin/sh
# ARRAY.sh: example usage of arrays in Bourne Shell
array_traverse()
{
for i in $(seq 1 $2)
do
current_value=$1$i
echo $(eval echo \$$current_value)
done
return 1
}
ARRAY_1=one
ARRAY_2=two
ARRAY_3=333
array_traverse ARRAY_ 3
无论你选择哪种方式使用数组,sh
它总是很麻烦。如果可以的话,请考虑使用不同的语言,例如Python
或 ,Perl
除非您受限于非常有限的平台或想要学习一些东西。
答案4
一种在破折号中模拟数组的方法(它可以适应数组的任意维数):(请注意,使用该seq
命令需要将其IFS
设置为“ ”(空格=默认值)。您可以使用while ... do ...
或do ... while ...
循环以避免这种情况(我保持seq
在更好地说明代码的作用的范围内)。)
#!/bin/sh
## The following functions implement vectors (arrays) operations in dash:
## Definition of a vector <v>:
## v_0 - variable that stores the number of elements of the vector
## v_1..v_n, where n=v_0 - variables that store the values of the vector elements
VectorAddElementNext () {
# Vector Add Element Next
# Adds the string contained in variable $2 in the next element position (vector length + 1) in vector $1
local elem_value
local vector_length
local elem_name
eval elem_value=\"\$$2\"
eval vector_length=\$$1\_0
if [ -z "$vector_length" ]; then
vector_length=$((0))
fi
vector_length=$(( vector_length + 1 ))
elem_name=$1_$vector_length
eval $elem_name=\"\$elem_value\"
eval $1_0=$vector_length
}
VectorAddElementDVNext () {
# Vector Add Element Direct Value Next
# Adds the string $2 in the next element position (vector length + 1) in vector $1
local elem_value
local vector_length
local elem_name
eval elem_value="$2"
eval vector_length=\$$1\_0
if [ -z "$vector_length" ]; then
vector_length=$((0))
fi
vector_length=$(( vector_length + 1 ))
elem_name=$1_$vector_length
eval $elem_name=\"\$elem_value\"
eval $1_0=$vector_length
}
VectorAddElement () {
# Vector Add Element
# Adds the string contained in the variable $3 in the position contained in $2 (variable or direct value) in the vector $1
local elem_value
local elem_position
local vector_length
local elem_name
eval elem_value=\"\$$3\"
elem_position=$(($2))
eval vector_length=\$$1\_0
if [ -z "$vector_length" ]; then
vector_length=$((0))
fi
if [ $elem_position -ge $vector_length ]; then
vector_length=$elem_position
fi
elem_name=$1_$elem_position
eval $elem_name=\"\$elem_value\"
if [ ! $elem_position -eq 0 ]; then
eval $1_0=$vector_length
fi
}
VectorAddElementDV () {
# Vector Add Element
# Adds the string $3 in the position $2 (variable or direct value) in the vector $1
local elem_value
local elem_position
local vector_length
local elem_name
eval elem_value="$3"
elem_position=$(($2))
eval vector_length=\$$1\_0
if [ -z "$vector_length" ]; then
vector_length=$((0))
fi
if [ $elem_position -ge $vector_length ]; then
vector_length=$elem_position
fi
elem_name=$1_$elem_position
eval $elem_name=\"\$elem_value\"
if [ ! $elem_position -eq 0 ]; then
eval $1_0=$vector_length
fi
}
VectorPrint () {
# Vector Print
# Prints all the elements names and values of the vector $1 on sepparate lines
local vector_length
vector_length=$(($1_0))
if [ "$vector_length" = "0" ]; then
echo "Vector \"$1\" is empty!"
else
echo "Vector \"$1\":"
for i in $(seq 1 $vector_length); do
eval echo \"[$i]: \\\"\$$1\_$i\\\"\"
###OR: eval printf \'\%s\\\n\' \"[\$i]: \\\"\$$1\_$i\\\"\"
done
fi
}
VectorDestroy () {
# Vector Destroy
# Empties all the elements values of the vector $1
local vector_length
vector_length=$(($1_0))
if [ ! "$vector_length" = "0" ]; then
for i in $(seq 1 $vector_length); do
unset $1_$i
done
unset $1_0
fi
}
##################
### MAIN START ###
##################
## Setting vector 'params' with all the parameters received by the script:
for i in $(seq 1 $#); do
eval param="\${$i}"
VectorAddElementNext params param
done
# Printing the vector 'params':
VectorPrint params
read temp
## Setting vector 'params2' with the elements of the vector 'params' in reversed order:
if [ -n "$params_0" ]; then
for i in $(seq 1 $params_0); do
count=$((params_0-i+1))
VectorAddElement params2 count params_$i
done
fi
# Printing the vector 'params2':
VectorPrint params2
read temp
## Getting the values of 'params2'`s elements and printing them:
if [ -n "$params2_0" ]; then
echo "Printing the elements of the vector 'params2':"
for i in $(seq 1 $params2_0); do
eval current_elem_value=\"\$params2\_$i\"
echo "params2_$i=\"$current_elem_value\""
done
else
echo "Vector 'params2' is empty!"
fi
read temp
## Creating a two dimensional array ('a'):
for i in $(seq 1 10); do
VectorAddElement a 0 i
for j in $(seq 1 8); do
value=$(( 8 * ( i - 1 ) + j ))
VectorAddElementDV a_$i $j $value
done
done
## Manually printing the two dimensional array ('a'):
echo "Printing the two-dimensional array 'a':"
if [ -n "$a_0" ]; then
for i in $(seq 1 $a_0); do
eval current_vector_lenght=\$a\_$i\_0
if [ -n "$current_vector_lenght" ]; then
for j in $(seq 1 $current_vector_lenght); do
eval value=\"\$a\_$i\_$j\"
printf "$value "
done
fi
printf "\n"
done
fi
################
### MAIN END ###
################