-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/bin/bash | ||
############## | ||
##Author: yul1 | ||
##Date: 2019-09-13 11:29:25 | ||
##LastEditors: yul1 | ||
##LastEditTime: 2019-09-13 11:32:32 | ||
##Description: | ||
############## | ||
|
||
#初始化定义三个数组 | ||
arry1=(A B C) | ||
arry2=(D E F) | ||
arry3=(G H I) | ||
# | ||
#使用for循环来读取数组中元素的个数,每次读取完一个数组将其打印到屏幕上并继续读取 | ||
for ((i=0;i<4;i++)) | ||
do | ||
eval value=\${arry${i}[@]} | ||
for element in ${value} | ||
do | ||
echo -e ${value} | ||
continue 2 | ||
done | ||
done | ||
echo | ||
|
||
#定义三个一维数组 | ||
array1="A B C" | ||
array2="D E F" | ||
array3="G H I" | ||
# | ||
#使用for语句来循环读取所定义的数组中的元素并暂存到变量i中 | ||
#将暂存在变量i中的元素赋予变量value | ||
#使用for语句读取变量value中的值 每次读取完后都打印到标准输出直到读取完成 | ||
for i in array1 array2 array3 | ||
do | ||
eval value=\$$i | ||
for j in $value | ||
do | ||
echo -e $value | ||
continue 2 | ||
done | ||
done | ||
|
||
|
||
#初始化第一个数组 | ||
array2=( | ||
element2 | ||
element3 | ||
element4 | ||
) | ||
#初始化第二个数组 | ||
array3=( | ||
element5 element6 element7 | ||
) | ||
#定义一个函数 将所定义的两个一维数组组合成一个二维数组并显示到屏幕上 | ||
ARRAY() | ||
{ | ||
echo | ||
echo ">>Two-dimensional array<<" | ||
echo | ||
echo "${array2[*]}" | ||
echo "${array3[*]}" | ||
} | ||
# | ||
ARRAY | ||
echo array | ||
|
||
|
||
|
||
declare -i j=0 | ||
declare -i limit=4 | ||
# | ||
#初始化一个一维数组 | ||
array=(34 35 36 37 38 39) | ||
# | ||
echo "Two-dimensional array" | ||
#使用while循环完成对一维数组元素的读取 并将读取的元素重新组成一个二维数组后输出 | ||
while [ $j -lt $limit ] | ||
do | ||
#对数组array中的元素每次都从第$j个元素开始读取且读取的数目为3 | ||
echo "${array[*]:$j:3}" | ||
let j+=2 | ||
let j++ | ||
done | ||
echo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
############## | ||
##Author: yul1 | ||
##Date: 2019-09-13 11:29:25 | ||
##LastEditors: yul1 | ||
##LastEditTime: 2019-09-13 12:03:10 | ||
##Description: | ||
############## | ||
|
||
#在示例中,行分隔符(第1维)是空格字符。为了引入字段分隔符(第二维),使用标准unix工具tr。用于附加尺寸的附加分隔符可以以相同的方式使用。 | ||
|
||
#当然,这种方法的性能不是很好,但是如果性能是不是一个标准,这种做法是非常通用的,可以解决很多问题: | ||
|
||
array2d="1.1:1.2:1.3 2.1:2.2 3.1:3.2:3.3:3.4" | ||
|
||
function process2ndDimension { | ||
for dimension2 in $* | ||
do | ||
echo -n $dimension2 " " | ||
done | ||
echo | ||
} | ||
|
||
function process1stDimension { | ||
for dimension1 in $array2d | ||
do | ||
process2ndDimension `echo $dimension1 | tr : " "` | ||
done | ||
} | ||
|
||
process1stDimension | ||
|
||
##该样品的输出是这样的: | ||
|
||
##1.1 1.2 1.3 | ||
##2.1 2.2 | ||
##3.1 3.2 3.3 3.4 | ||
|
||
|
||
|
||
|
||
#!/bin/bash | ||
##提取控制台上w命令给出的:USER,TTY和FROM值.在bash中我试图获取此输出并将这些值放入多维数组(或只是一个带空格分隔符的数组). | ||
w|awk '{if(NR > 2) print $1,$2,$3}' | while read line | ||
do | ||
USERS+=("$line") | ||
echo ${#USERS[@]} | ||
done | ||
echo ${#USERS[@]} | ||
|
||
#!/bin/bash | ||
USERS=() | ||
shopt -s lastpipe | ||
w | awk '{if(NR > 2) print $1,$2,$3}' | while read line; do | ||
USERS+=("$line") | ||
done | ||
echo ${#USERS[@]} | ||
|
||
##可以使用process substitution而不是管道,以便read命令在主shell进程中运行. | ||
|
||
#!/bin/bash | ||
USERS=() | ||
while read line; do | ||
USERS+=("$line") | ||
done < <(w | awk '{if(NR > 2) print $1,$2,$3}') | ||
echo ${#USERS[@]} | ||
|
||
##可以使用可移植方法,该方法适用于没有进程暂停或ksh / zsh行为的shell,例如Bourne,dash和pdksh. (对于数组,您仍然需要(pd)ksh,bash或zsh.)运行需要管道内管道数据的所有内容. | ||
|
||
#!/bin/bash | ||
USERS=() | ||
shopt -s lastpipe | ||
w | awk '{if(NR > 2) print $1,$2,$3}' | { | ||
while read line; do | ||
USERS+=("$line") | ||
done | ||
echo ${#USERS[@]} | ||
} |