linuxshell数组for
‘壹’ linux shell 脚本 求任意10个数的最大值。(使用数组)
读取10个数字存入数组中
然后使用一个变量max将它的初始值设置为数组中第一个元素的值
接着从数组中第二个元素开始遍历整个数组
如果某个元素的值大于max则将max设置为该值
如此遍历完整个数组后max中便是整个数组中最大的值
下面是一个简单的示例代码
#!/bin/bash
echo-n'输入10个数:'
read-aa
max=${a[0]};
foriin{1..9}
do
if[${a[$i]}>$max]
then
max=${a[$i]}
fi
done
echo"最大值为:$max"
exit0
‘贰’ linux shell中数组循环取值和赋值的问题。
[seesea@UC ~]$ cat a.txt
1
2
3
4
a
b
c
d
[seesea@UC ~]$ ar=( $(cat a.txt) )
[seesea@UC ~]$ for (( i = 0; i < ${#ar[@]}; ++i )); do echo "ar[$i] = ${ar[i]}"; done
ar[0] = 1
ar[1] = 2
ar[2] = 3
ar[3] = 4
ar[4] = a
ar[5] = b
ar[6] = c
ar[7] = d
用数组更方便吧。
‘叁’ linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素
题主你好,
代码及测试截图如下:
希望可以帮到题主, 欢迎追问.
‘肆’ linux shell里怎么用数组
split(s,a,fs) 以fs为指定分隔符将字符串s分割成一个数组a awk 'BEGIN{print split("aswd|sawa|eee",Ti,"|"),Ti[1]}'
‘伍’ Linux Shell awk中怎么调用数组和for循环
echo|awk'
{
array[1]=1
array[2]=2
array[3]=3
array[4]=4
array[5]=5
for(i=1;i<=5;++i){
printarray[i]
}
}'
‘陆’ Linux中编写一个shell程序,读入一组数字,使用for循环计算该数组数字的乘积。
#!/bin/bash
read-p"请输入一组数字:"-aarray
accumulate=1
fornumin${array[@]}
do
accumulate=$(($accumulate*num))
done
echo"乘积是:"$accumulate
计算读入的一组数字(在同一行输入)的积
‘柒’ Linux shell 数组array基础和$a的区别
Linux Bash中,数组变量的赋值有两种方法:
(1) name = (value1 ... valuen) // 此时下标从0开始;
(2) name[index] = value
下面以一个简单的脚本来说明,脚本内容如下:
#!/bin/bash
#定义数组
A=(a b c def)
#把数组按字符串显示输出
echo ${A[@]}
#屏幕显示:a b c def
#数组的长度表示${#A[*]}
len=${#A[*]}
echo ${#A[*]}
#屏幕显示:4
#改变数组元素的值
A[3]='vivian'
echo ${A[*]}
#屏幕显示:a b c vivian
#循环输出数组元素
i=0
while [ $i -lt $len ]
do
echo ${A[$i]}
let i++
done
#屏幕输出:
# a
# b
# c
# vivian
#循环输出数组元素的另一种写法,注意,在条件中,用#a[@]取其个数。
for ((i=0;i<${#A[@]};i=$i+1))
do
echo ${A[$i]}
done
#循环输出数组元素的另一种写法,注意,在条件中,引用变量一定要用双引号 ,否则报错。
for (( j=0; j<"$len"; j=j+1 )) //len表示数组长度值
do
echo ${A[$j]}
done
#循环输出数组元素的另一种写法,注意,${A[*]}不能写成$A ,$A默认是第一个元素。 如果A="a b c ded",就可以写$A,
for value in ${A[*]}
do
echo $value
done
ps:
若a=(1 2 3 4)表示所有元素,则其只能用${a[*]}或者${a[@]}来表示。在a=(1 2 3 4)中,$a只是表示第一个元素1。
若a="1 2 3 4"表示所有元素,则其可以用${a[*]}或者${a[@]}或者$a来表示。
假如a="1 2 3 4",则array=($a)就相当于a=(1 2 3 4),不信你可以试试echo ${array[@]}。
上面的例子还可以改写成以下内容:
for value in $A
do
echo $value
done
‘捌’ linux shell 使用数组拷贝文件
shell没有数组,不过下面的实施方式,不知道对你有帮组不?
#!/bin/sh
i=0
for filename in `ls`
do
echo "myfile$i=$filename" >> ./mydata
i=`expr $i + 1`
done
eval `cat ./mydata`
#以下演示
b=0
cat ./mydata | while read line
do
myarray="myfile$b"
eval echo "cp \$$myarray to some dir"
b=`expr $b + 1`
done
rm -f ./mydata
另外要判断是否执行成功,可调用$?,然后判断执行就行。
‘玖’ linux shell 关联数组的一个小问题
shell 并不支持关联数组。
只有awk才支持关联数组。
shell只支持index数字类型的数组,凡是不是数字的,都会当做0,或者-1,也就是最后的那个元素。
详细参见bash的man手册。
Arrays
Bash provides one-dimensional array variables. Any variable may be used as an array; the declare builtin
will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement
that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.
An array is created automatically if any variable is assigned to using the syntax name[subscript]=value.
The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal
to zero. To explicitly declare an array, use declare -a name (see SHELL BUILTIN COMMANDS below).
declare -a name[subscript] is also accepted; the subscript is ignored. Attributes may be specified for
an array variable using the declare and readonly builtins. Each attribute applies to all members of an
array.
Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value
is of the form [subscript]=string. Only string is required. If the optional brackets and subscript are
supplied, that index is assigned to; otherwise the index of the element assigned is the last index
assigned to by the statement plus one. Indexing starts at zero. This syntax is also accepted by the
declare builtin. Indivial array elements may be assigned to using the name[subscript]=value syntax
introced above.
Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid
conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name.
These subscripts differ only when the word appears within double quotes. If the word is double-quoted,
${name[*]} expands to a single word with the value of each array member separated by the first character
of the IFS special variable, and ${name[@]} expands each element of name to a separate word. When there
are no array members, ${name[@]} expands to nothing. If the double-quoted expansion occurs within a
word, the expansion of the first parameter is joined with the beginning part of the original word, and
the expansion of the last parameter is joined with the last part of the original word. This is analogous
to the expansion of the special parameters * and @ (see Special Parameters above). ${#name[subscript]}
expands to the length of ${name[subscript]}. If subscript is * or @, the expansion is the number of ele-
ments in the array. Referencing an array variable without a subscript is equivalent to referencing ele-
ment zero.
The unset builtin is used to destroy arrays. unset name[subscript] destroys the array element at index
subscript. Care must be taken to avoid unwanted side effects caused by filename generation. unset name,
where name is an array, or unset name[subscript], where subscript is * or @, removes the entire array.
The declare, local, and readonly builtins each accept a -a option to specify an array. The read builtin
accepts a -a option to assign a list of words read from the standard input to an array. The set and
declare builtins display array values in a way that allows them to be reused as assignments.
‘拾’ shell中数组怎么循环赋值,急
Bash环境可以这样。
#!/bin/bash
for ((i=1;i<=100;i++))
do
name[$i]=$i
echo ${name[$i]} #为方便检查,加了打印
done
通用点的(符合POSIX标准)可以这样:
#!/bin/sh
declare -a name
for i in `seq 100`
do
name[$i]=$i
echo ${name[$i]} #为方便检查,加了打印
done