Solus 4.6 debuts with Linux kernel 6.10, Usr-Merge, new multimedia defaults, and upgraded Mesa 24.2 for better hardware support.
The post Solus 4.6 “Convergence” Released, Here’s What’s New appeared first on Linux Today.
Solus 4.6 debuts with Linux kernel 6.10, Usr-Merge, new multimedia defaults, and upgraded Mesa 24.2 for better hardware support.
The post Solus 4.6 “Convergence” Released, Here’s What’s New appeared first on Linux Today.
Solus 4.6 debuts with Linux kernel 6.10, Usr-Merge, new multimedia defaults, and upgraded Mesa 24.2 for better hardware support.
The post Solus 4.6 “Convergence” Released, Here’s What’s New appeared first on Linux Today.
The .htaccess file is a powerful configuration file used on Apache-based web servers to manage and modify settings at the directory level. By modifying .htaccess file, you can control many aspects of your website’s behavior without needing to alter server-wide settings. Below are 25 essential .htaccess tricks and tips that can help improve your site’s […]
The .htaccess file is a powerful configuration file used on Apache-based web servers to manage and modify settings at the directory level. By modifying .htaccess file, you can control many aspects of your website’s behavior without needing to alter server-wide settings. Below are 25 essential .htaccess tricks and tips that can help improve your site’s […]
Fwupd 2.0.1 adds API for gnome-firmware device emulation, shifts emulation-tag devices to the database, and fixes compilation issues.
The post Fwupd 2.0.1 Enhances GNOME Firmware Emulation with New API appeared first on Linux Today.
Inkscape is a popular free and open-source vector graphics editor, used by artists, designers, and anyone who wants to create high-quality illustrations, icons, logos, and other types of graphics. Unlike raster images that use pixels (like in Photoshop), vector graphics are based on mathematical equations, allowing you to scale your artwork up or down without […]
Find out the CLI and GUI methods to add and remove passwords from PDF files on Linux, Windows, and macOS with a practical example.
The post How to Add and Remove Passwords from PDF Files on Linux appeared first on Linux Today.
Learn how to install iTunes on Ubuntu using a Wine program, then how to launch iTunes on Ubuntu, including its removal steps.
The post How to Install and Use iTunes on Ubuntu (A Quick Guide) appeared first on Linux Today.
The sshd splitting progresses with sshd-auth, isolating authentication in a separate binary, and reducing pre-auth attack surface in OpenSSH.
The post OpenSSH Splits Again: New Authentication Binary Unveiled appeared first on Linux Today.
Linux is a powerful operating system, not only because of its stability and open-source nature but also because of the incredible control it gives users over their system. One of the most effective ways to harness this power is through Bash scripting, which allows you to automate tasks, simplify processes, and unlock greater efficiency in managing Linux environments.
In this tutorial, we’ll walk you through the basics of creating and running a Bash script. Whether you’re new to Linux or looking to expand your skills, this guide will get you started with scripting.
What is Bash?
Bash (short for Bourne Again Shell) is a command-line interpreter that provides a user interface for Unix-based systems, including Linux. The shell is where you can type commands, run programs, and interact with the underlying OS. Bash scripts are text files that contain a series of commands executed line by line.
By learning how to write Bash scripts, you can automate repetitive tasks, schedule jobs, and even build complex workflows.
Part 1: Writing Your First Bash Script
Step 1: Create a New Script File
To create a new Bash script, you first need to create a file that will store your commands. Open a terminal and use a text editor (like nano or vim) to create a new file. For this example, let’s use nano:
Bash:
nano myscript.sh
The .sh extension is commonly used for Bash scripts, though it’s not required. You can name the file anything you want.
Step 2: Add a Shebang
At the very top of your script file, include a shebang line. This tells the system that the script should be executed using Bash. The shebang is written as follows:
Bash:
#!/bin/bash
Without this line, your script might not know which shell to use, especially if you’re running it in a different environment.
Step 3: Add Commands to the Script
Now, let’s add some simple commands. For example, we can create a script that outputs “Hello, World!” and shows the current date:
Bash:
#!/bin/bash # This is a comment echo“Hello, World!” echo“Today is: $(date)”
The echo command prints text to the terminal.
The $(date) command executes the `date` command and substitutes its output.
Step 4: Save and Exit
Once you’ve written your script, save the file by pressing CTRL + O in nano, then press Enter. To exit, press CTRL + X.
Part 2: Running Your Script
Step 1: Make the Script Executable
Before running your script, you need to give it executable permissions. You can do this using the chmod command:
Bash:
chmod +x myscript.sh
This command tells Linux that the file can be executed like a program.
Step 2: Run the Script
Now that your script is executable, you can run it by typing:
Bash:
./myscript.sh
You should see output similar to:
Bash:
“Hello, World!” “Today is: Mon Oct 16 12:34:56 UTC 2024”
Congratulations! You’ve just written and executed your first Bash script.
Part 3: Adding Variables and Logic
Bash scripts become more useful when you introduce variables and basic logic. Let’s expand our script.
Step 1: Using Variables
You can store values in variables and use them throughout your script. Here’s an example of a script that asks for your name and greets you:
Bash:
#!/bin/bash # Prompt the user for their name echo“Enter your name:” readname
# Greet the user echo“Hello, $name! Welcome to Bash scripting.”
In this script:
The read command captures user input and stores it in the name variable.
Variables in Bash are referenced by prefixing the variable name with a dollar sign ($).
Step 2: Using Conditional Statements
Bash supports if statements for basic decision-making. Here’s an example that checks if the user is root (the system administrator):
Bash:
#!/bin/bash # Check if the user is root if[“$USER”==“root”]; then echo“Hello, root user!” else echo“You are not the root user.” fi
The if statement checks if the USER environment variable is equal to root. The then block runs if the condition is true, and the else block runs if it’s false.
Step 3: Loops in Bash
Bash also supports loops, which are essential when you want to repeat a task multiple times. For example, here’s a script that prints numbers from 1 to 5:
Bash:
#!/bin/bash # Loop from 1 to 5 foriin{1..5}; do echo“Number: $i” done
Part 4: Making Your Script More Useful
Let’s apply what we’ve learned to automate a real task—backing up a directory. This script will copy all files from a source directory to a backup location.
# Create the backup directory if it doesn’t exist mkdir-p“$backup_dir”
# Copy files cp-r“$source_dir”/*“$backup_dir”
# Print a success message echo“Backup completed successfully.”
Step 2: Scheduling with Cron
To automate running this script daily, you can use cron, a task scheduler in Linux. To set this up, follow these steps:
Open the cron configuration file with the following command:
Bash:
crontab -e
Add a new line to run your script every day at 2 AM:
Bash:
0 2 * * * /path/to/myscript.sh
Save and exit. Now, your script will run automatically every day.
Conclusion
Bash scripting is a powerful tool that allows you to automate tasks, create workflows, and manage your Linux environment more efficiently. In this tutorial, we’ve covered the basics of writing and running Bash scripts, introduced variables, logic, and loops, and demonstrated how to automate tasks with cron.
With these fundamentals, you can now explore more advanced features like functions, error handling, and interacting with other Linux tools. Happy scripting!
The post How to Run Own Online SMS Portal with playSMS in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .playSMS is an open-source SMS management software that allows you to send and receive SMS messages using various gateways and
T…
The post How to Run Own Online SMS Portal with playSMS in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .playSMS is an open-source SMS management software that allows you to send and receive SMS messages using various gateways and
T…
The post CentOS Stream: The Perfect Distribution for Development Projects first appeared on Tecmint: Linux Howtos, Tutorials & Guides .CentOS Stream has become a key part of the Red Hat ecosystem, offering developers an exciting opportunity to shape th…
A firewall is a software that acts as a protective barrier between a user’s system and external networks, allowing or blocking packets based on predefined rules. Firewalls operate mainly at the network layer, handling both IPv4 and IPv6 packets. The decision to allow or block a packet is based on rules defined in the firewall. […]
Dollar for dollar matching funds, cool swag, and you get to sleep better at night knowing that the folks at Tor are able to continue working hard to protect your privacy. What’s not to like about that?
The post FOSS Force Tor’s Raising Funds — Get Swag…
The DistroWatch news feed is brought to you by TUXEDO COMPUTERS. Rodolphe Bachelart has announced the release of Voyager Live 24.10, the latest release of the project’s Ubuntu-based desktop Linux distribution that combines GNOME and Xfce into one unif…
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkPrivacy policy