Jump to content

Modify a linux command

Hi guys,

 

is anyone aware of a method to modify a linux command to change its functionality (like adding a feature) ?

 

Have a nice day !

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, DarkRuskov said:

is anyone aware of a method to modify a linux command to change its functionality (like adding a feature) ?

You download the source-code, add your changes, then recompile it.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, WereCatf said:

You download the source-code, add your changes, then recompile it.

Ok thanks, and where would one find source code for basic commands such as ls or poweroff ?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, DarkRuskov said:

Ok thanks, and where would one find source code for basic commands such as ls or poweroff ?

Find out the package it belongs to and then search for that package's source code. Basic things like ls are part of the GNU coreutils package, for example: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src

There you can find, for example, ls: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, DarkRuskov said:

Ok thanks, and where would one find source code for basic commands such as ls or poweroff ?

If you're on a Debian-based OS, you could e.g. find which package the file belongs to:

dpkg -S /bin/ls

Which will reply that it belongs to coreutils. Then you can use apt-cache show coreutils to find out the package's homepage and source-code. Alternatively, you can use apt to download the package's source-code like e.g. here: https://askubuntu.com/questions/28372/how-do-i-get-and-modify-the-source-code-of-packages-installed-through-apt-get

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

52 minutes ago, DarkRuskov said:

Hi guys,

 

is anyone aware of a method to modify a linux command to change its functionality (like adding a feature) ?

 

Have a nice day !

What exactly do you want to change? Depending on that there are simpler ways of changing basic functionality... for instance you could alias "ls" to your own script or parameters or to an alternative program like exa.

 

Changing and recompiling the program isn't really advised because you lose access to updates for that program and for something as essential as ls it might break parts of your system.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Sauron said:

What exactly do you want to change? Depending on that there are simpler ways of changing basic functionality... for instance you could alias "ls" to your own script or parameters or to an alternative program like exa.

 

Changing and recompiling the program isn't really advised because you lose access to updates for that program and for something as essential as ls it might break parts of your system.

I have a raspberry pi and I want it to show a message on a nokia screen right before powering off to know when it's safe to unplug it. The problem is that showing this image before executing the command won't cut it since in the case where the poweroff command takes too much time the user could unplug it without it being safe. There is a method to show the PI's status with an LED and GPIO pins but it isn't acceptable in my case.

 

My thinking was to run the script once the bulk of the shutting down was done. If you think of a better way I'm all ears because I agree with you, mine is extremely janky...

This is necessary since the screen stays on/doesn't clear once the pi shuts down.

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, DarkRuskov said:

I have a raspberry pi and I want it to show a message on a nokia screen right before powering off to know when it's safe to unplug it. The problem is that showing this image before executing the command won't cut it since in the case where the poweroff command takes too much time the user could unplug it without it being safe. There is a method to show the PI's status with an LED and GPIO pins but it isn't acceptable in my case.

 

My thinking was to run the script once the bulk of the shutting down was done. If you think of a better way I'm all ears because I agree with you, mine is extremely janky...

This is necessary since the screen stays on/doesn't clear once the pi shuts down.

You could use a systemd service. The ExecStop option allows you to run a script when the service stops and if you start that service first it will be stopped last, meaning that that script will be the last thing the pi executes before shutdown. I'm assuming this is on raspbian of course - if you aren't using a systemd distribution then you'll need to look up how to do something similar with your init system.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

You could use a systemd service. The ExecStop option allows you to run a script when the service stops and if you start that service first it will be stopped last, meaning that that script will be the last thing the pi executes before shutdown. I'm assuming this is on raspbian of course - if you aren't using a systemd distribution then you'll need to look up how to do something similar with your init system.

I am indeed on raspbian. Thanks a lot, it seems much cleaner ! My question then is what's the best way to order how services start ?

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, DarkRuskov said:

I am indeed on raspbian. Thanks a lot, it seems much cleaner ! My question then is what's the best way to order how services start ?

I *think* it's enough to enable the service, since systemd has parallel initialization most services should be started together and stopped more or less together - the delay between when this service stops and the others do should be minimal anyway. A lot of larger services need to wait for other services to start so a custom service with no dependencies should be among the first to start. I'm not aware of a simple way of making it the absolute first to start without manually changing the dependencies for all other services to include it, which I wouldn't advise unless it's absolutely necessary. Since it does basically nothing I would expect it to start and stop in a matter of milliseconds, probably less time than your screen takes to update.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Sauron said:

I *think* it's enough to enable the service, since systemd has parallel initialization most services should be started together and stopped more or less together - the delay between when this service stops and the others do should be minimal anyway. A lot of larger services need to wait for other services to start so a custom service with no dependencies should be among the first to start. I'm not aware of a simple way of making it the absolute first to start without manually changing the dependencies for all other services to include it, which I wouldn't advise unless it's absolutely necessary. Since it does basically nothing I would expect it to start and stop in a matter of milliseconds, probably less time than your screen takes to update.

OK thank you very much, I'm going to test this, seems very promising !

Link to comment
Share on other sites

Link to post
Share on other sites

You can create a bash file with the same name as the program and export a path to it before all your other paths. That essentially will execute your bash script instead of the actual program. It wont add any new features though unless you are going to reinvent the wheels and implement everything it does and more in the script. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...
On 11/19/2019 at 5:28 PM, wasab said:

You can create a bash file with the same name as the program and export a path to it before all your other paths. That essentially will execute your bash script instead of the actual program. It wont add any new features though unless you are going to reinvent the wheels and implement everything it does and more in the script. 

I see, thanks ! But I think the other methods are more aligned with what I want to do :)

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/19/2019 at 5:28 PM, wasab said:

You can create a bash file with the same name as the program and export a path to it before all your other paths. That essentially will execute your bash script instead of the actual program. It wont add any new features though unless you are going to reinvent the wheels and implement everything it does and more in the script. 

This is a somewhat dangerous option because you're changing the behaviour of a linux command which might be used in other scripts you use.  Since you're changing the behaviour of the program, you're maybe breaking.  Luckily, most shells have an alias command.  If you do

 

alias ls="ls -C"

you'll get the different behaviour for interactive console use, but scripts will still use the default behaviour.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×