使用 cshell 在单个 for 循环中从一个目录中获取两个文件

take two files from a directory in a single for loop using cshell

我在一个目录中有两种类型的文件。

AB011.X AB012.X AB013.X

AB011.Y AB012.Y AB013.Y

如果他们的基本名称匹配,我想一次从每个组中挑选一个 我正在使用此代码:

    for i in *.X
    do
      a=${i%.*}
      for j in *.Y
      do
        b=${j%.*}
        if ["$b" == "$a"] then
          echo "$a, $b"
        endif
      done
    done

此代码出现以下错误:

    line 10: syntax error near unexpected token `done'
    line 10: `done'

希望有人能帮忙。

对于 bash(即 sh somecode.sh),工作 - 基于示例代码

3 处更改 - endif 变为 fi,添加 ;在 ] 之后,并在大括号内添加空格...

 for i in *.X
   do
     a=${i%.*}
     for j in *.Y
       do
         b=${j%.*}
         if [ "$b" == "$a" ]; then
           echo "same $a, $b"
         fi
       done
     done

对于 cshell:

如果使用 csh,那么希望这段 csh 脚本能让您走上正轨:它会打印出文件的 Y 版本对于任何给定的 X 版本是否存在。

我正在使用 csh

#> csh --version

 tcsh 6.18.01 (Astron) 2012-02-14 (x86_64-unknown-linux) options 
 wide,nls,dl,al,kan,sm,rh,color,filec

在 Centos 7 上。

我这样创建了文件:

#> touch AB011.X AB011.Y AB012.X AB012.Y AB013.X AB013.Y

和 运行 来自名为 test.csh 的文件的以下脚本使用 csh:

#> csh test.csh

'test.csh'的内容:

foreach v ( *.X )
    echo "$v"
    set a = "$v:r.Y"
    if ( -f $a ) then
        echo $a exist
    else
        echo $a does not exist
    endif
end

输出为:

AB011.X
AB011.Y exist
AB012.X
AB012.Y exist
AB013.X
AB013.Y exist

(我已经回答过了,但是有点太偏bash了。所以我编辑成csh)。