bash 比较bash中两个变量的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13437104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:58:57  来源:igfitidea点击:

compare content of two variables in bash

bashdiff

提问by Martin

I have a variable $dataand variable $filein a bash script:

我在 bash 脚本中有一个变量$data和变量$file

data=$(echo "$(printf '%s\n' "${array[@]/%/$'\n\n'}")")
file=$(<scriptfile_results)

Those variables will contain text. How to compare those two? One option is to use diff(1) utility like this:

这些变量将包含文本。怎么比较这两个?一种选择是使用diff(1) 实用程序,如下所示:

diff -u <(echo "$data") <(echo "$file")

Is this an correct and elegant way to compare content of two variables? In addition how is the <( )technique called? As I understand, for each <( )a temporary file(named pipe) is created..

这是比较两个变量内容的正确而优雅的方法吗?另外这个<( )技术怎么称呼?据我了解,为每个<( )临时文件(命名管道)创建..

回答by andrewdotn

Yes, diff <(echo "$foo") <(echo "$bar")is fine.

是的,diff <(echo "$foo") <(echo "$bar")很好。

By searching the bash manpagefor the characters <(, you can find that this is called “process substitution.”

通过在bash 联机帮助页中搜索字符<(,您会发现这称为“进程替换”。

You don't need to worry about the efficiency of creating a temporary file, because the temporary file is really just a pipe, not a file on disk. Try this:

不用担心创建临时文件的效率,因为临时文件实际上只是一个管道,而不是磁盘上的文件。尝试这个:

$ echo <(echo foo)
/dev/fd/63

This shows that the temporary file is really just the pipe “file descriptor 63.” Although it appears on the virtual /devfilesystem, the disk is never touched.

这表明临时文件实际上只是管道“文件描述符 63”。尽管它出现在虚拟/dev文件系统上,但从未接触过磁盘。

The actual efficiency issue that you might need to worry about here is the ‘process' part of “process substitution.” Bash forks another process to perform the echo foo. On some platforms, like Cygwin, this can be very slow if performed frequently. However, on most modern platforms, forking is pretty fast. I just tried doing 1000 process substitutions at once by running the script:

您在这里可能需要担心的实际效率问题是“流程替换”的“流程”部分。Bash fork 另一个进程来执行echo foo. 在某些平台上,如 Cygwin,如果频繁执行,这可能会非常慢。然而,在大多数现代平台上,forking 非常快。我只是尝试通过运行脚本一次执行 1000 个进程替换:

echo <(echo foo) <(echo foo) ... 997 repetitions ... <(echo foo)

It took 0.225s on my older Mac laptop, and 2.3 seconds in a Ubuntu virtual machine running on the same laptop. Dividing by the 1000 invocations, this shows that process substitutions takes less than 3 milliseconds—something totally dwarfed by the runtime of diff, and probably not anything you need to worry about!

在我较旧的 Mac 笔记本电脑上花费了 0.225 秒,在同一台笔记本电脑上运行的 Ubuntu 虚拟机上花费了 2.3 秒。除以 1000 次调用,这表明进程替换所需的时间不到 3 毫秒——这与 的运行时相比完全相形见绌diff,而且可能不需要您担心!

回答by William Pursell

test "$data" = "$file" && echo the variables are the same

If you wish to be verbose, you can also do:

如果你想变得冗长,你也可以这样做:

if test "$data" = "$file"; then
  : variables are the same
else
  : variables are different
fi

回答by determinacy

This works best for me:

这对我最有效:

var1="cat dog mule pig"
var2="cat dog ant"

diff <( echo "$var1" ) <( echo "$var2" )

First I set var1 and var2. Then I diff with <( ) elements to say that the input is a variable.

首先我设置 var1 和 var2。然后我用 <( ) 元素进行区分,说输入是一个变量。

回答by deadElk

~ cat test.sh   

#!/usr/bin/env bash

array1=(cat dog mule pig)
array2=(cat dog ant)

diff -ia --suppress-common-lines <( printf "%s\n" "${array1[@]}" ) <( printf "%s\n" "${array2[@]}" )

回答by imriss

Using comm:

使用comm

comm -23 <(echo $variableA | tr ' ' '\n' | sort) <(echo $variableB | tr ' ' '\n' | sort)