Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /home/u596154002/domains/usbusinessreviews.com/public_html/wp-includes/load.php on line 2057

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u596154002/domains/usbusinessreviews.com/public_html/wp-includes/functions.php on line 6114
What is a Linux bash script and how do you create one? - Best Business Review Site 2024

What is a Linux bash script and how do you create one?

[ad_1]

bashgettyimages-1163541985

Sure, bash scripts can get very complicated — but at first they can be as simple as you like.

Deagreez/Getty Images

I’ve been using Linux for a very long time, during which I’ve done just about everything you can imagine with the open-source operating system. From my early days, one thing I needed to learn how to do was create a bash script. When I first started using Linux, I had a 33.6k modem that refused to stay online. To get around that problem, I had to create a bash script that would monitor my connectivity; if the script discovered I was offline, it would reconnect me.

Also: The best Linux laptops right now

Thankfully, I no longer have to pull off such tricks. In fact, Linux has become so user-friendly, I rarely have to bother with bash scripts. Even so, it’s a great feature to have handy.

What is a bash script?

Think of a bash script as a tiny application you create that consists of Linux commands. You can write bash scripts to do just about anything, such as create backups, set variables, open applications, navigate to specific directories, create files, and so much more. In fact, with just a little creativity, the sky’s the limit with bash scripts.

But remember, bash scripts are just that…scripts. They aren’t GUI applications, nor is there a GUI application that will walk you through the process of creating a script. In other words, bash scripts are a bit more advanced than using a word processor, web browser, or email client. 

That doesn’t mean bash scripts are for advanced users only. Sure, bash scripts can get very complicated but at first they can be as simple as you like.

Also: How to choose the right Linux desktop distribution for you

I’m going to demonstrate creating two different bash scripts. First, we’ll do the tried-and-true “Hello, World!” and then we’ll create a bash script that will back up a directory.

Are you ready? Let’s go.

How to create the Hello, World! bash script

What you’ll need: The only thing you’ll need for this is a running instance of Linux. Since every distribution supports bash scripts, it doesn’t matter which one you use. With that at the ready, let’s create.

The first thing you must do is open the terminal window, which can be found within your desktop menu.

Our first script file will be called hello_world.sh. Create the file with the command:

The first line in every bash script is:

The second line of the script is the Hello, World! portion. First, we’ll add a comment that indicates what the next command does, which might look like this:

# My Hello, World! script

Finally, the command for the bash script uses the echo command like so:

Put it all together and it looks like this:

#!/bin/bash

# My Hello, World! script
echo "Hello, World!"

Save and close the file with the Ctrl+X keyboard shortcut.

Before the script can be run, it must have executable permissions. To do that, issue the command:

To run your first bash script, issue the command:

The output of the command should be “Hello, World!”

Also: The most important reason you should be using Linux at home

Creating a backup with a bash script

Let’s say you want to back up your Documents directory to an external drive located at /media/USER/data (Where USER is your Linux username). With this backup, we’re also going to set variables that will be used to append the date to the backup file name, set the destination, as well as set the source. We’ll also use the tar command for the actual backup.

The first thing to do is create the new file with the command:

Also: The best Linux distros for beginners

1. Create the new file

The first thing to do is create the new file with the command:

2. Create the script

The entire script will look something like this (where USER is your Linux username):

#!/bin/bash

# set variables for data, destination, and source

BACKUPDATE=`date +%b-%d-%y` 

DESTINATION=/media/USER/data/DOCUMENTS-$BACKUPDATE.tar.gz 

SOURCEFOLDER=/home/USER/Documents 

# the backup command

tar -cpzf $DESTINATION $SOURCEFOLDER #create the backup

The Destination variable not only sets the location for the backup, but it also names the backup DOCUMENTS-BACKUPDATE.tar.gz (where BACKUPDATE will be the date the script is run).  For example, if you run the backup script on July 24, 2023, the file name would be DOCUMENTS-Jul-24-23.tar.gz. 

3. Give the script executable permissions

To make it such that the script can be run, give it executable permissions with the command:

4. Run the script

To run the new backup bash script, the command would be:

And that’s all it takes to create your first bash script. As I said, with a bit of creativity, you can do so much with these handy little command-line applications. 

Also: My idea for a great new beginner-friendly Linux distribution



[ad_2]

Source link

slot gacor slot gacor togel macau slot hoki bandar togel slot dana slot mahjong link slot link slot777 slot gampang maxwin slot hoki slot mahjong slot maxwin slot mpo slot777 slot toto slot toto situs toto toto slot situs toto situs toto situs toto situs toto slot88 toto slot slot gacor thailand slot bet receh situs toto situs toto slot toto slot situs toto situs toto situs toto situs togel macau toto slot slot demo slot pulsa slot pragmatic situs toto deposit dana 10k surga slot toto slot link situs toto situs toto slot situs toto situs toto slot777 slot gacor situs toto slot slot pulsa 10k toto togel situs toto slot situs toto slot gacor terpercaya slot dana slot gacor pay4d agen sbobet kedai168 kedai168 deposit pulsa situs toto slot pulsa situs toto slot pulsa situs toto situs toto situs toto slot dana toto slot situs toto slot pulsa toto slot situs toto slot pulsa situs toto situs toto situs toto toto slot toto slot slot toto akun pro maxwin situs toto slot gacor maxwin slot gacor maxwin situs toto slot slot depo 10k toto slot toto slot situs toto situs toto toto slot toto slot toto slot toto togel slot toto togel situs toto situs toto toto slot slot gacor slot gacor slot gacor situs toto situs toto cytotec toto slot situs toto situs toto toto slot situs toto situs toto slot gacor maxwin slot gacor maxwin link slot 10k slot gacor maxwin slot gacor slot pulsa situs slot 10k slot 10k toto slot toto slot situs toto situs toto situs toto bandar togel 4d toto slot