Help with batch file menu

bob ruzzo

New Member
Joined
Dec 9, 2020
Messages
4
Reaction score
1
Credits
38
New here.....I created about 7 .sh "batch" files to listen to audio streams via the Terminal. However I don't know where the actual files are located. But they work anyway, by simply typing "./<URL> and the audio quickly comes on in the terminal with much text flying by. What I would like to do is make some type of a menu/file where I can access whatever station I want to listen to in terminal, using just a simple letter or number associated with the batch file to run that stream. For example if I want to listen to NPR I type "./npr.sh" but I would like to put all 7 of these batch files into a menu to choose from....sorta like the old DOS menus. I don't know if this is really worth the bother or not.
 


New here.....I created about 7 .sh "batch" files to listen to audio streams via the Terminal. However I don't know where the actual files are located. But they work anyway, by simply typing "./<URL> and the audio quickly comes on in the terminal with much text flying by. What I would like to do is make some type of a menu/file where I can access whatever station I want to listen to in terminal, using just a simple letter or number associated with the batch file to run that stream. For example if I want to listen to NPR I type "./npr.sh" but I would like to put all 7 of these batch files into a menu to choose from....sorta like the old DOS menus. I don't know if this is really worth the bother or not.
Can you post at least one of the scripts? Just to see what you got in there, so we can provide some advice, and well, I'm curious too :). However, I think you can try using "positional parameters" inside the script, i.e.
Code:
#!/bin/bash
your_code_here $1
Where "$1" is the parameter passed to the command, i.e. radio station name, so you can
Code:
$ ./npr.sh some_radio_station
Then it takes that as an input to play that particular station. Note that if you have some other options inside the script then "$1" should become another number since that option is already "$1". BTW, VLC can do this too from the command line.
Code:
vlc https://some_url/some_radio_station
I use that as an alarm, so every morning I'm being woken up with the news lol


Hope this helps! :)
 
OK all the scripts are the same with exception of the URL:

#!/bin/bash
mplayer http://us4.internet-radio.com:8266
chmod +rx jazz.sh

One other thing. If I get an "Access denied" what is the chmod code to change permissions? I used to know much of this stuff back around 2004 when I first go into Linux.....but don't really tweak anymore and forgot most of what I remember.
 
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.
Bash:
mkdir ~/bin

3. Move all of your personal scripts into the personal bin directory:
Bash:
mv *.sh ~/bin/
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:
Bash:
cd ~/bin

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.
Bash:
#!/usr/bin/env bash

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 cding 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:
Bash:
. ~/.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:
Bash:
#!/usr/bin/env python3
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!
 
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.
It is possible to create a functions.sh file with bash functions inside the ~/bin directory too, something like;
Code:
#!/bin/bash
#my functions
#function 1 to play radio stations in my terminal
npr() {
your code here for whatever npr does
}

#function 2 to do something else
funct2_name(){
code here
}
#function 3 to do more stuff I need
funct3_name(){
code here
}
then in ~/.profile or ~/.bashrc type
Code:
source $HOME/bin/functions.sh
reload .bashrc
Code:
. .bashrc
or .profile
Code:
. .profile
this will have the same effect as @JasKinasis says; you'll be able to run your programs/functions from the terminal, i.e. npr
Also, you can add as many functions as you want into that file, just remember to reload .bashrc or .profile every single time after.
 
Last edited:
It is possible to create a functions.sh file with bash functions inside the ~/bin directory too, something like;
Code:
#!/bin/bash
#my functions
#function 1 to play radio stations in my terminal
npr() {
your code here for whatever npr does
}

#function 2 to do something else
funct2_name(){
code here
}
#function 3 to do more stuff I need
funct3_name(){
code here
}
then in ~/.profile or ~/.bashrc type
Code:
source $HOME/bin/functions.sh
reload .bashrc
Code:
. .bashrc
this will have the same effect as @JasKinasis says; you'll be able to run your programs/functions from the terminal, i.e. npr
Also, you can add as many functions as you want into that file, just remember to reload .bashrc every single time after.
That approach works too, but once you have a personal bin set up, there’s no need to reload your .bashrc when you add something new. Just add new scripts to your bin directory and run them!

So it’s swings and roundabouts.... With Unix/Linux, there’s always more than one way to figuratively skin the proverbial cat!
 
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.
Bash:
mkdir ~/bin

3. Move all of your personal scripts into the personal bin directory:
Bash:
mv *.sh ~/bin/
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:
Bash:
cd ~/bin

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.
Bash:
#!/usr/bin/env bash

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 cding 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:
Bash:
. ~/.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:
Bash:
#!/usr/bin/env python3
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!

OK I will try this. All my bash scripts are located in /Home and they are not in any other folder....they are in /Home along with the other folders normally in /Home. I think if I can run the scripts with just one simple name with no extension from the Terminal, thats good enuff for me. A menu wouldnt really be needed. I have all the scripts to look at so I can try this.
 
I tried but failed....when I type in for example npr at terminal I get: bash: npr: command not found
 
I tried but failed....when I type in for example npr at terminal I get: bash: npr: command not found

And you definitely followed ALL of the steps?!

