| by Arround The Web

Ardour 8.10 Open-Source DAW Released with Fixes for New Major Issues

Ardour 8.10 open-source digital audio workstation is now available for download with various bug fixes and minor improvements. Here’s what’s changed in this release!
The post Ardour 8.10 Open-Source DAW Released with Fixes for New Major Issues appeared…

Share Button
Read More
| by Arround The Web

VirtualBox 7.1.4 Released, Here’s What’s New

VirtualBox 7.1.4 offers improved GUI, fixes for Linux kernel flickering, Windows 11 issues, and initial support for Linux kernel 6.12.
The post VirtualBox 7.1.4 Released, Here’s What’s New appeared first on Linux Today.

Share Button
Read More
| by Arround The Web

KDE Plasma 6.2.1: October Bugfix Update Released

Plasma 6.2.1 desktop environment October update is out now bringing essential bug fixes, feature refinements, and fresh translations.
The post KDE Plasma 6.2.1: October Bugfix Update Released appeared first on Linux Today.

Share Button
Read More
| by Arround The Web

SUSE Edge upgrade targets Kubernetes and Linux at the Edge

SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments.
The post SUSE Edge upgrade targets Kubernetes an…

Share Button
Read More
| by Arround The Web

FSF Blogs: Winamp failed to confuse people about software freedom

The Winamp Collaborative License included restrictions that rendered Winamp nonfree

Share Button
Read More
| by Arround The Web

FSF Blogs: Winamp failed to confuse people about software freedom

The Winamp Collaborative License included restrictions that rendered Winamp nonfree

Share Button
Read More
| by Arround The Web

About that brawl between the WordPress co-founder and WP Engine…

WordPress has been the most popular content management system for years — and WP Engine was one of the most popular WordPress hosting services around. Not long ago, everyone was happy. Now, it’s a miserable open-source business war. What happened…

Share Button
Read More
| by Arround The Web

VirtualBox 7.1.4 Adds Initial Support for Linux Kernel 6.12 and Other Linux Changes

Coming only three weeks after VirtualBox 7.1.2, the VirtualBox 7.1.4 release is here to introduce automatic upgrading of the Linux Guest Additions via the Devices menu on ARM systems, improved flickering, black screen, and other screen update issues with recent Linux kernels, and adds initial support for the upcoming Linux 6.12 kernel series in Linux […]

The post VirtualBox 7.1.4 Adds Initial Support for Linux Kernel 6.12 and Other Linux Changes appeared first on Linux Today.

Share Button
Read More
| by Arround The Web

The Evolution of Linux Package Management and Its Impact on Modern Computing

by George Whittaker

Introduction

If you’ve ever used a modern Linux distribution, you’ve likely experienced the convenience of installing and updating software with a single command. …

Share Button
Read More
| by Arround The Web

Distribution Release: Murena 2.4

The DistroWatch news feed is brought to you by TUXEDO COMPUTERS. The Murena team have announced the launch of /e/OS 2.4 which introduces a number of improvements for cameras and updates to the mobile operating system’s privacy features. “This release …

Share Button
Read More
| by Arround The Web

Red Hat’s New RHEL AI 1.2 Ships by Default on a Lenovo AI Server Line

Red Hat’s new deal will see RHEL AI 1.2 shipping preinstalled on a $29,000 AI-focused server from Lenovo.
The post Red Hat’s New RHEL AI 1.2 Ships by Default on a Lenovo AI Server Line appeared first on FOSS Force.

Share Button
Read More
| by Arround The Web

VirtualBox 7.1.4 Readies Linux Kernel 6.12 Support + More

Oracle has released the second maintenance update for the latest VirtualBox 7.1 series. VirtualBox 7.1.4 includes a small set of improvements, bug fixes, and stability enhancements to this open-source, cross-platform virtualisation tool, though new maj…

Share Button
Read More
| by Arround The Web

How to Install GNU Octave 9.2.0 in Ubuntu 24.04 | 22.04

This tutorial shows you how to install the latest GNU Octave (version 9.2.0 so far) in all current Ubuntu releases, including Ubuntu 20.04, Ubuntu 22.04, and Ubuntu 24.04. GNU Octave is a free open-source scientific programming language, primarily intended for numerical computations. It features powerful mathematics-oriented syntax with built-in 2D/3D plotting and visualization tools, cross-platform, […]

