-1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Would you please elaborate what's not working? How about some error messages? – paladin Apr 13 '22 at 06:18
  • @paladin . I have added the error : it seems that command interprets like it was a directory. Regards –  Apr 13 '22 at 06:35
  • Are you sure that the `/` in `for file in "$1"/$2;` is correct? `for file in $1 $2;` gives a more logical answer. PS you should write variable names always in CAPITALS, for better readability. – paladin Apr 13 '22 at 10:05
  • @paladin It's generally considered best practice to only upper-case environment variables, not private shell variables. https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – Shawn Apr 13 '22 at 11:08
  • @paladin to respond to your question about the `/`, I make sure before to not put no slash in my command line, i.e I do a : `diffm . *.f90 ../../dir2` without slash on `dir1` and `dir2` –  Apr 13 '22 at 17:35

1 Answers1

0

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
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Thanks, this doesn't work under zsh shell. I provide in update a working version for bash. I am looking for the equivalent working under zsh. Regards –  Apr 17 '22 at 20:41
  • Your `zsh` version works fine, thanks ! –  Apr 17 '22 at 22:52