FSF Blogs: Winamp failed to confuse people about software freedom
The Winamp Collaborative License included restrictions that rendered Winamp nonfree
Read MoreThe Winamp Collaborative License included restrictions that rendered Winamp nonfree
Read MoreWordPress 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…
Read MoreComing 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.
Read Moreby 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. …
Read MoreThe 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 …
Read MoreRed 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.
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…
Read MoreThis 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, […]
Read MoreThe 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. …
Read MoreThe 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…
Read MoreUbuntu 24.10 is out and now we await Fedora 41.
Read MoreMongoDB 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….
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…
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:
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!
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!
The syntax for creating a function is simple:
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:
#!/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:
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:
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.
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.
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:
#!/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:
And the output will be:
$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!
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!
Every command in Bash returns an exit status:
0
means the command was successful.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:
#!/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:
if [ -f "$1" ]
checks if the file exists (-f
checks for a file).If you run the script like this:
And myfile.txt
doesn’t exist, you’ll see:
Adding error handling like this helps your script avoid continuing with bad data or broken commands.
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.
You can easily redirect the output of your script to a file. Here’s how:
#!/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
.After running the script, open script.log
, and you’ll see:
Logging is incredibly useful for tracking what happens in long-running or automated scripts.
Now, let’s combine everything we’ve learned so far into one simple script. This script will:
Here’s the script:
#!/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.
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!
Read MorePython 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…
Download from https://ftp.gnu.org/gnu/libunistring/libunistring-1.3.tar.gz
This is a stable release.
New in this release:
The data tables and algorithms have been updated to Unicode version 16.0.0.
New function uc_is_property_modifier_com…
Read MoreSolus 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 post 36 Useful Apache ‘.htaccess’ Tricks for Security and Performance appeared first on Linux Today.
Read More