linux 带有多个二进制文件的 cat 命令

linux cat command with multiple binary files

我正在尝试熟悉 Linux 中命令 shell 上的 cat 命令。 运行 以下几行产生了一个意想不到的结果(至少对我来说是这样):

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

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

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

谢谢。

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

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

如果你想执行三次,你可能需要:

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() 函数?我不知道该怎么做,所以我做没什么。

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