我正在尝试比较bash脚本中两个文件的内容.
local_file=$(cat my_local_file.txt)
remote_file=$(curl -s "http://example.com/remote-file.txt")
if [ local_file == remote_file ]; then
echo "Files are the same"
else
echo "Files are different. Here is the diff:"
diff <(echo "$local_file") <(echo "$remote_file")
fi
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,我发现我有一个语法错误:
./bin/check_files.sh: line 8: syntax error near unexpected token `('
./bin/check_files.sh: line 8: ` diff <(echo "$local_file") <(echo "$remote_file")'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何从bash脚本中显示这两个字符串的差异?
进程替换是一种bash功能,通常不/bin/sh支持与POSIX兼容.
如果要将脚本作为可执行文件运行,请确保使用以下shebang行:
#!/bin/bash
Run Code Online (Sandbox Code Playgroud)
代替
#!/bin/sh
Run Code Online (Sandbox Code Playgroud)
或使用
bash script.sh
Run Code Online (Sandbox Code Playgroud)
代替
sh script.sh
Run Code Online (Sandbox Code Playgroud)
如果你这样运行它
为了使脚本适用于POSIX符合shell,我只需下载该文件并将其与本地文件进行比较.差异后删除下载的文件.