SupplantACommandInLinux

From Super Grub Disk Wiki
Jump to navigationJump to search

Introduction

A friend of mine wants to compile a free software application that in one point of its executions runs another command. He would like to add an option this called command so that it makes its work better even. But I told him you do not need to recompile it. Just supplant it.

The caller program is something like Avatta Audio (have to check) and the called program is mpg123.

Steps

Locate called program.

bash-3.00$ whereis mpg321
ls: /usr/bin/mpg321

Rename called program to other name.

mv /usr/bin/mpg321 /usr/bin/original_mpg321

Create script that supplants the program.

gedit /usr/bin/mpg321

Contents:

#!/bin/bash
ORIGINAL_PROGRAM="/usr/bin/original_mpg321"
NEW_OPTIONS="--gain 5"

# Let's run our program with our options added and exit with its exit status:
exit `"$ORIGINAL_PROGRAM" "$@" "$NEW_OPTIONS"`

Give permissions to the script.

 chmod +x /usr/bin/mpg321

Test it.

Just use the program as normal but try to see if you see any differences.

P.S.: I have not tested the script yet!


Not the same program!

Do you want to just run another program instead of the hard-coded default one? Just modify ORIGINAL_PROGRAM variable in the script above as you like it.

Improving options

If you are smart enough you can parse the original options in $@ to eliminate the ones you do not want to use and keep the ones that you want to use or just modify them with sed so that the options are fit for another program called (instead of the original program).