首页 > 解决方案 > 带有多个二进制文件的linux cat命令

问题描述

我正在尝试熟悉catLinux 命令 shell 上的命令。运行以下几行只会产生意想不到的(至少对我而言)结果:

cp /bin/ls file1
cat file1 file1 file1 > ls3
chmod u+x ls3
./ls3

我预计整个目录将被打印 3 次,但结果是我只打印了一次整个目录。这是什么原因?我以为linux中的二进制文件和文本文件没有区别,不知何故文件只写一次?

如果有人可以为这些基本命令和管道提供有用的资源/指南,我真的很喜欢它,因为基本命令从未像我刚才所做的那样涵盖任何内容。

谢谢。

标签: linuxshellbinaryfilescat

解决方案


让我给你一些背景知识为什么这可能会出错:在几种编程语言中,整个程序嵌入在一个main()函数中,所以ls可能看起来像:

main(){
  <show the listing of the current directory>
}

如果要执行此操作 3 次,您可能需要:

main(){
  <show the listing of the current directory>
  <show the listing of the current directory>
  <show the listing of the current directory>
}

但如果你只是简单地将所有东西粘在一起,你会得到:

main(){
  <show the listing of the current directory>
}
main(){
  <show the listing of the current directory>
}
main(){
  <show the listing of the current directory>
}

如果你尝试运行它,计算机会说“这是什么?不止一个main()功能?我不知道该做什么,所以我什么也不做。

因此,如您所见,粘合二进制文件以多次执行它们是一个坏主意,无论多么好。


推荐阅读