百度
360搜索
搜狗搜索

shell数组,shell函数怎么返回一个关联数组详细介绍

本文目录一览: shell数组长度一直是1

shell数组长度一直是1。根据查询相关公开信息显示,Shell中的所有变量都是数组类型,普通变量是一个长度为1,下标为0的数组。

linux shell 关联数组的一个小问题

因为关联数组是 declare -A array。大写的
declare -A xxx
-A Each name is an associative array variable
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. Individual array elements may be assigned to using the name[subscript]=value syntax
introduced 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.

阅读更多 >>>  linux如何恢复默认PS1

shell中数组1(1,2,3),数组2(a,b,c)如何合拼两个数组为数组3(a1,b2,c3)?

连个for循环,然后用字符串拼接的方法得到第三个数组
数组1
a=【1,2,3】
数组2
b=【a,b,c】
数组3
c=【】
for i in a:
for j in b:
c.append(j+i)
这个代码有点问题,只能适用数组1和数组2都是字符的情况下

shell 打乱数组顺序

走到设置进行重新数字进行排列。按顺序进行。
兆了么对不起垡\吵了架势利,学习惯了[晚安],点击[ http://pinyin.cn/e293157 ]查看表情
最简单的方法:
利用 Shell 的 $RANDOM 变量给原文件的每一行加上随机的行号然后根据这个随机行号进行排序,再把临时加上去的行号给过滤掉,这样操作之后得到的新文件就相当于被随机“洗”了一次:
while read i;do echo "$i $RANDOM";done
<file|sort -k2n|cut -d" " -f1 最笨的方法:
类似于穷举法,构造一个散列来记录已经打印行出现行的次数,如果出现次数多于一次则不进行处理,这样可以防止重复,但缺点是加大了系统的开销:

awk -v N=`sed -n '$=' data` 'BEGIN{FS="\n";RS=""}{srand();while(t!=N){x=int(N*rand()+1);a[x]++; if(a[x]==1){print $x;t++}}}' data

散列的方法:

利用AWK中散列的特性(详细请看:info gawk 中的7.x ),只要构造一个随机不重复的散列函数即可,因为一个文件每行的linenumber是独一无二的,所以用:

随机数+每行linenumber ------对应------> 那一行的内容

即为所构造的随机函数。从而有:

awk 'BEGIN{srand()}{b[rand()NR]=$0}END{for(x in b)print b[x]}' data

linux shell脚本中,数组名称是一个变量,怎么打印出它里面的元素?

题主你好,
代码及测试截图如下:
下面这个例子可能对题主有帮助:
说明: array和array1是两个数组, 其中的内容分别为aa bb cc和dd ee ff, 然后再定义一个数组array2,这个数组中的元素为array和array1,最后使用for循环,通过array2数组将array和array1中的元素输出.
希望可以帮到题主, 欢迎追问.

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
其实差不多
for((i=1;i<=100;i++))
do
name[$i]=$i;
done
shell里需要加 do 和done 限定循环的范围
i=1;
while(test i -le 100)
do
name[$i]=$i;
i=$[$i+1];
done
for((i=0; i<100; i++))
do
name[$i]=$i
done
echo ${name[@]}
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
bash手册 arrays章节第一段话的某一句:
数组的大小没有上限,也没有限制在连续对成员引用和赋值时有什么要求。数组以整数为下标,从 0 开始。
使用cat的话可以这样:
cat 123.txt | while read line
do
echo "$line"
done
123.txt的每行读取到变量line中并输出。

shell函数怎么返回一个关联数组

Shell函数返回值,常用的两种方式:return,echo
1) return 语句
shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回。
示例:
#!/bin/sh
function test()
{
echo "arg1 = $1"
if [ $1 = "1" ] ;then
return 1
else
return 0
fi
}
echo
echo "test 1"
test 1
echo $? # print return result
echo
echo "test 0"
test 0
echo $? # print return result
echo
echo "test 2"
test 2
echo $? # print return result
# 实例演示shell函数返回数组,准确说应该是返回字符串,然后通过一定的构造得到一个数组
# 重点在于自己怎么样去构造适合用数组存储的数据格式
# 函数 thinker()
# 功能 将附加在脚本末尾的 域名:IP 抽取出来,存储在字符串变量中,并返回该变量
function thinker(){
# 这里是过滤脚本本身尾部的域名ip区域
vars=`cat $0 | sed -n '/# BEGINVAR$/,/# ENDVAR$/p' | grep -v -E '# BEGINVAR|# ENDVAR' | sed -n 's/^# //gp'`
echo $vars
}
# 这里演示了获取shell函数返回值
# 我这里需要将返回值存放到数组中,通过下面的形式就构造了一个数组了
domainip=(`thinker`)
echo ${domainip[0]}
echo ${domainip[1]}
#
# 脚本的要实现的具体功能部分就省略了
# BEGINVAR
# xx2.yy.com:131.10.238.190
# xx3.yy.com:133.106.227.132
# xx4.yy.com:123.160.19.138
# xx5.yy.com:131.10.18.177
# ENDVAR

阅读更多 >>>  powershell是什么,powershell和cmd命令有什么区别?

shell多行数据用数组接收

因为你前面使用了管道把数据传给 while,管道会启动一个“子进程”,while是在子进程中执行的,子进程中的变量是不会返回到父进程中的,所以你 while 结束后再看 arr 是空的,因为 while 里的 arr 是子进程的变量。改成如下就可以了:
i=0while read linedo arr[${i}]=`echo ${line} | awk -F":" '{print $1}'` (( ++i ))done < a.txtecho "${arr[@]}"

shell 如何定义空数组

array=()echo ${array[*]}这样就是空的
array=(123 456 789)echo ${array[*]}这样就是非空

8. shell将字符串以逗号分割转成数组(借助IFS)

原理是将变化shell环境下的一个系统变量IFS

#!/bin/bash

function to_array()

{

x=$1

OLD_IFS="$IFS" #默认的IFS值为换行符

IFS=","

array=($x) ?#以逗号进行分割了

IFS="$OLD_IFS" #还原默认换行符

for each in ${array[*]}

do

echo $each

done

}

arr=($(to_array 'a,b,c,d,e'))

echo ${arr[*]}

参考: shell分割字符串为数组

另外一个例子,介绍IFS的用法。 参考 shell中的特殊变量IFS

比如,有个文件内容如下:

? ? ? the first line.

the second line.

the third line.

打印每行:

forline in `cat filename`

do

echo $line

done

结果是下面这种一行一个单词,显然是不符合预期的:

the

first

line.

the

second

line.

the

third

line.

借助IFS变量进行做个调整:

IFS=$'\n'

for line in `cat k.shfile`

do

echo $line

done

输出就是正确的:

? ? the first line.

the second line.

the third line.

网站数据信息

"shell数组,shell函数怎么返回一个关联数组"浏览人数已经达到21次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:shell数组,shell函数怎么返回一个关联数组的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!