-2

Why do the following lines print only once the files in the current directory (and not three times)?

This is a question from an exam. If you know some source to read about it, it will be great

cp /bin/ls blah
cat blah blah blah >bbb
chmod u+x bbb
bbb
Mat
  • 202,337
  • 40
  • 393
  • 406
user1479376
  • 327
  • 1
  • 2
  • 10

2 Answers2

2

You would need something like:

cp /bin/ls/blah .
cat blah blah blah >bbb
chmod u+x bbb
./bbb

The first difference is the number of parameters to cp. You need to specify the destination as well as the source. The second difference is that unless you have the current directory in the path, you need to use ./command instead of just command to execute a program residing in the current directory.

Besides that, it would important to know what is the content of /bin/ls/blah. If that file is a bash script containing something like:

#!/bin/ls
ls

the modified block of code given at the beginning of this answer will work as expected (printing 3 times the contents of the current directory). If the file is a binary file, it most probably would not work, since you cannot concatenate binary files and expect the resulting file to behave as 3 sequential executions of the original binary file.

UPDATE: After your edit, the answer clearly changes. Now, only the last part applies to it. Since /bin/ls is a binary file, you cannot concatenate binary files and expect the resulting file to behave as 3 sequential executions of the original binary file. If instead of a binary file you were using a script the example would work, though.

betabandido
  • 18,946
  • 11
  • 62
  • 76
  • But this *still* wouldn't display all filenames 3 times. – Ignacio Vazquez-Abrams Jul 22 '12 at 16:58
  • @IgnacioVazquez-Abrams I know, I am modifying the answer to reflect that. Actually, it would depend on the contents of the file being copied. – betabandido Jul 22 '12 at 16:59
  • 1. Why /bin/ls is a binary file? 2. What do you mean here? "If instead of a binary file you were using a script the example would work" , script=code? 3. In second thoguth ,Why I dont get error when I try to execute a concatenate of binary files ? thanks(and sorry about my English..) – user1479376 Jul 22 '12 at 18:47
  • @user1479376 `/bin/ls` is a compiled C file. The resulting file is a [binary file](http://en.wikipedia.org/wiki/Binary_file). Simplifying it a bit, that means the file is not in a human-friendly format. A [script file](http://en.wikipedia.org/wiki/Scripting_language) contains some scripting code (in a human-friendly way). Script files are not typically compiled, but interpreted (that is the case of a shell script, which is typically parsed by bash). – betabandido Jul 22 '12 at 18:54
  • @user1479376 You do not get an error, since the binary file contains a header specifying all the necessary information for the program execution. When you concatenate the other two copies, they do not affect the first copy, which will just ignore the data after the first copy. – betabandido Jul 22 '12 at 18:55
0

ELF binaries have a definite size and structure, so there's no way to execute the other two copies of ls contained in the bbb binary.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358