ShellScript Menu


As a newb, I forced myself to type commands manually until I learned them. These days typing long strings can be arduous, so I have developed a menu system which includes system information. Each menu item (1, 2, 3, etc) is simply an executable file in the /sc directory..

Firstly make the directory and set permissions..

mkdir /sc
chmod 755 /sc

Advantages of putting all your custom scripts in one place include easy backup and short paths. Also putting the directory in the system path means your scripts can be called from anywhere..

I do all my work as root 'cos I hate typing sudo and I've never had a problem with it so there. Modify the BASH login for root to include the new scripts directory using your editor of choice (I like vi)....

vi /root/.bashrc


PS1="[\u@\H] $ "

alias l="ls -laG; pwd;"

PATH=`echo $PATH:/sc`  
Scrollable on small screens..box

So in the above example I've modified the .bashrc for root. I've modified the command prompt and added an alias. The nifty bit is to echo the system path and add the /sc and then redefine this as the new system path for the root login. Save and close.

A couple of caveats. First log-in using an additional, separate shell window and su to root user to make sure it works. That way if it stuffs up you can still fix things using the original root login. Secondly don't try modifying the system path for the whole system unless you really know what you're doing: you can stuff things up big-time and it's not necessary.

If you're not happy doing this - and there are sound reasons to use sudo instead, then add the lines to the end of the pi user's BASH login script instead..

vi /home/pi/.bashrc

S'probably for the best..

Making the Menu

So the stage is set and we're gonna chuck all our custom scripts in the /sc directory where they will be invokable from anywhere. You could make lots of little aliases, but they're really only OK for one-liners. So the first script will be the menu which will be invoked by typing mm:

vi /sc/mm

Copy and paste this wad of code into the file and save and close..


Scrollable on small screens..box

Finally make the script executable..

chmod 755 /sc/mm

To make the menu easily updatable create a second script and make it executable..

vi /sc/mme


clear
sudo vi /sc/mm
/sc/mm 
Scrollable on small screens..box

Heh, see what I did there!? I edited a script which invokes a script editor to edit a script. Use sudo before all your commands in the script if you're gonna be running them as a normal user.

chmod 755 /sc/mme

When you run it you'll appreciate the ingenuity..and hey - this is quite fun - we're having fun doing this..!

Making the Scripts

So now all you gotta do is make some scripts - literally numbers - inside /sc which correspond with what the menu says. Most will be only one or two lines of code..

Taking my example script screenshot from above..

vi /sc/1

sudo vi /etc/dhcp.conf

Save & exit

chmod 755 /sc/1

Then..

vi /sc/2

cat /var/db/dhcpd.leases

Save & exit

chmod 755 /sc/2

And..

Then..

vi /sc/5

/usr/sbin/dhcpd restart

Save & exit

chmod 755 /sc/5

ALWAYS use full paths to all files, including binaries (executables). They may work without, but once you start scheduling your scripts through cron and stuff like that they may not work properly. It's a good habit to get into and helps you understand how things work..

AndyM | Updated July 2019