Arrays 比较b/w两个数组,然后比较其他函数的逻辑OR操作(GNU bash 4.2版本)
我是unix新手,正在学习它的基础知识。我想比较两个不同数组的相同索引,在比较的基础上,我想把一个数据放到另一个数组中。首先我尝试将其用于字符串类型数组,但没有成功,然后我又尝试将其用于数字数组。我失败了,所以我决定讨论我尝试了OR语句的多种组合,并且还将索引数组元素作为参数传递,但仍然不起作用。我只想做其他的功能,而不是在同一个功能Arrays 比较b/w两个数组,然后比较其他函数的逻辑OR操作(GNU bash 4.2版本),arrays,bash,shell,unix,Arrays,Bash,Shell,Unix,我是unix新手,正在学习它的基础知识。我想比较两个不同数组的相同索引,在比较的基础上,我想把一个数据放到另一个数组中。首先我尝试将其用于字符串类型数组,但没有成功,然后我又尝试将其用于数字数组。我失败了,所以我决定讨论我尝试了OR语句的多种组合,并且还将索引数组元素作为参数传递,但仍然不起作用。我只想做其他的功能,而不是在同一个功能 Problem Description 1) suppose if at binary_data_1[0]=0 and binary_data_2[0]=1
Problem Description
1) suppose if at binary_data_1[0]=0
and binary_data_2[0]=1
then at master_binary_data[0]=0
means compare the binary_data_1 or binary_data_2 for same index and if
any element is 0 it will insert the value 0 for that index for master_binary_data[ ] array
2) suppose if at string_array[0]='A'
and string_array2[0]='B'
then at master_string_array[0]='A'
means if any record is 'A' at any index master_string_array[] for that index should
be 'A'
Now suppose my arrays in the function1
#as
loopcount1=0
function1()
{
binary_data_1=(0 1 0 1 1 1 0 1 0 1 0 1 0)
binary_data_2=(1 1 0 1 1 1 0 1 0 1 1 1 0)
string_array=( 'A' 'B' 'A' 'A' 'B' 'A' )
string_array2=( 'B' 'A' 'B' 'A' 'B' 'B' )
}
#at by using the function2 I wanted to achive the same
function2()
{
#
# I tried if [ "${binary_data_1[$loopcount1]}" -eq 0 ] || [ "${binary_data_2[$loopcount1]}" -eq 0 ] | bash -x
# then
# master_binary_data[$loopcount1]=0
# I tried if [ "${string_array[$loopcount1]}" == 'A' ] || [ "${string_array[$loopcount1]}" == 'A' ] | bash -x
# master_string_array[$loopcount1]=A
}
### Also passed each elements of array as a parameter but still not works
EDITED CODE
lopcount1=0
set -A binary_data_1 0 1 0 1 1 1 0 1 0 1 0 1 0
set -A binary_data_2 1 1 0 1 1 1 0 1 0 1 1 1 0
set -A string_array 'A' 'B' 'A' 'A' 'B' 'A'
set -A string_array2 'B' 'A' 'B' 'A' 'B' 'B'
echo "${string_array[@]}"
echo "${string_array2[@]}"
echo "${binary_data_1[@]}"
echo "${binary_data_2[@]}"
echo " Function 2 called "
function2
#at by using the function2 I wanted to achive the same
function2()
{
if [ "${binary_data_1[$loopcount1]}" -eq 0 ] || [ "${binary_data_2[$loopcount1]}" -eq 0 ]
then
master_binary_data[$loopcount1]=0
fi
if [ "${string_array[$loopcount1]}" == 'A' ] || [ "${string_array2[$loopcount1]}" == 'A' ]
then
master_string_array[$loopcount1]=A
fi
loopcount1=`expr loopcount1+1`
}
echo "${master_string_array[@]}"
echo "${master_binary_data[@]}"
echo "Function 2 ends here "
您需要全局声明数组,然后在函数2中使用它们。 下面的代码就可以了
#!/bin/bash
lopcount1=0
binary_data_1=(0 1 0 1 1 1 0 1 0 1 0 1 0)
binary_data_2=(1 1 0 1 1 1 0 1 0 1 1 1 0)
string_array=( 'A' 'B' 'A' 'A' 'B' 'A' )
string_array2=( 'B' 'A' 'B' 'A' 'B' 'B' )
#at by using the function2 I wanted to achive the same
function2()
{
if [ "${binary_data_1[$loopcount1]}" -eq 0 ] || [ "${binary_data_2[$loopcount1]}" -eq 0 ] | bash -x
then
master_binary_data[$loopcount1]=0
fi
if [ "${string_array[$loopcount1]}" == 'A' ] || [ "${string_array[$loopcount1]}" == 'A' ] | bash -x
then
master_string_array[$loopcount1]=A
fi
}
修改
#!/bin/bash
loopcount1=0
binary_data_1=(0 1 0 1 1 1 0 1 0 1 0 1 0 )
binary_data_2=(1 1 0 1 1 1 0 1 0 1 1 1 0)
string_array=('A' 'B' 'A' 'A' 'B' 'A')
string_array2=('B' 'A' 'B' 'A' 'B' 'B')
echo "${string_array[@]}"
echo "${string_array2[@]}"
echo "${binary_data_1[@]}"
echo "${binary_data_2[@]}"
echo " Function 2 called "
#at by using the function2 I wanted to achive the same
function2()
{
if [ "${binary_data_1[$loopcount1]}" -eq 0 ] || [ "${binary_data_2[$loopcount1]}" -eq 0 ]
then
master_binary_data[$loopcount1]=0
fi
if [ "${string_array[$loopcount1]}" == 'A' ] || [ "${string_array2[$loopcount1]}" == 'A' ]
then
master_string_array[$loopcount1]=A
fi
loopcount1=`expr loopcount1+1`
}
function2
echo "${master_string_array[@]}"
echo "${master_binary_data[@]}"
echo "Function 2 ends here "
在Unix上,任何元素都可以是数组,因此,如果我只声明了一个元素,那么在函数中将该元素作为数组并在另一个函数中使用它是可能的吗?您可能需要将数组作为参数传递给另一个函数,例如function2 binary\u data\u 1 binary\u data\u 2,然后在function2中使用这些参数,如下面-$1对应于第一个数组,$2对应于第二个数组如果答案/注释有用,请您接受答案??然后在函数2中假设一个数组包含10个元素,那么如何使用$1获取这10个元素是我们必须使用另一个数组意思是数组[]=$1我已经试过了,但不起作用。如果可能的话,请在回答中修改它。你需要在最后调用函数2,在它定义之前调用它。这就是它不起作用的原因。我在我的答案中张贴修改后的代码,供您在修改后的标签下参考