1

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

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

我对么?

4

2 回答 2

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"
于 2012-11-16T13:23:55.513 回答
-1

通过使用 Bash 的进程替换:

b=( some data )
if ! diff <(echo ${b[*]}) file; then
  echo "different"
fi
于 2012-11-16T20:11:02.920 回答