Share Button
Read More
| by Arround The Web

How to Install Opcache for Optimal PHP Performance on Linux

The post How to Install Opcache for Optimal PHP Performance on Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .PHP (Hypertext Preprocessor) is a widely-used server-side scripting language known for its efficiency in web development. …

Share Button
Read More
| by Arround The Web

How to Secure SSH with pam_faillock: Lockout Failed Login Attempts

The post How to Secure SSH with pam_faillock: Lockout Failed Login Attempts first appeared on Tecmint: Linux Howtos, Tutorials & Guides .The pam_tally2 module, once used to lock user accounts after a certain number of failed SSH login attempts, has bee…

Share Button
Read More
| by Arround The Web

FOSS Weekly #24.42: Ubuntu 24.10, Redox OS Review, LLMs on Pi, KDE Plasma 6.2, Linux Terminal on Android and More

Ubuntu 24.10 is out and now we await Fedora 41.

Share Button
Read More
| by Arround The Web

How to Install and Secure MongoDB on Ubuntu 24.04

MongoDB is an open-source, cross-platform, and distributed NoSQL (non-SQL or Non-Relational) database system. In this guide, we’ll show you how to install MongoDB on an Ubuntu 24.04 server.
The post How to Install and Secure MongoDB on Ubuntu 24….

Share Button
Read More
| by Arround The Web

How to Install Fail2ban on Ubuntu 24.04 Server

Fail2ban is free and open-source IPS (Intrusion Prevention Software) that helps administrators secure Linux servers against malicious login and brute-force attacks. In this guide, you’ll learn how to install Fail2ba on Ubuntu 24.04 server.
The po…

Share Button
Read More
| by Arround The Web

Linux Bash Scripting Part 2: Functions, Arguments, and Basic Error Handling

In Part 1 of this tutorial series, we introduced the basics of Bash scripting on Linux. We learned how to write simple scripts, use variables, loops, conditionals, and even schedule tasks with cron. Now, let’s build on that knowledge with some beginner-friendly, yet more advanced, techniques.

In this article, we’ll explore:

  • Functions: Reusable blocks of code to keep your scripts clean and organized.
  • Command-line arguments: How to make your scripts accept input directly from the terminal.
  • Basic error handling: Checking for problems so your scripts can deal with them gracefully.
  • Simple logging: How to capture the output of your script for future reference.

Don’t worry, we’ll keep things simple and explain everything along the way. By the end of this guide, you’ll feel more confident about writing Bash scripts that are a little more dynamic and smarter!


1. Using Functions to Keep Things Organized

As your scripts get bigger, you’ll notice you’re doing similar things in different parts of the script. That’s where functions come in handy. A function is a way to group commands together and reuse them throughout your script.

Think of it like a recipe you can call anytime you need it!

How to Create a Function

The syntax for creating a function is simple:

Bash:

function_name() {
# Your commands here
}

Let’s say you want to greet the user. Instead of writing the same command over and over again, you can create a function for it:

Bash:

#!/bin/bash
# Define a function to greet the user
greet_user() {
echo “Hello, $1! Welcome to Bash scripting!”
}

# Call the function
greet_user “Alice”
greet_user “Bob”

In this script:

  • We define the function greet_user that prints a greeting.
  • $1 represents the first argument passed to the function (in this case, the user’s name).

When you run the script, you’ll see:

Hello, Alice! Welcome to Bash scripting!
Hello, Bob! Welcome to Bash scripting!

You can pass different names to the function, making it reusable. Functions are super helpful when you need to use the same logic multiple times in your script.


2. Command-Line Arguments: Making Your Script Interactive

Wouldn’t it be great if you could pass information to your script when you run it, instead of hardcoding values into it? Bash lets you do that with command-line arguments.

What Are Command-Line Arguments?

Command-line arguments allow you to pass data to your script from the terminal. For example, instead of telling the script what to do beforehand, you give it instructions when you run it.

Here’s an example script that accepts a name and a favorite color as arguments:

Bash:

