File search and replace

To use shell script regular expressions to search and replace files, you can use the “sed” command.

sed is a text stream editor that can be used to make file replacements. To use sed, you need to specify the regular expression you want to fetch and the string you want to replace it with.

The general syntax of the sed command for search and replace is as follows:

sed ‘s/pattern/replacement/g’ file

Where:

  • “pattern” is the regular expression you want to fetch;
  • “replacement” is the string you want to replace with the pattern;
  • “g” is an option that indicates that all occurrences of the pattern are to be replaced;
  • “file” is the name of the file you want to replace.

Por exemplo, suponha que você queira substituir todas as ocorrências da palavra “casa” pela palavra “apartamento” no arquivo “arquivo.txt”. Você pode fazer isso com o seguinte comando:

sed ‘s/house/apartment/g’ file.txt

In addition, “sed” supports a wide variety of regular expressions that can be used to search for more complex patterns, such as:

  • “[a-z]”: search for any character from a to z;
  • “^”: search for the beginning of the line;
  • “$”: search for the end of the line;
  • “.”: search for any character;
  • “*”: search for zero or more occurrences of the previous pattern;
  • “+”: search for one or more occurrences of the previous pattern;
  • “\”: used to escape special characters like ., $, ^, etc.

For example, the following command searches for lines that begin with the word “error” and contain the word “system”:

sed -n ‘/^error.*system/p’ file.txt

In this example, the “-n” option is used to suppress the standard output of “sed”, and the regular expression “/^error.*system/p” is used to search for lines that start with “error” and contain “system “. The “p” option is used to print the lines that match the pattern.

Create and use functions

To create and use functions in shell scripts, you can follow these steps:

  • Define the function: The syntax for defining a function is as follows:

function_name() {
# function code here
}

You can name the function whatever you like, as long as it doesn’t contain any whitespace.

  • Write function code: Within the function code block, you can write the commands you want to execute.
  • Call the function: to call the function, just write its name followed by parentheses.

Here’s an example of how to create and use a function in a shell script:

# define the function
show_message() {
echo “This is a sample message”
}

#Call the function
show_message

When running this script, the message “This is a sample message” will be displayed on the standard output.

You can also pass parameters to a function in the shell. To do this, simply define the parameters inside the parentheses in the function definition and refer to them by name when executing the function. Here is an example:

#define the function
salute() {
name=$1
echo “Hello, $name! How are you?”
}

#Call the function with a parameter
salute “John”

In this example, the “salute” function takes a name parameter, which is used to display a custom greeting on standard output. When calling the function with the “John” parameter, the output will be “Hello, John! How are you?”.

“case” command

The “case” command is used to test various conditions and execute a set of commands for each condition. Here is an example of how to use the “case” command in a shell script:

!/bin/bash
fruit=”banana”
case $fruit in
“apple”) echo “The fruit is an apple”;;
“orange”) echo “The fruit is an orange”;;
“banana”) echo “The fruit is a banana”;;
*) echo “The fruit is not apple, orange or banana”;;
esac

This script checks the value of the “fruit” variable using the “case” command. If the value is “apple”, the script prints the message “The fruit is an apple”.

“elif” command

The “elif” command is used in conjunction with the “if” command to execute a command or group of commands if a certain condition is not true, but another condition is true. Here is an example of how to use the “elif” command in a shell script:

!/bin/bash
age=15
if [ $age -ge 18 ]
then
echo “You are of legal age”
elif [ $age -ge 13 ]
then
echo “You are a teenager”
else
echo “You are a child”
fi


This script checks if the “age” variable is greater than or equal to 18. If the condition is true, the script prints the message “You are of legal age”. Otherwise, it checks if the “age” variable is greater than or equal to 13. If this condition is true, the script prints the message “You are a teenager”. Otherwise, it prints the message “You are a child”.

“else” command

The “else” command is used in conjunction with the “if” command to execute a command or group of commands if the “if” condition is not true. Here is an example of how to use the “else” command in a shell script:

!/bin/bash
age=16
if [ $age -ge 18 ]
then
echo “You are of legal age”
else
echo “You are underage”
fi


This script checks if the “age” variable is greater than or equal to 18. If the condition is true, the script prints the message “You are of legal age”. Otherwise, it prints the message “You are underage”.

“if” command

The “if” command is used to execute a command or group of commands if a given condition is true. Here is an example of how to use the “if” command in a shell script:

!/bin/bash
age=18
if [ $age -ge 18 ]
then
echo “You are of legal age”
fi

This script checks if the “age” variable is greater than or equal to 18. If the condition is true, the script prints the message “You are of legal age”.

Assignment operators

Assignment operators allow you to assign values to variables in Shell Script. Here are some of the most common assignment operators in Shell Script:

“=” (simple assignment)
“+=” (additive assignment)
“-=” (subtractive assignment)
“*=” (multiplicative assignment)
“/=” (divisive assignment)
“%=” (module assignment)

Here is an example of how to use the “+=” assignment operator in Shell Script to add a value to an existing variable:

number=5
number=10
echo “The number is $number”