【问题标题】:Issues trying to execute bash script on windows using cygwin尝试使用 cygwin 在 Windows 上执行 bash 脚本的问题
【发布时间】:2018-05-04 06:25:23
【问题描述】:

我正在尝试使用 cygwin 在 Windows 7 上运行 bash 脚本。该脚本采用两个文件目标列表(文件相同,散布在不同的文件夹对中),遍历它们并检测文件是否更改。

#!/bin/bash
src=(
  "./src/index.js"
  "./src/index_2.js"
)
dest=(
  "./client/src/index.js"
  "./client/src/index_2.js"
)

arraylength=${#src[@]};

for (( i=0; i<${arraylength}; i++ )); 
do
  DIFF=$(diff -u ${src[$i]} ${dest[$i]})
  if [ $? != 0 ]; then
    echo "$DIFF"
    echo "Files ${src[$i]} and ${dest[$i]} are not equal!"
    exit 1
  fi
done
echo "All files are equal"

当我运行./shareddiff.sh 之类的命令时,该命令执行没有错误,但什么也不显示(没有回显消息)。即使我手动更改 index.jsindex_2.js 文件之一 - 它也不会检测到更改。

知道我做错了什么吗?

【问题讨论】:

    标签: cygwin


    【解决方案1】:

    您在传递文件参数时滥用了差异;您可以比较两个文件或两个目录,而不是两个文件列表。

    SYNOPSIS
           diff [OPTION]... FILES
    
     FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'.  If
           --from-file or  --to-file  is  given,  there  are  no  restrictions  on
           FILE(s).   If  a FILE is '-', read standard input.  Exit status is 0 if
           inputs are the same, 1 if different, 2 if trouble.
    

    试试

     diff -uR src client/src
    

    【讨论】:

    • 这部分代码DIFF=$(diff -u ${src[$i]} ${dest[$i]}) 和它下面的所有代码不应该工作吗?即使我用一个简单的回显替换整个脚本,比如echo "Hello World",我也看不到任何输出。
    • 如果回显不起作用不是差异问题。你用的是什么终端?
    • 我正在使用 Cygwin。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2013-01-13
    • 1970-01-01
    • 2022-10-20
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多