Monday, December 28, 2020

How to Use a Bash Script File

 A Bash script file is simply a file containing a list of commands as one might enter from a terminal command prompt, with the addition of some more programming options like If . . . then, for, and while statements. It can be made to do most any function on a Linux, Apple, or other Unix based operating systems. They can greatly simplify and automate certain tasks.

For example, on a Debian based system, to preform an upgrade from the terminal requires entering at least two lines of text-based commands into a terminal. First, one must do an update to update the repositories to ensure one has all the most recent changes to them. Then you accomplish the upgrade, so the terminal commands would look like this:

sudo apt update
sudo apt upgrade

To create a bash file, you open your favorite text editor and give it an appropriate name, like in this case we might name this file "aptup.sh"

Note, the "sh" extension is commonly used for bash script files, but unlike MSWindows, that doesn't make it executable for Linux. You could put any extension on there you want, or none at all.

Now, enter the following to create the bash file:

#!/bin/bash

# This is a script file to make updates simpler!

sudo apt-get update
sudo -y apt-get upgrade

exit

The first line is the same for every bash file. It essentially tells the system what program and where it is at that will run this file.

The second line is optional. It is a comment. Every line that begins with a # will be ignored by the bash program, except the very first line, of course. But it is a good practice to put a general purpose statement at the beginning of your file, if for no other reason, if you forget what this file does, it serves as a good reminder for you, and provides any introductory information for others who might look at your source code. You can put as many or as few comments in a file as you want, put them anywhere you desire, or none at all. As long as a line has a # before it, it will not be run by the system. As a matter of fact, putting a # before lines of code is a good way to not have them run without deleting them entirely.

On the update and upgrade commands, you'll notice two changes I've made from the initial "apt" commands above. First, I've used "apt-get" instead of "apt". Apt is designed to be run interactively by the user, where as apt-get has provisions for running inside a script.

The second change is adding in the -y option. This option allows the script to become more automated by assuming a "yes" answer to any yes/no questions it asks. Typically, an upgrade script will list out the files it intends to upgrade, then ask you if you want to proceed with the upgrade.

The last line is optional at the end of the file, but it is good practice to put it in. It tells the bash script to stop processing and exit the file. It becomes more necessary if you want to stop processing earlier in the script, like during an if . . . then statement.

Once you have saved the file, there are a couple more steps to turn this into an executable file.

One, you should put this file into a directory that is in your path statement. To discover what is in your PATH statement, enter the following into a terminal window:

echo $PATH

Yes, it needs to be in all caps. Variables in Linux are case-sensitive.

What this does is it allows you to run the program no matter where you are in your directory structure. Otherwise, you would have to enter the full path every time you ran the script: /usr/bin/aptup.sh instead of just aptup.sh

The logical place to place this file is in your /usr/bin directory. That will make it available to all users of the computer and is where the bulk of user based programs reside. Or you can put it in a home directory and add that directory to your PATH environment variable by doing the following steps:

I usually create a bin directory in my home folder. You can do this using your file manager, but I'll show you here how to accomplish it from the command line. This is where I put all my scripts.

Bonus Info: In Solus, you don't have to add the user home directory bin to the path statement. If you create the directory, it automatically picks it up and adds it for you!

To create the bin directory ensure you are in your home directory by entering:

cd ~

Then enter (unless you already have one):

mkdir ./bin

Then to enter it into you path variable, enter:

export PATH=$PATH:$HOME/bin

If you run it from a terminal, it will only be good for the life of that terminal session. To have it come up every time a new terminal session is started, enter that command in your .bashrc or .profile file, whichever your system uses to initialize a terminal session. For Debian based systems, that will generally be .bashrc

Two, you'll want to make your file executable. Like the above, you don't have to do this, as long as you wish to preface every command with "bash" and the full path to where the file is, even if it is in a path where your system looks for executable program files. It is an easy command to do. Enter:

chmod +x ~/bin/aptup.sh

To accomplish the same in most file managers. right-click on the file and select "properties" from the menu. Go to the "permissions" tab and make sure to check off the box "make file executable" or something similar.

Now you should be ready to use your script file by simply typing in the name: aptup.sh! And your system will get updated by one command instead of two.

Wednesday, December 23, 2020

How to Install Parrot OS

 I have installed Parrot OS, but despite the Calamaris installer, it ended up not being a simple event. I attempted to install the MATE security edition on a virtual Gnome Box machine. The installation went fine until when it was about 93% of the way done, it stopped and sat on the following command: /usr/sbin/sources-media -u. It sat on it until the 600 seconds was up. I tried installing it with different settings, and still it would hand up at that spot, only to force me to close the installation and failed to install the operating system.

While on the Live installation of the system, I decided to take a look at the file. I discovered that it was a script file, and that the -u switch caused it to go into an update. The switch activates the following commands:

if [ "$1" = "-u" ]; then
    umount $CHROOT/$MEDIUM_PATH
    rm $CHROOT/etc/apt/sources.list.d/debian-live-media.list || true
    chroot $CHROOT apt-get update
    exit 0
fi

The fourth line updates the new system, which can be done equally well either before or after you install the system. However, the reason it was hanging is because as of today, it required almost 1700 updates, which alone would go beyond the 600 seconds it allows it to finish. However, it also ask several questions and requires user interaction to finish. Which means that was why it was hanging up. With no movement as it sat there and waited for my input in vain since the system didn't provide a way for me to do that, naturally it would time out. And since there wasn't any provision made for that in the install process, it ends the installation with only 7% more to go. Also, due to the big amounts of updates, you can't update it before you start the install because there isn't enough room on the "USB", virtual though it may be, to do it.

So with the only option left to update after the install, I had to disable the update in order to finish the installation. So I commented out the update command line in the file by putting a "#" at the beginning of that line so that it now showed:

#    chroot $CHROOT apt-get update

That allowed it to bypass the update and finish the install. Then I was able to do the giant update manually. Here are the commands to do this.

To edit the file, use your favorite text editor to comment out the line as described above. My favorite is nano from the command line BEFORE you start the install process from the live boot up.

sudo nano /usr/sbin/sources-media

Once you boot up into your new system, you can start the upgrade process using the following commands from the terminal, which I recommend you use instead of the GUI interface to update this many files.

sudo apt update
sudo apt upgrade

Then check on it every once in a while because it will wait for your interaction at points. (Note: when you get to the section right after it downloads all the files and displays the change log to you with a ":" waiting for your input, you can just press enter until it tells you you can press "q" to exit it.)

Then enter the following command to remove unneeded files:

sudo apt autoremove

Once you've upgraded all the files, you're good to go!