Getting Started with Linux Shell Scripting: A Beginner's Tutorial
Linux shell scripting is a powerful tool for automating tasks, processing data and performing system maintenance. If you're new to Linux or shell scripting, this beginner's tutorial will help you get started.
What is a Linux shell script?
A shell script is a computer program that runs in a command-line interface. It is a series of commands and variables that are executed in a particular sequence to automate a task or to perform system maintenance. Shell scripts are written in a shell programming language like Bash, Korn, or C shell.
How to create a shell script?
To create a shell script, you have to follow these basic steps:
1. Open a text editor like vi, emacs, or nano.
2. Begin your script with a shebang line that specifies the shell you want to use.
3. Write your commands and save the file with a '.sh' extension.
4. Set the file as executable with the chmod command.
Let's create a simple shell script that outputs "Hello, World!" on the screen.
1. Open a text editor and type:
```
#!/bin/bash
echo "Hello, World!"
```
2. Save the file with a ".sh" extension, for example, "hello_world.sh".
3. Open the terminal and navigate to the directory where you saved the file.
4. Make the script executable with the chmod command:
```
chmod +x hello_world.sh
```
5. Run the script with the following command:
```
./hello_world.sh
```
And you should see the output on the terminal:
```
Hello, World!
```
Variables in shell scripting
Variables are used to store data that can be accessed and modified by the shell script. A variable name must start with a letter or an underscore, and it can contain letters, numbers, and underscores. To assign a value to a variable, use the equal sign (=).
Let's create a shell script that uses variables to store and print some data.
```
#!/bin/bash
# Assign values to variables
name="John"
age=30
country="USA"
# Print the values
echo "Name: $name"
echo "Age: $age"
echo "Country: $country"
```
Save the file with a ".sh" extension, for example, "data.sh". Make it executable with the chmod command, and run it with the following command:
```
./data.sh
```
You should see the output on the terminal:
```
Name: John
Age: 30
Country: USA
```
The script assigns values to three variables, name, age, and country, and prints their values using the echo command. Note that to access the value of a variable, you have to write a dollar sign ($) before the variable name.
Conditional statements in shell scripting
Conditional statements are used to execute different code based on a condition. In shell scripting, you can use the if-else statement to test a condition and execute different code blocks.
Let's create a simple shell script that tests whether a person is eligible to vote based on their age.
```
#!/bin/bash
# Assign a value to the age variable
age=19
# Test the age and print a message
if [ $age -ge 18 ]; then
echo "You are eligible to vote."
else
echo "You are not eligible to vote."
fi
```
Save the file with a ".sh" extension, for example, "vote.sh". Make it executable with the chmod command, and run it with the following command:
```
./vote.sh
```
You should see the output on the terminal:
```
You are eligible to vote.
```
The script assigns a value of 19 to the age variable, tests whether the age is greater than or equal to 18 using the if statement, and prints a message accordingly.
Loops in shell scripting
Loops are used to execute a set of commands repeatedly. In shell scripting, you can use the while loop to execute a set of commands as long as a condition is true, or the for loop to execute a set of commands for a specified number of times.
Let's create a simple shell script that prints the first ten numbers using a for loop.
```
#!/bin/bash
# Print the first ten numbers
for i in {1..10}; do
echo $i
done
```
Save the file with a ".sh" extension, for example, "numbers.sh". Make it executable with the chmod command, and run it with the following command:
```
./numbers.sh
```
You should see the output on the terminal:
```
1
2
3
4
5
6
7
8
9
10
```
The script uses a for loop to iterate over the values 1 to 10 and print them using the echo command.
Conclusion
Linux shell scripting is a powerful tool for automating tasks, processing data, and performing system maintenance. In this beginner's tutorial, we covered some of the basic concepts of shell scripting, including creating a shell script, using variables, conditional statements, and loops. With these skills, you can start writing shell scripts to automate repetitive tasks, process large datasets, and maintain your system more efficiently.