最优雅的技术,用于比较bash数组的内容和文件的内容

时间:2012-11-16 13:11:40

标签: bash

我想将数组的内容与文件的内容进行比较。我想最好的解决方案是:

b=( some data )
a=$(<file)
if [ $a -ne ${b[@]} ]
then
    echo "variables are different"
fi

我说错了吗?

2 个答案:

答案 0 :(得分:2)

试试这个:

$ cat file
a
b
c
$ echo -n "arrays are "
$ x1=( a b c )
$ mapfile -t x2 < file
$ [[ ${x1[@]} == ${x2[@]} ]] && echo "identical" || echo >&2 "different"

答案 1 :(得分:-1)

使用Bash的流程替换:

b=( some data )
if ! diff <(echo ${b[*]}) file; then
  echo "different"
fi