Some techniques

Debugging shell scripts is an important part of the development process as it helps you identify and correct errors in your scripts. There are several techniques that can be used to debug shell scripts, including displaying variables and logging errors.

Variable display

You can use the “echo” command to display the value of a variable at a certain point in your script. For example:

#!/bin/bash
name=”John”
echo “The name is $name”

When running this script, the output will be “The name is John”. This can help verify that variables are being assigned correctly.


Error logging

You can use the “set -e” command at the beginning of your script to automatically terminate the script if an error occurs. Additionally, you can use the “set -x” command to enable debug mode, which displays each command as it is executed. For example:

#!/bin/bash
set -e
set -x
echo “Starting script”
ls /nonexistent_dir
echo “End of script”

When running this script, you will get an error message that the directory does not exist and the script will exit. In addition, debug mode will display each command executed, making it easier to identify where the error occurred.


Output redirection

You can redirect the output of your script to a log file, which can be useful for analyzing errors later. For example:

#!/bin/bash
exec &> log.txt
echo “Starting script”
ls /nonexistent_dir
echo “End of script”

When running this script, the output will be redirected to the “log.txt” file. If an error occurs, it will be logged to this file.

Start, Stop, and Restart Services with Shell Scripts

Managing processes on a Unix/Linux system using shell scripts is a common task, and there are several commands and operations available for this purpose. Some examples are:

Start a service

You can use the “systemctl” command to start a service on the system. For example, the following command starts the Apache service:

systemctl start apache2


Stopping a service

You can use the “systemctl” command to stop a service on the system. For example, the following command for the Apache service:

systemctl stop apache2


Restart a service

You can use the “systemctl” command to restart a service on the system. For example, the following command restarts the Apache service:

systemctl restart apache2


List Services

You can use the “systemctl” command to list the services available on the system. For example, the following command lists all available services on the system:

systemctl list-unit-files –type=service


Checking the status of a service

You can use the “systemctl” command to check the status of a service on the system. For example, the following command checks the status of the Apache service:

systemctl status apache2


Kill a process

You can use the “kill” command to kill a running process. For example, the following command terminates the process with ID 123:

kill 123


Check running processes

You can use the “ps” command to list the processes running on the system. For example, the following command lists all processes running on the system:

ps -ef

Copy a file

cp is a Linux shell command to copy files and directories.

You can use the “cp” command to copy a file. For example, the following command copies the file “file.txt” to the file “copy_file.txt”:

cp file.txt copy_file.txt

You can use the “cp” command to copy a directories.

Copy from source to dest
$ cp [optionssource dest

cp command main options:

optiondescription
cp -aarchive files
cp -fforce copy by removing the destination file if needed
cp -iinteractive – ask before overwrite
cp -llink files instead of copy
cp -Lfollow symbolic links
cp -nno file overwrite
cp -Rrecursive copy (including hidden files)
cp -uupdate – copy when source is newer than dest
cp -vverbose – print informative messages

Manipulate strings in shell scripts

To manipulate strings in shell scripts, you can use a number of operations and commands available in the language. Some examples of how to find and replace text in a string are:

Finding text in a string: You can use the “grep” command to find a pattern of text in a string. For example, the following command looks for the word “example” in the string “This is an example string”:

echo “This is an example string” | grep example

The result would be “example string”.

Replace text in a string: You can use the “sed” command to replace a text pattern in a string. For example, the following command replaces the word “example” with “test” in the string “This is an example string”:

echo “This is an example string” | sed ‘s/example/test/g’

The result would be “This is a test string”.

Get the length of a string: You can use the “#” operator to get the length of a string. For example, the following command returns the length of the string “example”:

string=”example”
echo “${#string}”

The result would be “7”.

Replacing the contents of a variable: You can use the “=” operator to replace the contents of a variable. For example, the following command replaces the contents of the “string” variable from “example” to “test”:

string=”example”
string=”test”
echo “$string”

The result would be “test”.

Extract a substring: You can use the “cut” command to extract a part of a string. For example, the following command extracts the first two characters from the string “example”:

echo “example” | cut -c 1-2

The result would be “ex”.