linux Notes By ShariqSP
Scripting Overview
What is Scripting?
Scripting refers to the process of writing and executing scripts, which are sequences of commands that automate tasks. Scripts are commonly used in programming and system administration to simplify repetitive tasks and streamline workflows.
Types of Scripting
There are various types of scripting languages, including:
- Bash Scripting: Shell scripting language primarily used on Unix and Unix-like systems.
- Python Scripting: High-level programming language known for its simplicity and readability.
- Perl Scripting: Versatile scripting language often used for text processing and system administration tasks.
- JavaScript: Scripting language commonly used in web development for client-side scripting.
- PowerShell: Command-line shell and scripting language developed by Microsoft for Windows systems.
Bash Scripting
Bash (Bourne Again Shell) is a command language interpreter for Unix-like operating systems. It is the default shell on most Linux distributions and macOS.
Bash scripting allows users to automate tasks, manipulate files, and perform system administration tasks using a series of commands.
Some common features of Bash scripting include:
- Variables and data types
- Conditional statements
- Loops
- Functions
- File manipulation
- Command execution
Bash Scripting Basics
1. Echo Command
The echo
command is used to print text to the terminal. It's commonly used for displaying messages or output from scripts.
echo "Hello, world!"
This command will output: Hello, world!
2. Declaring and Initializing Variables
In Bash, variables are declared and initialized without specifying their type. You simply assign a value to a variable name.
Example with Character
char='A'
echo $char
Here, char
is a variable holding the character A
.
Example with Number
number=42
echo $number
Here, number
is a variable holding the number 42
.
Example with String
string="Hello, Bash!"
echo $string
Here, string
is a variable holding the string Hello, Bash!
.
3. Reading Input from User
You can use the read
command to take input from the user.
read -p "Enter your name: " name
echo "Hello, $name!"
This script will prompt the user to enter their name and then greet them with the name they provided.
Solving Expressions in Bash Scripting
Bash scripting offers several methods to perform arithmetic operations and solve expressions. Here are the primary methods used for solving expressions:
1. Using the `expr` Command
The `expr` command evaluates expressions and outputs the result. It handles basic arithmetic operations but requires spaces around operators and operands.
# Addition
result=$(expr 5 + 3)
echo "5 + 3 = $result"
# Subtraction
result=$(expr 10 - 4)
echo "10 - 4 = $result"
# Multiplication (note the escaping of the asterisk)
result=$(expr 5 \* 3)
echo "5 * 3 = $result"
# Division
result=$(expr 15 / 3)
echo "15 / 3 = $result"
# Modulus
result=$(expr 10 % 3)
echo "10 % 3 = $result"
2. Using Double Parentheses `(( ))`
The double parentheses `(( ))` provide a more modern and flexible way to perform arithmetic operations. It supports a wide range of arithmetic operations and does not require escaping of operators.
# Addition
result=$(( 5 + 3 ))
echo "5 + 3 = $result"
# Subtraction
result=$(( 10 - 4 ))
echo "10 - 4 = $result"
# Multiplication
result=$(( 5 * 3 ))
echo "5 * 3 = $result"
# Division
result=$(( 15 / 3 ))
echo "15 / 3 = $result"
# Modulus
result=$(( 10 % 3 ))
echo "10 % 3 = $result"
3. Using `bc` Command
The `bc` command is a calculator utility that can handle arbitrary precision arithmetic. It is useful for more complex calculations and when floating-point arithmetic is needed.
# Addition
result=$(echo "5 + 3" | bc)
echo "5 + 3 = $result"
# Subtraction
result=$(echo "10 - 4" | bc)
echo "10 - 4 = $result"
# Multiplication
result=$(echo "5 * 3" | bc)
echo "5 * 3 = $result"
# Division (result will be a floating-point number)
result=$(echo "15 / 3" | bc -l)
echo "15 / 3 = $result"
# Modulus
result=$(echo "10 % 3" | bc)
echo "10 % 3 = $result"
4. Using `awk` Command
The `awk` command is a powerful text processing utility that can also be used for arithmetic operations. It is particularly useful for operations involving multiple fields or for complex scripts.
# Addition
result=$(awk 'BEGIN {print 5 + 3}')
echo "5 + 3 = $result"
# Subtraction
result=$(awk 'BEGIN {print 10 - 4}')
echo "10 - 4 = $result"
# Multiplication
result=$(awk 'BEGIN {print 5 * 3}')
echo "5 * 3 = $result"
# Division
result=$(awk 'BEGIN {print 15 / 3}')
echo "15 / 3 = $result"
# Modulus
result=$(awk 'BEGIN {print 10 % 3}')
echo "10 % 3 = $result"
5. Using `let` Command
The `let` command performs arithmetic operations directly and updates the value of variables. It is a simpler way to perform arithmetic operations in scripts without using the `$(( ))` syntax.
# Addition
let "result = 5 + 3"
echo "5 + 3 = $result"
# Subtraction
let "result = 10 - 4"
echo "10 - 4 = $result"
# Multiplication
let "result = 5 * 3"
echo "5 * 3 = $result"
# Division
let "result = 15 / 3"
echo "15 / 3 = $result"
# Modulus
let "result = 10 % 3"
echo "10 % 3 = $result"
Comparison with Conditional Statements
For comparisons, Bash provides conditional expressions within `[[ ]]` or `[ ]` to evaluate relationships between values. Here are examples:
# Greater than
if [[ 5 -gt 3 ]]; then
echo "5 is greater than 3"
fi
# Less than
if [[ 3 -lt 5 ]]; then
echo "3 is less than 5"
fi
# Greater than or equal to
if [[ 5 -ge 5 ]]; then
echo "5 is greater than or equal to 5"
fi
# Less than or equal to
if [[ 3 -le 5 ]]; then
echo "3 is less than or equal to 5"
fi
Bash Scripting Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical operations on numeric values.
- Addition (+):
expr 5 + 3
results in8
. - Subtraction (-):
expr 5 - 3
results in2
. - Multiplication (*):
expr 5 \* 3
results in15
. - Division (/):
expr 15 / 3
results in5
. - Modulus (%):
expr 5 % 3
results in2
.
2. Relational Operators
Relational operators are used to compare two values and return true or false.
- Equal to (==):
[ 5 == 5 ]
returnstrue
. - Not equal to (!=):
[ 5 != 3 ]
returnstrue
. - Greater than (>):
[ 5 \> 3 ]
returnstrue
using `>` in expressions. - Less than (<):
[ 3 \< 5 ]
returnstrue
using `<` in expressions. - Greater than or equal to (>=):
[ 5 \>= 5 ]
returnstrue
using `>=` in expressions. - Less than or equal to (<=):
[ 3 \<= 5 ]
returnstrue
using `<=` in expressions.
3. Logical Operators
Logical operators are used to combine conditional statements.
- AND (&&):
[ 5 -gt 3 ] && [ 8 -gt 5 ]
returnstrue
. - OR (||):
[ 5 -gt 3 ] || [ 8 -lt 5 ]
returnstrue
. - NOT (!):
! [ 5 -lt 3 ]
returnstrue
.
4. String Operators
String operators are used to perform operations on strings.
- String Equality:
[ "$string1" == "$string2" ]
returnstrue
if both strings are equal. - String Inequality:
[ "$string1" != "$string2" ]
returnstrue
if the strings are not equal. - String Length:
${#string}
returns the length ofstring
.
5. File Test Operators
File test operators are used to check file attributes.
- File Exists:
[ -e filename ]
returnstrue
if the file exists. - File is Directory:
[ -d dirname ]
returnstrue
if it is a directory. - File is Regular File:
[ -f filename ]
returnstrue
if it is a regular file. - File is Readable:
[ -r filename ]
returnstrue
if the file is readable. - File is Writable:
[ -w filename ]
returnstrue
if the file is writable. - File is Executable:
[ -x filename ]
returnstrue
if the file is executable.
6. Difference Between Numeric Comparison Operators
Greater than vs. -gt
Using >
and -gt
:
The symbol >
is used in arithmetic expressions within the double parentheses (( ))
, while -gt
is used within single brackets [ ]
or test
commands.
# Greater than with >
if (( 5 > 3 )); then
echo "5 is greater than 3"
fi
# Greater than with -gt
if [ 5 -gt 3 ]; then
echo "5 is greater than 3"
fi
Less than vs. -lt
Using <
and -lt
:
The symbol <
is used in arithmetic expressions within the double parentheses (( ))
, while -lt
is used within single brackets [ ]
or test
commands.
# Less than with <
if (( 3 < 5 )); then
echo "3 is less than 5"
fi
# Less than with -lt
if [ 3 -lt 5 ]; then
echo "3 is less than 5"
fi
Greater than or equal to vs. -ge
Using >=
and -ge
:
The symbol >=
is used in arithmetic expressions within the double parentheses (( ))
, while -ge
is used within single brackets [ ]
or test
commands.
# Greater than or equal to with >=
if (( 5 >= 5 )); then
echo "5 is greater than or equal to 5"
fi
# Greater than or equal to with -ge
if [ 5 -ge 5 ]; then
echo "5 is greater than or equal to 5"
fi
Less than or equal to vs. -le
Using <=
and -le
:
The symbol <=
is used in arithmetic expressions within the double parentheses (( ))
, while -le
is used within single brackets [ ]
or test
commands.
# Less than or equal to with <=
if (( 3 <= 5 )); then
echo "3 is less than or equal to 5"
fi
# Less than or equal to with -le
if [ 3 -le 5 ]; then
echo "3 is less than or equal to 5"
fi
Using `if` Statements in Bash Scripting
The `if` statement in Bash allows you to execute commands based on conditions. Below are the different types of `if` statements, their syntax, and simple examples:
1. Basic `if` Statement
The basic `if` statement executes a block of code if a condition is true.
# Syntax
if [ condition ]; then
# Commands to execute if condition is true
fi
# Example
if [ 5 -gt 3 ]; then
echo "5 is greater than 3"
fi
2. `if-else` Statement
The `if-else` statement allows you to specify what to do if the condition is false.
# Syntax
if [ condition ]; then
# Commands to execute if condition is true
else
# Commands to execute if condition is false
fi
# Example
if [ 5 -gt 10 ]; then
echo "5 is greater than 10"
else
echo "5 is not greater than 10"
fi
3. `if-elif-else` Statement
The `if-elif-else` statement allows you to test multiple conditions in sequence.
# Syntax
if [ condition1 ]; then
# Commands to execute if condition1 is true
elif [ condition2 ]; then
# Commands to execute if condition2 is true
else
# Commands to execute if none of the conditions are true
fi
# Example
if [ 5 -gt 10 ]; then
echo "5 is greater than 10"
elif [ 5 -eq 5 ]; then
echo "5 is equal to 5"
else
echo "5 is neither greater than 10 nor equal to 5"
fi
4. Using Double Brackets `[[ ]]`
Double brackets `[[ ]]` allow for more advanced condition checking and are often used for string comparisons and logical operations.
# Syntax
if [[ condition ]]; then
# Commands to execute if condition is true
fi
# Example
if [[ 5 -gt 3 && 10 -lt 20 ]]; then
echo "5 is greater than 3 and 10 is less than 20"
fi
5. Using Parentheses `(( ))` for Arithmetic Tests
Parentheses `(( ))` are used for numeric comparisons and arithmetic operations.
# Syntax
if (( condition )); then
# Commands to execute if condition is true
fi
# Example
if (( 5 > 3 )); then
echo "5 is greater than 3"
fi
6. Using `test` Command
The `test` command (or its synonym `[ ]`) is used for checking file attributes and comparing strings.
# Syntax
if test condition; then
# Commands to execute if condition is true
fi
# Example
if test -e "example.txt"; then
echo "example.txt exists"
else
echo "example.txt does not exist"
fi
Additional Examples
Here are some extra examples showing different use cases for `if` statements:
Checking if a Directory Exists
# Check if a directory exists
if [ -d "mydirectory" ]; then
echo "Directory mydirectory exists"
else
echo "Directory mydirectory does not exist"
fi
Checking String Equality
# Check if two strings are equal
string1="hello"
string2="hello"
if [ "$string1" = "$string2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
Checking Multiple Conditions
# Check if a number is between two values
number=7
if [ $number -ge 5 ] && [ $number -le 10 ]; then
echo "Number is between 5 and 10"
else
echo "Number is not between 5 and 10"
fi
Using Loops in Bash Scripting
Loops in Bash allow you to execute a block of code multiple times. Below are the different types of loops available in Bash scripting, their syntax, and simple examples:
1. `for` Loop
The `for` loop iterates over a list of items and executes a block of code for each item.
# Syntax
for variable in list; do
# Commands to execute
done
# Example 1: Iterating over a list of numbers
for i in 1 2 3 4 5; do
echo "Number: $i"
done
# Example 2: Iterating over files in a directory
for file in *.txt; do
echo "Processing file: $file"
done
2. `while` Loop
The `while` loop repeatedly executes a block of code as long as a given condition is true.
# Syntax
while [ condition ]; do
# Commands to execute
done
# Example: Counting from 1 to 5
counter=1
while [ $counter -le 5 ]; do
echo "Counter: $counter"
((counter++)) # Increment counter
done
3. `until` Loop
The `until` loop repeatedly executes a block of code as long as a given condition is false.
# Syntax
until [ condition ]; do
# Commands to execute
done
# Example: Counting from 1 to 5 (similar to `while` but with `until`)
counter=1
until [ $counter -gt 5 ]; do
echo "Counter: $counter"
((counter++)) # Increment counter
done
4. `for` Loop with C-Like Syntax
This `for` loop is similar to loops in C programming and is useful for iterating with a numeric range.
# Syntax
for (( initialization; condition; increment )); do
# Commands to execute
done
# Example: Counting from 1 to 5
for (( i = 1; i <= 5; i++ )); do
echo "Number: $i"
done
5. Looping Through Array Elements
Loops can also be used to iterate over elements of an array.
# Syntax
array=("element1" "element2" "element3")
for item in "${array[@]}"; do
# Commands to execute
done
# Example: Iterating over an array of fruits
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
6. Nested Loops
Nested loops allow you to execute loops within loops. This is useful for working with multi-dimensional data.
# Syntax
for outer in list; do
for inner in list; do
# Commands to execute
done
done
# Example: Printing a multiplication table
for i in {1..3}; do
for j in {1..3}; do
echo "$i * $j = $((i * j))"
done
done
Additional Examples
Here are some extra examples showcasing different use cases for loops:
Using `for` Loop with Command Substitution
# Looping over the output of a command
for file in $(ls *.txt); do
echo "Text file found: $file"
done
Using `while` Loop with `read` Command
# Reading lines from a file and processing them
while IFS= read -r line; do
echo "Line: $line"
done < "file.txt"
Using `until` Loop with Arithmetic Operations
# Performing a task until a condition is met
number=10
until (( number == 0 )); do
echo "Countdown: $number"
((number--)) # Decrement number
done
Defining and Using Functions in Bash Scripting
Functions in Bash allow you to encapsulate a block of code and reuse it throughout your script. Below is a comprehensive guide on how to write functions, call them with and without parameters, and return values.
1. Defining a Function
Functions are defined using the `function` keyword or by simply using the function name followed by parentheses.
# Syntax
function function_name {
# Commands to execute
}
# Or
function_name() {
# Commands to execute
}
# Example: A simple function that prints a message
greet() {
echo "Hello, welcome to Bash scripting!"
}
# Calling the function
greet
2. Calling a Function with Parameters
You can pass parameters to a function and use them inside the function. Parameters are accessed using `$1`, `$2`, etc.
# Syntax
function_name() {
param1=$1
param2=$2
# Commands using $param1 and $param2
}
# Example: A function that greets a user by name
greet_user() {
local name=$1
echo "Hello, $name!"
}
# Calling the function with a parameter
greet_user "Alice"
3. Calling a Function Without Parameters
Functions can also be called without parameters. In this case, you can define the function without expecting any arguments.
# Example: A function that prints a fixed message
print_date() {
echo "Today's date is: $(date)"
}
# Calling the function without parameters
print_date
4. Returning Values from a Function
Functions in Bash can use the `return` keyword to set an exit status code, which is an integer value between 0 and 255. This is different from returning values via `echo`, which can be captured using command substitution.
# Syntax
function_name() {
# Commands
return 0 # Sets the exit status code
}
# Example: A function that calculates the square of a number and returns the status
square() {
local number=$1
local result=$((number * number))
echo $result
return 0 # Return status code (0 for success)
}
# Calling the function and capturing the returned value
result=$(square 5)
echo "The square of 5 is $result"
# Check the exit status
echo "Exit status: $?"
5. Example: Function with Multiple Parameters and Return Value
# Example: A function that calculates the sum of two numbers
add() {
local num1=$1
local num2=$2
echo $((num1 + num2))
return 0 # Return status code
}
# Calling the function with two parameters and capturing the result
sum=$(add 10 20)
echo "The sum of 10 and 20 is $sum"
# Check the exit status
echo "Exit status: $?"
6. Example: Function with Default Values
Functions can also use default values for parameters if none are provided.
# Example: A function that greets a user with a default name
greet_default() {
local name=${1:-"Guest"}
echo "Hello, $name!"
}
# Calling the function with and without a parameter
greet_default "Bob"
greet_default
Arrays in Bash Scripting
Arrays in Bash allow you to store and manage multiple values in a single variable. Unlike some other languages, Bash arrays can hold values of any type and can be dynamically resized.
1. Declaring Arrays
Arrays can be declared in several ways:
# Declaring an indexed array with values
my_array=(value1 value2 value3)
# Declaring an empty array
empty_array=()
# Declaring an associative array (Bash 4.0+)
declare -A assoc_array
2. Accessing Array Elements
Array elements are accessed using their indices. Bash arrays are zero-indexed.
# Accessing elements of an indexed array
echo ${my_array[0]} # Outputs: value1
echo ${my_array[1]} # Outputs: value2
# Accessing all elements
echo ${my_array[@]} # Outputs: value1 value2 value3
# Accessing the number of elements
echo ${#my_array[@]} # Outputs: 3
3. Modifying Array Elements
Array elements can be modified by directly assigning new values to specific indices.
# Modifying an element
my_array[1]="new_value2"
# Adding a new element
my_array[3]="value4"
4. Looping Through Array Elements
You can use loops to iterate over array elements.
# Loop through all elements using a for loop
for item in "${my_array[@]}"; do
echo $item
done
# Loop through array indices
for i in "${!my_array[@]}"; do
echo "Index $i: ${my_array[$i]}"
done
5. Associative Arrays
Associative arrays (or hash tables) are arrays where keys are strings, not numbers.
# Declaring an associative array
declare -A assoc_array
# Adding key-value pairs
assoc_array[username]="john_doe"
assoc_array[age]=30
# Accessing values
echo ${assoc_array[username]} # Outputs: john_doe
# Loop through associative array keys and values
for key in "${!assoc_array[@]}"; do
echo "$key: ${assoc_array[$key]}"
done
6. Multi-Dimensional Arrays
Bash does not support multi-dimensional arrays directly, but you can emulate them using associative arrays.
# Example of emulating a 2D array
declare -A matrix
# Setting values
matrix["0,0"]="1"
matrix["0,1"]="2"
matrix["1,0"]="3"
matrix["1,1"]="4"
# Accessing values
echo ${matrix["0,1"]} # Outputs: 2
# Looping through simulated 2D array
for row in 0 1; do
for col in 0 1; do
echo "Value at [$row,$col]: ${matrix[$row,$col]}"
done
done
7. Unsetting Array Elements
Array elements can be removed using the `unset` command.
# Unsetting a specific element
unset my_array[1]
# Unsetting the entire array
unset my_array
8. Example: Working with Arrays
# Example script demonstrating various array operations
#!/bin/bash
# Indexed array
colors=("red" "green" "blue")
echo "First color: ${colors[0]}" # Outputs: red
colors[1]="yellow"
echo "All colors: ${colors[@]}"
# Associative array
declare -A person
person[firstname]="Alice"
person[lastname]="Smith"
echo "Full name: ${person[firstname]} ${person[lastname]}"
# Loop through associative array
for key in "${!person[@]}"; do
echo "$key: ${person[$key]}"
done
# Multi-dimensional array simulation
declare -A table
table["0,0"]="A1"
table["0,1"]="A2"
table["1,0"]="B1"
table["1,1"]="B2"
echo "Cell [1,0]: ${table[1,0]}"
# Unset an element
unset colors[2]
echo "Colors after unset: ${colors[@]}"
File Handling in Bash Scripting
Bash scripting provides various commands and operators for file handling. This section covers the basics of creating, reading, writing, appending, and deleting files, as well as other common file operations.
1. Creating Files
Files can be created using several methods:
# Create an empty file using touch
touch filename.txt
# Create a file and write content using the > operator
echo "This is a new file." > newfile.txt
# Create a file and write content using the cat command
cat > anotherfile.txt <
2. Reading Files
Reading files can be done using commands like `cat`, `more`, `less`, and `head`:
# Display the entire file content
cat filename.txt
# View the file content page by page
more filename.txt
# View the file content with scrollable pages
less filename.txt
# Display the first 10 lines of the file
head filename.txt
# Display the last 10 lines of the file
tail filename.txt
3. Writing to Files
Files can be written or overwritten using the `>` operator or appended using the `>>` operator:
# Overwrite or create a new file
echo "New content" > file.txt
# Append content to an existing file
echo "Additional content" >> file.txt
4. Deleting Files
Files can be deleted using the `rm` command:
# Delete a file
rm filename.txt
# Delete multiple files
rm file1.txt file2.txt
5. Renaming Files
Files can be renamed using the `mv` command:
# Rename a file
mv oldname.txt newname.txt
# Move a file to a different directory (effectively renaming)
mv file.txt /path/to/directory/newfile.txt
6. Copying Files
Files can be copied using the `cp` command:
# Copy a file to a new location
cp file.txt /path/to/directory/
# Copy a file and rename it
cp file.txt /path/to/directory/copiedfile.txt
7. Checking File Existence
To check if a file exists, you can use the `-e` option in conditional statements:
# Check if a file exists
if [ -e filename.txt ]; then
echo "File exists."
else
echo "File does not exist."
fi
8. File Permissions
File permissions can be changed using the `chmod` command:
# Change file permissions to read and write for the owner, and read-only for others
chmod 644 file.txt
# Make a script executable
chmod +x script.sh
9. File Ownership
File ownership can be changed using the `chown` command:
# Change the owner and group of a file
chown username:groupname file.txt
10. Example: File Handling Script
# Example script demonstrating various file operations
#!/bin/bash
# Create a file
echo "Creating a file..."
touch myfile.txt
# Write to the file
echo "Writing to the file..." > myfile.txt
echo "Hello, World!" >> myfile.txt
# Read the file
echo "Reading the file..."
cat myfile.txt
# Append more content
echo "Appending more content..."
echo "Goodbye, World!" >> myfile.txt
# Check if file exists
if [ -e myfile.txt ]; then
echo "File exists."
else
echo "File does not exist."
fi
# Rename the file
mv myfile.txt renamedfile.txt
# Copy the file
cp renamedfile.txt copyfile.txt
# Delete the file
rm renamedfile.txt
rm copyfile.txt
echo "File operations completed."
Examples of Bash Scripting
Example 1: Hello World
Prints "Hello, World!" to the terminal.
#!/bin/bash
echo "Hello, World!"
Example 2: Add Two Numbers
Adds two numbers and prints the result.
#!/bin/bash
num1=10
num2=5
sum=$((num1 + num2))
echo "Sum: $sum"
Example 3: Check User Input
Prompts the user for input and displays it.
#!/bin/bash
echo "Enter your name: "
read name
echo "Hello, $name"
Example 4: For Loop
Iterates over a list of items using a for loop.
#!/bin/bash
for item in apple banana orange
do
echo "Fruit: $item"
done
Example 5: While Loop
Executes a block of code repeatedly using a while loop.
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
Example 6: Conditional Statement
Checks if a number is greater than 10.
#!/bin/bash
num=15
if [ $num -gt 10 ]
then
echo "Number is greater than 10"
else
echo "Number is not greater than 10"
fi
Example 7: Function
Defines and calls a function.
#!/bin/bash
say_hello() {
echo "Hello, world!"
}
say_hello
Example 8: Read File Contents
Reads and displays the contents of a file.
#!/bin/bash
filename="example.txt"
while IFS= read -r line
do
echo "$line"
done < "$filename"
Example 9: Check File Existence
Checks if a file exists.
#!/bin/bash
filename="example.txt"
if [ -e "$filename" ]
then
echo "File exists"
else
echo "File does not exist"
fi
Example 10: Create Directory
Creates a directory if it does not exist.
#!/bin/bash
directory="new_directory"
if [ ! -d "$directory" ]
then
mkdir "$directory"
echo "Directory created"
else
echo "Directory already exists"
fi
Example 11: Delete File
Deletes a file if it exists.
#!/bin/bash
filename="example.txt"
if [ -f "$filename" ]
then
rm "$filename"
echo "File deleted"
else
echo "File does not exist"
fi
Example 12: Check Command Output
Checks the output of a command.
#!/bin/bash
output=$(ls)
echo "$output"
Example 13: Execute Command
Executes a command and captures its output.
#!/bin/bash
result=$(date)
echo "Current date and time: $result"
Example 14: Calculate File Size
Calculates the size of a file in bytes.
#!/bin/bash
filename="example.txt"
size=$(stat -c %s "$filename")
echo "File size: $size bytes"
Example 15: Concatenate Strings
Concatenates two strings.
#!/bin/bash
string1="Hello,"
string2="world!"
result="$string1 $string2"
echo "$result"
Example 16: Replace String
Replaces a string in a variable.
#!/bin/bash
text="Hello, world!"
new_text="${text/world/planet}"
echo "$new_text"
Example 17: Reverse String
Reverses a string.
#!/bin/bash
text="Hello, world!"
reversed=$(rev <<< "$text")
echo "$reversed"
Example 18: Check Internet Connection
Checks if the system has an active internet connection.
#!/bin/bash
if ping -q -c 1 -W 1 google.com >/dev/null; then
echo "Internet is up"
else
echo "Internet is down"
fi
Example 19: Generate Random Number
Generates a random number between 1 and 100.
#!/bin/bash
random=$(shuf -i 1-100 -n 1)
echo "Random number: $random"
Example 20: Check System Uptime
Displays the system uptime.
#!/bin/bash
uptime=$(uptime -p)
echo "System uptime: $uptime"