Bash scripts

21 March 2023 4 minutes Author: Endpool

Features of Bash and where it is used

One of the greatest advances in computer software development was the introduction of windows and menus, so what’s the point of going back? People use command-line interfaces like Bash because they still have several distinct advantages over GUIs. Let’s take a look at some of these benefits. Get access to the operating system more efficiently. People use Bash when they want to manage their computer or OS without navigating menus, options, and windows in a GUI. Bash is a scripting language that runs in a terminal on most Linux distributions as well as MacOS. Scripts are a sequence of bash commands in a file, strung together to perform more complex tasks than simple one-liners, and are particularly useful when it comes to automating sysadmin tasks such as backups. As we mentioned earlier, with command-line interfaces like Bash, you usually don’t even need to use a mouse. You can navigate your computer’s OS without taking your fingers off the keyboard.

For example, if you want to quickly create, edit, or delete multiple files, it’s easier to use Bash instead of finding each file by pointing and clicking multiple directories. Also, using Bash instead of the GUI is less resource intensive because your computer doesn’t need to allocate resources to render the graphical output. This makes Bash an attractive option when you’re already running multiple applications, a virtual machine, or otherwise have limited computing resources to work with.

Step 1. Create a directory and a file

  1. Create a .sh file

  2. Write the code to solve the necessary problem, save the changes in the file;

  3. Run the script for execution.

How to work with Bash scripts?

1. Create a folder called bash.

mkdir bash

2. Move to the created folder.

cd bash. Let’s check the existence of the file using the ls command:

3. Create an empty file of the appropriate type.

Step 2. Writing the code

1.Let’s imagine that we need to create a program that will perform the following actions:

will create a folder with the username;

will enter it and create a text.txt file there;

fill this file with text;

will display this text in the terminal;

will delete all created objects.

The final code is shown in the figure

2. To write the code, open the file in a text editor

For example Sublime Text 4, and add a comment that will describe the functionality of this script. Let’s add a hint for the operating system that it understood that this is a Bash script, such a mark is called a shebang:

Add a comment using # [comment text]

# That script creates a folder with the username, creates a file text.txt inside it, then fills this file with the specified text.

# After that prints that text out, and removes all created objects after it.

3. After writing the comment, we will add the following tapes to this script

  1. we will save the username;

  2. filename=”${USER}”;

  3. create a folder with the username;

  4. mkdir $filename.

  5. let’s go to this folder and create a text.txt file there;

  6. cd $filename;

  7. touch text.txt.

  8. fill this file with text;

  9. Esho “Glory to Ukraine!” > tht.tht.

4. Display this text in the terminal

5. Exit the folder that we will delete and delete it

Step 3. Attempt to run the script

1.In order to test the script, we need to run it

 For this we will call and run it with the following command:

./script.sh. You can see an error after launch

2. The reason for this error is that this file cannot be called due to lack of appropriate rights

Let’s fix it with the following command: chmod +x script.sh

An example of a correction.

Step 4. Try running the script again

1. Retrying to run the script

2. Everything seems to have worked, but it would be good to make sure and see each action clearly, adding some logging

3. The result of the program is shown

Other related articles
Automation and scriptsServices
Read more
Bash scripts
Discover the benefits of using the Bash command line interface over the GUI. Manage your computer's operating system efficiently with the Bash scripting language without the need for menus or windows.
327
Found an error?
If you find an error, take a screenshot and send it to the bot.