Shell scripting is a powerful skill for every system administrator and developer working in a Unix-like environment. In this blog post, we will delve into some fundamental concepts of shell scripting, exploring the shabang, variable substitution, dynamic data reading, command substitution, input/output redirection, error redirection, piping, tee, xargs, and conclude with some commonly asked Linux interview questions.
What is Shabang?
The shabang, represented by #!
, is a special sequence of characters at the beginning of a script that tells the system which interpreter should be used to execute the script. For example:
#!/bin/bash
This line at the beginning of a script indicates that the Bash shell should be used to interpret the script.
Variable Substitution
In shell scripting, variable substitution is a way to replace the value of a variable with its content. Let's consider an example:
name="Shell Script"
echo "Welcome to $name"
In this example, the value of the name
variable is substituted into the string.
Reading Dynamic Data from the End User
To read dynamic data from the end user, the read
command is used. Here's an example:
eecho "Enter your name:"
read username
echo "Hello, $username!"
The read
command is used to take user input and store it in the variable username
.
Command Substitution
Command substitution is a way to execute a command and replace it with its output. For instance:
current_date=$(date)
echo "Today is $current_date"
Here, the date
command is executed, and its output is stored in the current_date
variable.
Input and Output Redirection
Input and output redirection allow us to manipulate where a command reads input from or writes output to. Examples include:
# Output redirection
ls > file_list.txt
# Input redirection
wc -l < file_list.txt
Here, the first command redirects the output of ls
to a file, and the second command redirects the input of wc -l
from the file.
Error Redirection
Error redirection allows us to separate standard output from error messages. For example:
command_that_might_fail 2> error_log.txt
This redirects error messages (file descriptor 2) to the error_log.txt
file.
Piping
Piping is a powerful feature that allows the output of one command to be used as the input for another. Example:
ps aux | grep "process_name"
Here, the output of ps aux
is piped to grep
to filter processes containing "process_name."
Tee
The tee
command is used to redirect output to both a file and the screen. Example:
echo "Hello, Tee!" | tee output.txt
This command will display "Hello, Tee!" on the screen and also save it to the output.txt
file.
xargs Command
The xargs
command is used to build and execute command lines from standard input. Example:
echo "file1 file2 file3" | xargs rm
Here, xargs
takes the list of files and constructs a rm
command.
Symbolic Link (Soft Link):
A symbolic link, often referred to as a soft link, is a separate file that acts as a pointer to the target file or directory. If the original file is deleted, moved, or renamed, the symbolic link becomes "broken" or "dangling," pointing to a non-existent location.
Example:
ln -s /home/vagrant/one/softcheckkar.txt hmm
In this example, a symbolic link named "hmm" is created, pointing to the file /home/vagrant/one/softcheckkar.txt
. If the original file is removed, the symbolic link "hmm" will be broken.
Hard Link:
A hard link is an additional reference to an existing inode (data structure on a filesystem that stores information about a file or directory). All hard links to the same file share the same inode and, therefore, the same physical data blocks on the disk. If the original file is deleted or renamed, the hard link(s) remain valid and accessible.
Example:
ln /home/vagrant/one/softcheckkar.txt hmmhard
In this example, a hard link named "hmmhard" is created, pointing to the same inode as the file /home/vagrant/one/softcheckkar.txt
. If the original file is removed or renamed, the hard link "hmmhard" remains valid and can still be used to access the file.