當前位置:首頁 » 操作系統 » linuxshell數組for

linuxshell數組for

發布時間: 2022-07-04 15:34:22

『壹』 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

熱點內容
hp存儲擴容 發布:2024-11-17 23:29:16 瀏覽:569
在ftp中put表示什麼 發布:2024-11-17 23:29:12 瀏覽:383
mvc多文件上傳 發布:2024-11-17 23:13:56 瀏覽:155
玩游戲硬碟緩存32m 發布:2024-11-17 23:03:42 瀏覽:525
藍光存儲系統 發布:2024-11-17 23:03:41 瀏覽:436
地平線4提示配置低於最低怎麼辦 發布:2024-11-17 22:54:38 瀏覽:611
注冊銀行卡賬戶密碼填什麼 發布:2024-11-17 22:54:35 瀏覽:537
java壓縮上傳圖片 發布:2024-11-17 22:26:59 瀏覽:627
plc編程課件 發布:2024-11-17 22:18:23 瀏覽:469
我的世界伺服器信號一直在檢測 發布:2024-11-17 22:09:52 瀏覽:547