Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 比较数组元素,如果不是,则添加_Arrays_Bash_Compare - Fatal编程技术网

Arrays 比较数组元素,如果不是,则添加

Arrays 比较数组元素,如果不是,则添加,arrays,bash,compare,Arrays,Bash,Compare,我有一个有两种图片的目录。以年份开头的照片名称(e.q,20131118 SpecificNumber)和以swSPECIFICNUMBER开头的草稿名称 我想先将ls sw*插入阵列: i=0 while read line do array1[ $i ]="$line" (( i++ )) done < <(ls sw*) 对于高于4.0的版本,可以使用bash关联的数组(我不确定早于4.0的bash版本是否支持此功能) 希望有帮助。不要

我有一个有两种图片的目录。以年份开头的照片名称(e.q,20131118 SpecificNumber)和以swSPECIFICNUMBER开头的草稿名称

我想先将ls sw*插入阵列:

    i=0
while read line
do
    array1[ $i ]="$line"        
    (( i++ ))
done < <(ls sw*)

对于高于4.0的版本,可以使用bash关联的数组(我不确定早于4.0的bash版本是否支持此功能)


希望有帮助。

不要像这样使用
ls
array1=(sw*)
array2=(20*)
将使用所需的文件名集填充数组。感谢您的提示:)
j=0
    while read line
    do
        array2[ $j ]="$line"        
        (( j++ ))
    done < <(ls 20*)
for t in "${Array2[@]}"; do
     skip=
     for q not in "${Array1[@]}"; do
        [[ $t == $q ]] && { skip=1; break; }
     done
     [[ -n $skip ]] || Array1+=("$t")
 done
#!/bin/bash

# preparing the two arrays
array1=($(ls -1 20*))
array2=($(ls -1 sw*))

# create a dictionary
# key is specific number
# value is an integer greater than 0
value=1
declare -A dict  # declare an associated array
for elem in ${array1[@]}; do
    key=${elem:8}  # pick the specific number after date
    dict[$key]=$value
    value=$((value+1))
done

i=${#array1[@]}
for elem in ${array2[@]}; do
    key=${elem:2}  # pick the specific number after "sw"
    if [ -z "${dict[$key]}" ]; then
        array1[i]=$elem
        i=$((i+1))
    fi
done