linux - [: <filename>: unexpected error in shell programming

标签 linux shell unix

我创建了一个脚本,该脚本创建两个文件并在两个文件不同时反转它们的内容。我不明白如何解决此错误:

[: my-filename: unexpected error in shell programming

代码:

echo "Enter first filename :"
read file1
echo "Enter second filename:"
read file2
echo "Enter content of file 1 : "
gedit $file1
echo "Enter content of file 2 : "
gedit $file2

check=" " 

x=` cmp $file1 $file2 `
if [ $x -eq $check ]
then
echo "Both files are same"
rm $file2
echo "Redundant file removed!!!"
else
echo "They are different"
fi

tac $file1 | cat > temp1
rev temp1 | cat > temp11 
tac $file2 | cat > temp2
rev temp2 | cat > temp22
mv temp11 $file1
mv temp22 $file2

echo "Content of both the files reversed"

最佳答案

您可以直接在 if 语句中使用 cmp

if cmp -s $file1 $file2
then
  echo "Both files are same"
  rm $file2
  echo "Redundant file removed!!!"
else
  echo "They are different"
fi

关于linux - [: <filename>: unexpected error in shell programming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955445/

相关文章:

bash - 如何通过命令行正确启动 Gnome-Shell 扩展?

linux - Linux 中登录的唯一用户列表

c - 为什么从数据集的/dev 源成功读取 errno?

php - 为什么当我在 Ubuntu 中运行一个 .php 脚本时它只是回应脚本本身?

php - wkhtmltoimage 错误 - 无法执行二进制文件

c - 在内核空间上实现 timecounter_init (clocksource.h) 会出现无效的 ISA 状态错误

python - 根据按下的键值执行 shell 脚本

Linux - 无法删除存在的目录

javascript - 如何使用 ECMAScript 6 node.js v4.2.1

linux - 想要检查文件夹中 .dmp 文件当天占用的总空间