8

I have a script from other person which has a look (note: it's a single file):

#!/bin/bash

some commands
some commands

#!/bin/bash

some commands
some commands

#!/bin/bash

some commands
some commands

I wondering what is the purpose of second and third shebangs? Is it by mistake or on purpose?

  • Are the extraneous #!/bin/bash preceeded by << construct (here document) as:some command <<end_of_script_flag? – dan Jun 01 '15 at 13:39
  • No any << constructs. Just plain shebangs and some remove commands (this was uninstall script) – Barat Sahdzijeu Jun 01 '15 at 19:47
  • It could be that the script was assembled from scripts meant to be run successively, or that the file was meant to be split into several independent scripts but the splitting failed. What is just before the extra #! lines? – Gilles 'SO- stop being evil' Jun 01 '15 at 22:17
  • @Gilles, I believe no extra hidden purposes of that script. It's a static script named Uninstall.command (platform: Mac OS X). – Barat Sahdzijeu Jun 02 '15 at 09:23

2 Answers2

10

If these lines are not the beginning of included shell scripts to be built, i.e. inside a scheme of the form:

cat <<end_of_shell_script >dynamically_built_shell
#!/bin/bash
[...]
end_of_shell_script

Then the repeated construct you found is the result of many copy - paste of full shell scripts but without enough care and understanding of what is the use of these very special comment on line 1 of scripts, starting with #!.

Be careful before using such a shell script (no sudo, no su :) ).

dan
  • 933
  • BTW, lots of sudo's there... Jeezzz... – Barat Sahdzijeu Jun 01 '15 at 11:54
  • /bin/rm (+ dummy stuff to feed the robot)! – dan Jun 01 '15 at 11:55
  • 3
    ... or combining multiple bash scripts similar to how it would be executed if you source-d them by name. I don't see the necessity for the brain off comment, though. It's perfectly admissible this way and it's the very reason why the shebang is a comment line. – 0xC0000022L Jun 01 '15 at 11:58
  • After a second look I noticed some other lines repeated, so I tend to think it's just careless copy-paste from multiple scripts. – Barat Sahdzijeu Jun 01 '15 at 12:32
4

No purpose, these are just comments. Unless 'some commands' contains some commands which save this into separate file for later execution.

gena2x
  • 2,397
  • No any writing operations and subsequent executions. This script is just a set of rm operations which do uninstall of some application. I thought that it might be related to forking new shell, but apparently it was dirty copy paste by script creator. – Barat Sahdzijeu Jun 01 '15 at 12:34
  • 1
    @BaratSahdzijeu BE CAREFUL WITH RANDOM SCRIPTS WITH rm IN THERE! Apologies for the caps, but I cannot stress how important that is. If the original author made the right mistakes (or rather, the wrong mistakes), it can damage other software on the machine and even break the OS. – Nzall Jun 01 '15 at 17:58
  • Nate, thanks! Actually I did not run that shell script. I used it as learning source for uninstaller on Mac OS X (Linux like OS) platform. Thought maybe it has some special flavor of bash which has some meaning is shebang repeated inside a script. – Barat Sahdzijeu Jun 01 '15 at 19:48
  • The #! at the same use at the kernel level (see man execve) on any Unix system (OpenBSD, FreeBSD, MacOS X, Kali-linux…). The bash does simply see this as a comment. – dan Jun 01 '15 at 21:18
  • @NateKerkhofs And what is with mv ln cp dd vi ... along with 200 other commands? Why aren't you stressing them too? They might feel left out. – ott-- Jun 01 '15 at 23:02
  • @ott-- Most other commands are reversible. mv can be repeated with reverse arguments. the link from ln can be undone. cp just copies. dd also is just a copy. vi is a bit riskier, but also can be reversed. rm cannot be reversed easily unless you got a recent backup. I do admit, though, that my experience with nix systems is limited, so I might be simplifying too much. I still stand by my point that rm is one of the most dangerous commands on a nix system. – Nzall Jun 02 '15 at 07:09
  • @Nate Kerkhofs: to avoid you deception. If b exists, then cp a b destroys b contents, the same stands for mv, dd. – dan Jun 28 '15 at 10:13
  • @danielAzuelos Show how much I think of secondary actions. Still, my main point was stressing that random scripts should be handled with proper care, considering they can quite easily break your machine. your comment just stresses even more how carelessness can do much damage. – Nzall Jun 29 '15 at 15:27