I'd say don't bother with the menu - I'd recommend the following steps to set up a personal bin directory instead.
Seriously - bear with me! It
will be worth it!
Just make sure to read my whole post a couple of times before trying any of the steps - I want you to know exactly what you're getting into here. And this is a one time set up thing.
Once you have this set up - you can use it forever!
Any questions, or doubts - please feel free to fire away.
Effectively, what I describe below will create a personal bin directory for you to put your scripts into and execute them from anywhere in the terminal.
- We'll create the personal bin directory,
- We'll move the scripts into the bin directory
- We'll add shebangs to the first line of each script.
- Once the scripts have shebangs, they can be renamed to have no file extensions.
- Finally, we'll edit a config file in your home directory, to add your personal bin directory to
$PATH
- this will allow you to run your scripts as if they are native applications/first-class citizens! In other words - no more
./whatever.sh
. You'll be able to run your
npr.sh
script by simply typing
npr
into the terminal from ANYWHERE in the file-system.
Sound good?!............Thought so!
Note: I use
~
(tilde) a lot in this post.
~
is a bash shortcut for the path to your home directory..AKA
$HOME
, or
/home/yourusername
.
So the steps:
1. Open a terminal - pretty easy so far... To be fair, it doesn't get too much harder!
2. Add a personal bin directory in your home directory.
3. Move all of your personal scripts into the personal bin directory:
NOTE: If your scripts are NOT in your home directory and they're in a subdirectory - make sure to
cd
into the correct directory BEFORE running the above command to move your .sh files.
4. cd into your personal bin directory:
5. Fire up your favourite text editor and edit each of your scripts to ensure they have a shebang as the very first line -
e.g.
NOTE: The shebang MUST be the very first line in the file - even before any comments.
The shebang is read by the shell and tells the shell which interpreter to load in order to run your script.
And once all of the files have shebangs in them - they no longer need to have file extensions.
6. Rename your shell-scripts to remove the .sh extension: (All will become clear shortly)
e.g.
Bash:
for file in *.sh ; do mv $file ${file%.*};done
7. Use your favourite text editor to edit the hidden config file
~/.profile
and add the following lines:
Bash:
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:${PATH}"
fi
Notes: If
~/.profile
does not exist - simply create it using your favourite text editor.
If the file already exists and already contains the above lines - winner winner, chicken dinner - your distro is already set up for users with personal bin directories - all you need to do is skip step 7 and continue reading.......
And if the lines are already in the file, but they are commented out - then simply uncomment them and save the file.
Whatever the case - at the end of step 7 - as long as you have a file called
.profile
in your home directory and it at least contains the three lines of code in the example above, then you are golden!!
BTW:
What the above code does is - it checks for the existence of a bin directory in the users home directory.
If a bin directory exists in the users home directory - the path to the bin directory is added to
$PATH
.
With this change made - any time you open a new terminal window your ~/.profile is read, your bin directory is added to
$PATH
and you will be able to run
any scripts that are in your personal bin directory as if they were native commands.
At least - as long as they have their executable permissions set!
In other words, you can wave goodbye to
cd
ing into your script directory and running
./npr.sh
. And say "HELLO!" to using
npr
instead.
And because your personal bin directory is in
$PATH
- it no longer matters which directory you're in when you run your script. You can run
npr
from anywhere in the file-system!
If the terminal you used to perform all of the above steps is still open, the change to
$PATH
will not yet be applied. You can either close the terminal and start a new one, or if you want to continue using the open terminal - there is one final step to perform.
And this step should also be used on any other, older terminal sessions you might have running, if you want to use your personal scripts in those too.
Final step - in each older terminal window:
8. Reload your .profile:
Run the above command in any older terminal sessions and that will reload your .profile and
$PATH
will be updated, allowing you to run your
npr
script (or any of your other scripts in your personal bin directory) from those older terminal sessions!
Final notes:
Adding a shebang works for
any other types of scripts too.
If you have a python3 script, you would use the following shebang:
Or if you have a ruby script, or a lua script, or a perl script, or whatever - put a shebang at the start of the script, in the very first line, specifying the path to the interpreter.
If your script contains a shebang, you can safely remove any .py, .rb, .lua, .pl, or .whatever extensions and run your script as if it was a native command/program.
That's gotta be better than any menu?! Am I right?!
However - if you can't remember all of the radio stations that you've set up scripts for and still would like a menu - we could help you to write another script in your shiny, new, personal bin directory that will allow you to select which of the other scripts to fire up!