In Arch Linux, if I do ls -l
in /sbin
, I can see that reboot
, shutdown
and poweroff
are all symlinks to /usr/bin/systemctl
. But issuing reboot
, shutdown
and systemctl
commands obviously does not all have the same behaviour.
Is ls -l
not showing me full information regarding symlinks? How can I, for example, know what the real symlink of reboot
is?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Many programs make use of this technique where there is a single executable that changes its behavior based on how it was executed.
There’s typically a structure inside the program called a case/switch statement that determines the name the executable was called with and then will call the appropriate functionality for that executable name. That name is usually the first argument the program receives. For example, in C
when you write:
int main(int argc, char** argv)
argv[0]
contains the name of the called executable. At least, this is the standard behaviour for all shells, and all executables that use arguments should be aware of it.
Example in Perl
Here’s a contrived example I put together in Perl which shows the technique as well.
Here’s the actual script, call it mycmd.pl
:
#!/usr/bin/perl use feature ':5.10'; (my $arg = $0) =~ s#./##; my $msg = "I was called as: "; given ($arg) { $msg .= $arg when 'ls'; $msg .= $arg when 'find'; $msg .= $arg when 'pwd'; default { $msg = "Error: I don't know who I am 8-)"; } } say $msg; exit 0;
Here’s the file system setup:
$ ls -l total 4 lrwxrwxrwx 1 saml saml 8 May 24 20:49 find -> mycmd.pl lrwxrwxrwx 1 saml saml 8 May 24 20:34 ls -> mycmd.pl -rwxrwxr-x 1 saml saml 275 May 24 20:49 mycmd.pl lrwxrwxrwx 1 saml saml 8 May 24 20:49 pwd -> mycmd.pl
Now when I run my commands:
$ ./find I was called as: find $ ./ls I was called as: ls $ ./pwd I was called as: pwd $ ./mycmd.pl Error: I don't know who I am 8-)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0