Friday, September 16, 2022

How to Use a Downloaded Bash File

I have a web page of mostly bash scripts located at: https://nlgg.rlcopple.com/index.php?currdir=&currfile=Download+Scripts.php

On that web page is a link to this web page with the promise of how to use those scripts, for those new to Linux or have never dealt with it before. So that is what you will find here, a post on how to use a bash script that you downloaded from my site. It would apply equally as well to other downloaded bash scripts.

Step 1 – Download the File

First, of course, you will need to download the desired script. The easiest way is to simply click on the link. But if it displays the text of the script instead of downloading the file, you can check the contents of the file that way, but saving it in a file on your computer would require you to copy the text, then paste it into a text editor, then save the file, giving it a name.

So, for most folk, the best way is to right-click (that means to hover your mouse over a target and then press down on the right mouse button instead of the left), then in the pop-up menu that should appear, you should select the option that says something similar to, “Save link as . . .” which will then, when clicked, will download the file to your hard drive. Make sure you remember where you put it and the name you gave it, if you changed it from the default.

Step 2 – Check the Contents of the File

This is the step most tend to skip. Why should you look the file over? 1) To identify any potential problems. Even if you feel you don’t have the chops to figure out the coding on a bash file, most of the commands are obvious, like “echo” means to copy the string of text to your terminal window. But there are other more practical reasons.

Frequently, a bash script file will have what are called comments in them. These are text preceded with the hash sign: #. Any comments in the file will tell you two basic things: 1) What the coding is meant to do, and 2) any instructions on how to use the script file. For instance, in my Install programs script, you will want to look over the list of programs it will install from repositories and flatpaks and adjust them to your liking.

Step 3 – Make the File Executable

The next step is to make the downloaded file executable. In Windows you generally accomplish that by including the correct file extension such as *.exe, *.msi, *.bat. Of course, the file must match the extension to work. But that isn’t the way to “mark” a file as executable in Linux.

Perhaps the easiest for new Linux users is to locate the file in your file manager, right-click the file to bring up the pop-up menu, Then select “Properties”, then the “Permissions” tab, then locate the text which says something to the effect of “make this file executable.” Click that box and click OK.

Or, if you want to use the command line in a terminal, you simply enter the following line and at the end of it, press Enter:

chmod +x [name of file]

Step 4 – Running the File

You can run a bash script file at this point. However, unless you saved it in a directory that is included in your PATH, you would run it by including the entire absolute path. For example, let’s assume we have a bash script file we’ll called “laughatme” and that we have already marked it as executable; and we have put the file in the bin directory under our home. There are three ways to run the file.

Method 1

You can run the file using an absolute path. So if we wanted to run the above mentioned bash file, I would enter the following command from any directory in a terminal window.

/home/$USER/bin/laughatme

In some distributions you may need to put “bash” before the line for it to work.

Method 2

If you are already in the directory where the bash file is at, or if you change to the directory where the file resides, you can enter the following shorthand:

./laughatme

Method 3

The easiest way is to put the directory as part of your path statement. Your path variable holds all the directories that your system will look for any program you tend to use from the command line. Each directory is deliminated by a colon - “:”.

To check which directories are in the $PATH variable, enter the following command in a terminal:

echo $PATH

Generally, you will see where most programs are put: /usr/bin being the most common. Other directories you may see are: /bin:/sbin:/usr/sbin. On occasion, some distributions will check if there is a bin directory in the user’s home directory, and add it into the path. If wherever you put the script at is not in one of the directories listed in the path, then you have two options:

1) You can move the bash script file to one of the directories listed.

If you use this method, it is best to put scripts like this into a home directory file. Those are any directories that begin with “/home”. If you do put it into a system directory like “/usr/bin”, then you will probably want to change the ownership of the file from your user name to “root.” Assuming the directory you are in contains the file, you would enter:

sudo chown root:root laughatme

Another common place to put scripts aside from $HOME/bin is $HOME/.local/bin

2) Add the directory it is in into the PATH variable.

In my example above, I would add the following command to the end of your .bashrc file in your home directory:

[[ -d $HOME/bin ]] && [[ $PATH != *”$HOME/bin”* ]] && export PATH=”$PATH:$HOME/bin”

That is a simplified IF/Then statement in bash. So if there exists a valid directory of “bin” in the users home directory, and the directory is not included in the PATH variable, then and only then will it add the path to the $PATH variable.

Once the path has been added to the $PATH variable, then you should be able to simply enter the name of the script file no matter what directory you are in (you may need to close out your terminal window and open a new one to activate the new path statement):

laughatme

That is it! You are now ready to use the script file, enjoy!

No comments:

Post a Comment