Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
5 Shell Scripts for Linux Newbies to Learn Shell Programming
If you're new to Linux, learning shell scripting can seem daunting at first. However, shell scripting can be an incredibly useful skill to have when working with Linux systems. Shell scripts can automate repetitive tasks, perform system administration tasks, and even help with software development.
In this article, we'll look at five essential shell scripts that Linux newbies can learn to get started with shell programming. We'll cover some basics of shell scripting, and provide examples of each script to help you understand how they work.
What is Shell Scripting?
A shell script is a program written in a scripting language that runs in a Unix/Linux shell. Shell scripting is a powerful tool that allows you to automate tasks, perform system administration tasks, and even create software.
Shell scripts are executed in a command-line interface, and they can be used to perform a wide variety of tasks, from basic file management to complex data processing.
Script 1: Hello World
The first script that every programmer learns is the classic "Hello, World!" program. It's a simple script that just prints a message to the screen. Here's what the script looks like
Example
#!/bin/bash echo "Hello, World!"
Output
Hello, World!
Let's break this down. The first line of the script is called the "shebang" line, and it tells the shell which interpreter to use to run the script. In this case, we're using the bash shell.
The second line is the actual command that prints the message to the screen. The echo command simply displays the text that follows it.
To run the script, save it to a file (e.g., hello.sh), make it executable with chmod +x hello.sh command, and then run it with ./hello.sh.
Script 2: Simple Calculator
The next script we'll look at is a simple calculator. This script takes two numbers as input from the user and performs a basic arithmetic operation on them. Here's what the script looks like
Example
#!/bin/bash echo "Enter first number: " read num1 echo "Enter second number: " read num2 echo "The sum is: $((num1 + num2))"
Output
Enter first number: 10 Enter second number: 20 The sum is: 30
Let's break this down. The first two lines of the script prompt the user to enter two numbers, and then read those numbers into num1 and num2 variables.
The third line performs the actual calculation. In this case, we're adding num1 and num2 together using the $(( )) syntax for arithmetic expansion.
To run the script, save it to a file (e.g., calculator.sh), make it executable with chmod +x calculator.sh command, and then run it with ./calculator.sh.
Script 3: File Backup
The next script we'll look at is a file backup script. This script copies a file to a backup directory and adds a timestamp to the file name. Here's what the script looks like
#!/bin/bash
backup_dir=/path/to/backup/dir/
filename=file.txt
cp $filename $backup_dir/${filename}_$(date +%Y-%m-%d_%H-%M-%S)
Let's break this down. The first line of the script sets a variable called backup_dir to the path of the backup directory.
The second line sets a variable called filename to the name of the file we want to back up.
The third line copies the file to the backup directory using the cp command. The ${filename}_$(date +%Y-%m-%d_%H-%M-%S) syntax adds a timestamp to the end of the file name, so that each backup has a unique name.
To run the script, save it to a file (e.g., backup.sh), make it executable with chmod +x backup.sh command, and then run it with ./backup.sh.
Script 4: System Information
The next script we'll look at is a system information script. This script displays information about the system, such as operating system, kernel version, and CPU information. Here's what the script looks like
Example
#!/bin/bash
echo "Operating system: $(uname -o)"
echo "Kernel version: $(uname -r)"
echo "CPU information: $(lscpu | grep "Model name" | awk '{print $3, $4, $5, $6, $7, $8}')"
Output
Operating system: GNU/Linux Kernel version: 5.15.0-60-generic CPU information: AMD Ryzen 9 5950X 16-Core Processor
Let's break this down. The first line of the script is the shebang line, as usual.
The next three lines use various commands to display system information. The uname -o command displays the operating system, uname -r command displays the kernel version, and lscpu | grep "Model name" | awk '{print $3, $4, $5, $6, $7, $8}' command displays CPU information.
To run the script, save it to a file (e.g., systeminfo.sh), make it executable with chmod +x systeminfo.sh command, and then run it with ./systeminfo.sh.
Script 5: Password Generator
The last script we'll look at is a password generator. This script generates a random password using a combination of uppercase and lowercase letters, numbers, and special characters. Here's what the script looks like
Example
#!/bin/bash length=12 password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$%^&*()_+-=' | fold -w $length | head -n 1) echo "Your new password is: $password"
Output
Your new password is: YRY+zm@JwYi#
Let's break this down. The first line of the script is the shebang line, as usual.
The second line sets a variable called length to the desired length of the password.
The third line generates a random password using cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$%^&*()_+-=' | fold -w $length | head -n 1 command. This command uses cat /dev/urandom to generate random data, and then uses tr command to filter out unwanted characters. The fold -w $length command breaks the output into lines of the desired length, and head -n 1 command selects the first line (i.e., the password).
To run the script, save it to a file (e.g., password.sh), make it executable with chmod +x password.sh command, and then run it with ./password.sh.
Additional Useful Scripts
File Search Script
A script that allows you to search for files in a directory or subdirectories based on a keyword. Here's an example
#!/bin/bash echo "Enter directory to search: " read dir echo "Enter keyword to search for: " read keyword find $dir -name "*$keyword*" -print
This script prompts the user to enter a directory and a keyword to search for, and then uses the find command to search for files with that keyword in their names.
Directory Backup Script
A script that backs up an entire directory (including subdirectories and files) to a backup location. Here's an example
#!/bin/bash src_dir=/path/to/src/dir/ backup_dir=/path/to/backup/dir/ tar -czf $backup_dir/backup_$(date +%Y-%m-%d_%H-%M-%S).tar.gz $src_dir
This script sets variables for the source directory and backup directory, and then uses the tar command to create a compressed archive of the source directory in the backup directory, with a timestamp in the filename.
Key Points
Shebang line Always start scripts with
#!/bin/bashto specify the interpreterVariables Use descriptive names and reference them with
$variable_nameCommands Shell scripts can execute any command available in the Linux terminal
Permissions Make scripts executable with
chmod +x script_name.shTesting Always test scripts in a safe environment before using in production
Conclusion
Shell scripting is a powerful tool that can help you automate tasks, perform system administration tasks, and even create software. The five scripts we covered in this article provide a solid foundation for understanding shell programming concepts like variables, user input, file operations, and system commands. Remember, practice makes perfect, so don't be afraid to experiment with different commands and scripts to see what works best for you.