If you definitely followed all of the steps and it didn't work - try logging out of your desktop and then log back in again. Then open a terminal and see if your scripts work.

I wrote the tutorial completely from memory, based on how I set my environment up. Perhaps I was wrong about .profile being loaded when you start a new terminal. Perhaps it's loaded when you log in?

So definitely try logging out, then log back in again and see if it works.
If it works after logging back in again, I'll update the previous post to correct the final steps.

And if it doesn't work, we'll try to get to the bottom of things and fix it!
 
Last edited:
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.
Bash:
mkdir ~/bin

3. Move all of your personal scripts into the personal bin directory:
Bash:
mv *.sh ~/bin/
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:
Bash:
cd ~/bin

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.
Bash:
#!/usr/bin/env bash

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 cding 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:
Bash:
. ~/.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:
Bash:
#!/usr/bin/env python3
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!
Thanks a bunch Jas. I will study this carefully.
OG TC
 
I tried but failed....when I type in for example npr at terminal I get: bash: npr: command not found
If using .profile file fails, try with .bashrc
edit the file
Code:
nano .bashrc
add these lines
Code:
export PATH="$HOME/bin:$PATH"
Once added, press Ctrl+o to save, press enter to confirm and the press Ctrl+x followed by enter to close nano and reload .bashrc
Code:
. .bashrc
Type that in the terminal and press enter. Then try running your commands.
 
OK all the scripts are the same with exception of the URL:

#!/bin/bash
mplayer http://us4.internet-radio.com:8266
chmod +rx jazz.sh

One other thing. If I get an "Access denied" what is the chmod code to change permissions? I used to know much of this stuff back around 2004 when I first go into Linux.....but don't really tweak anymore and forgot most of what I remember.
Then you can
Code:
#!/bin/bash
mplayer $1
make it executable
Code:
$ chmod +x jazz.sh
then you can run it like
Code:
$ ./jazz.sh http://us4.internet-radio.com:8266/
I tried here with VLC and it works. By the way, I think creating some aliases might be better so you don't have to type the URL every time:
1. Create .bash_aliases file if it doesn't exist
Code:
nano .bash_aliases
2. Add some aliases
Code:
alias jazz="mplayer -nocache -afm ffmpeg http://us4.internet-radio.com:8266/"
alias pop="mplayer -nocache -afm ffmpeg http://some_pop_radio/"
alias rock="mplayer -nocache -afm ffmpeg http://some_rock_radio/"
alias hip-hop="mplayer -nocache -afm ffmpeg http://some_hip-hop_radio/"
3. Edit .bashrc and add these lines
Code:
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
and reload .bashrc
Code:
$ . .bashrc
Type that and press Enter.
4. Load .bash_aliases file
Code:
$ . .bash_aliases
Type that in the terminal and press Enter. Now you can type jazz pop rock hip-hop or whatever genre you like and listen to the desired station. You can create as many aliases as you want/need for any other stations.
NOTE: I add those options because they process the sound but you might omit them if you like.
 
Last edited:
If @bob ruzzo correctly followed all of my steps and it didn't work, I'm pretty confident that logging into a new desktop session will make it work. It works for me on Debian. It's how I set up my personal bin directory - albeit years ago. So I know 100% it definitely works!

I use exactly the same setup with cygwin on my windows PC at work too.

Hopefully Bob will get back to us and update us...........

From doing a little bit of reading - if the user has a ~/.bash_profile or a ~/.bash_login file - then .profile will NOT be loaded by bash.

In which case, the workaround would be to add:
Bash:
source "$HOME/.profile"
to the users ~/.bash_profile, or ~/.bash_login.
Or just put the PATH customisation that we put in ~/.profile in there instead.

Basically, with bash login shells, bash looks for the configs in this order:
1. ~/.bash_profile
2. ~/.bash_login
3. ~/.profile
The reason I chose to put the customisation in .profile rather than having a .bash_profile is because .profile can be read/used by other login shells too.

From a quick search - it seems I've documented most of these steps before:
Check out my response in this old thread:

The OP in that thread asked how to install their C program and I gave a comprehensive response. Giving a number of different options, including adding an install target to their makefile that will copy files to various locations, to install it system-wide, to adding a personal bin directory, allowing them to run their program, but keep it private.

The instructions for creating the personal bin directory in that thread includes an additional step that will allow you to run your scripts from any text based application launchers too.

AND on top of that, I've even included how to add .desktop files to ~/.local/share/applications, so you can run your personal scripts from your main desktops main application menu - as if they were installed applications.

At some point, I might try to consolidate the information here AND the information in the other thread into a tutorial. Or perhaps create a small series of tutorials.

In my opinion, adding a personal bin directory is a MUST for anybody who regularly creates and uses their own scripts, or programs. It allows your personal programs to be treated as if they were any other application, without having to install them system-wide for other users.
Likewise - by adding .desktop files for your scripts/programs to ~/.local/share/applications - you can even access your personal programs via your desktops graphical application menus, without having to install them system-wide.

I don't use a traditional desktop, so I don't tend to use the .desktop option much. But I use my personal bin directory all of the time. I have all kinds of handy things in there.
 


Top