比较来自两个不同目录的相同类型的文件:ZSHRC函数

发布于 2025-01-20 23:46:54 字数 1144 浏览 3 评论 0原文

我很久以前就发布了一个关于如何比较两个不同目录中的相同类型(扩展名)文件的问题:

将通配符添加到 Bash 函数的参数中

在 bash shell 下,一切正常,但现在,我处于zsh shell 和下面的函数不再起作用:

function diffm() {for file in "$1"/$2; do diff -q "$file" "$3"/"${file##*/}"; done ;}

我收到以下错误:

$ diffm dir1 *.f90 dir2
diff: camb.f90/bessels.f90: Not a directory

camb.f90bessels.f90 位于同一目录,这是一个非常奇怪的错误。

我想像这样使用它(例如在 bash shell 下):

$ diffm . *.f90 ../../dir2

有解决方法来解决这个问题吗?

在此更新

此函数在 bash shell 上的工作版本:

function diffm() {
  # First dir
  dir1="$1"
  # Second dir
  dir2="${@: -1}"
  # Add slash if needed
  [[ "$dir1" != */ ]] && dir1=$dir1"/"
  [[ "$dir2" != */ ]] && dir2=$dir2"/"
  # Wildcard filenames
  files=( "$dir1"${@:2:$#-2} )
  # Compare the files
  for file in "${files[@]}"
    do
      diff -q "$file" "$dir2""${file##*/}"
    done;
  }

我正在寻找与 zsh shell 等效的版本。

I have posted a long time ago a question about how to compare same type (extension) files from 2 different directories:

Issue with wildcards into arguments of a Bash function

Under bash shell, everything is working fine but now, I am under zsh shell and the function below doesn't work any more:

function diffm() {for file in "$1"/$2; do diff -q "$file" "$3"/"${file##*/}"; done ;}

I get the following error:

$ diffm dir1 *.f90 dir2
diff: camb.f90/bessels.f90: Not a directory

camb.f90 and bessels.f90 are in the same directory, it is a very strange error.

I would like to use it like this (like under bash shell):

$ diffm . *.f90 ../../dir2

Is there a workaround to fix this?

Update

Here a working version on bash shell of this function:

function diffm() {
  # First dir
  dir1="$1"
  # Second dir
  dir2="${@: -1}"
  # Add slash if needed
  [[ "$dir1" != */ ]] && dir1=$dir1"/"
  [[ "$dir2" != */ ]] && dir2=$dir2"/"
  # Wildcard filenames
  files=( "$dir1"${@:2:$#-2} )
  # Compare the files
  for file in "${files[@]}"
    do
      diff -q "$file" "$dir2""${file##*/}"
    done;
  }

I am looking for the equivalent for zsh shell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2025-01-27 23:46:54

您应该重组参数的顺序:

function diffm() {
    local dir2="$1" dir1="$2"; shift 2
    for file; do 
        diff -q "$dir1/$file" "$dir2/$file"
    done 
}

diffm ../../dir2 . *.f90

ZSH版本:

#!/usr/bin/env zsh

function diffm() {
    setopt nullglob
    local dir1="$1" files="$2" dir2="$3" file1 file2
    for file1 in "$dir1"/${~files}; do
        file2="${file1##*/}"
        diff -q "$file1" "$dir2/$file2"
    done
}

diffm dir1 "*.f90" dir2

You should restructure the order of arguments :

function diffm() {
    local dir2="$1" dir1="$2"; shift 2
    for file; do 
        diff -q "$dir1/$file" "$dir2/$file"
    done 
}

diffm ../../dir2 . *.f90

zsh version :

#!/usr/bin/env zsh

function diffm() {
    setopt nullglob
    local dir1="$1" files="$2" dir2="$3" file1 file2
    for file1 in "$dir1"/${~files}; do
        file2="${file1##*/}"
        diff -q "$file1" "$dir2/$file2"
    done
}

diffm dir1 "*.f90" dir2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文