#!/bin/bash
# The first argument is the name, the second is the favorite color
greet_user() {
name=$1
color=$2

echo “Hello, $name! Your favorite color is $color.”

Let’s say you save this script as greet.sh. You can run it from the terminal like this:

./greet.sh Alice blue

And the output will be:

Hello, Alice! Your favorite color is blue.

Accessing Command-Line Arguments

  • $1, $2, $3, etc., represent the first, second, third argument, and so on.
  • $0 represents the script’s name.
  • $# gives the total number of arguments passed.

This makes your scripts much more flexible. Now, instead of changing the script every time you want to run it with different inputs, you just pass new arguments directly from the terminal!


3. Basic Error Handling: Dealing with Problems

Not everything goes smoothly in scripting, especially when dealing with files or commands that might fail. That’s why it’s important to include error handling in your script. You don’t want your script to keep running if something goes wrong!

Checking the Exit Status of a Command

Every command in Bash returns an exit status:

  • 0 means the command was successful.
  • Any number other than 0 means something went wrong.

We can use this exit status to handle errors. For example, let’s check if a file exists before trying to copy it:

bash:

#!/bin/bash

# Check if the file exists before copying
if [ -f “$1” ]; then
cp “$1” /backup/
echo “File copied successfully.”
else
echo “Error: $1 does not exist.”
exit 1 # Exit with an error code
fi

Here’s how this script works:

  1. The if [ -f "$1" ] checks if the file exists (-f checks for a file).
  2. If the file exists, it copies the file.
  3. If the file doesn’t exist, it prints an error message and exits the script with an error code (1).

If you run the script like this:

./copy.sh myfile.txt

And myfile.txt doesn’t exist, you’ll see:

Error: myfile.txt does not exist.

Adding error handling like this helps your script avoid continuing with bad data or broken commands.


4. Simple Logging: Keeping Track of What Happens

Logging is a way to record what your script does. Instead of relying on memory or watching the terminal, you can save everything that happens into a file for future reference.

Redirecting Output to a Log File

You can easily redirect the output of your script to a file. Here’s how:

Bash:

#!/bin/bash
# Redirect output to a log file
exec > script.log 2>&1

echo “This is a log of everything that happens in this script.”
date # Logs the current date and time
echo “Done!”

Let’s break it down:

  • exec > script.log 2>&1 redirects all output (both standard output and error messages) to a file called script.log.
  • Now, anything the script prints will go to the log file instead of the terminal.

After running the script, open script.log, and you’ll see:

This is a log of everything that happens in this script.
Tue Oct 17 10:00:00 UTC 2024
Done!

Logging is incredibly useful for tracking what happens in long-running or automated scripts.


5. Combining It All: A Simple Example

Now, let’s combine everything we’ve learned so far into one simple script. This script will:

  1. Accept a filename as an argument.
  2. Check if the file exists.
  3. If it exists, copy it to a backup directory and log the action.
  4. If it doesn’t exist, log an error.

Here’s the script:

bash:

#!/bin/bash

# Redirect all output to a log file
exec > backup.log 2>&1

# Check if exactly one argument (a file name) is passed
if [ $# -ne 1 ]; then
echo “Usage: $0 filename”
exit 1
fi

# Check if the file exists
if [ -f “$1” ]; then
# Copy the file to the backup directory
cp “$1” /backup/
echo “$(date): $1 copied to /backup/”
else
echo “$(date): Error – $1 does not exist.”
exit 1
fi

If you run this script and provide a valid file, it will copy the file to /backup/ and log the success message. If the file doesn’t exist, it will log an error.


Conclusion

In this article, we’ve built on the basics of Bash scripting by introducing functions, command-line arguments, error handling, and logging. These are essential skills that will help you write more effective and robust scripts.

As you practice these new techniques, you’ll see how much more dynamic and powerful your scripts can become. Whether you’re automating backups, processing files, or just learning to make the most out of the Linux command line, you now have some new tools to get the job done!

In the next article, we’ll dive deeper into more intermediate scripting techniques, but for now, keep practicing with what you’ve learned here. Happy coding!

Share Button
Read More
| by Arround The Web

13 Best Free and Open Source Command-Line Python Application Development Tools

Python comes with several different libraries that allow you to write a command line interface for your scripts. Here are 13 of the best application development tools.
The post 13 Best Free and Open Source Command-Line Python Application Development To…

Share Button
Read More