Shell Scripts

color=red
echo color
echo $color
 

man test

to get the exit code of the last command

  • to do the calculations in bash you have to give $((calculation))

  • put -x infront of a bash script to get debug mode

for loop

for i in nfs mountd rpc-bind; do firewall-cmd --add-service $i --permanent; done
 
for i in hello there can; do echo $i ; done

dnf install -y nfs-utils

mkdir -p /nfsdata /home/ldap/ldapuser{1..9}

echo "/nfsdata *(rw,no_root_squash)" >> /etc/exports
echo "/home/ldap *(rw,no_root_squash)" >> /etc/exports
systemctl enable --now nfs-server

for i in nfs mountd rpc-bind; do firewall-cmd --add-service $i --permanent; done

firewall-cmd --reload



Link to original

Practicing shell scripting is a great way to enhance your skills in automation and Linux administration. Here’s a structured approach to help you get started:


Step 1: Setup

  1. Environment: Use a Linux-based system (e.g., Ubuntu, CentOS, or Red Hat). If you’re on Windows, you can use WSL (Windows Subsystem for Linux) or a virtual machine.
  2. Editor: Use a text editor like vim, nano, or gedit. Alternatively, Visual Studio Code with a shell extension works well.
  3. Interpreter: Shell scripts usually start with a “shebang” line to specify the interpreter. Use #!/bin/bash for Bash scripts.

Step 2: Basics

1. Hello World Script

#!/bin/bash
echo "Hello, World!"
  • Save as hello.sh.
  • Run with:
    chmod +x hello.sh
    ./hello.sh

2. Variables and Comments

#!/bin/bash
# This is a comment
name="Srinath"
echo "Hello, $name!"

3. Input and Output

#!/bin/bash
read -p "Enter your name: " user_name
echo "Welcome, $user_name!"

4. Conditionals

man test
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
  echo "The number is greater than 10."
else
  echo "The number is less than or equal to 10."
fi

5. Loops

#!/bin/bash
for i in {1..5}; do
  echo "Iteration $i"
done

Step 3: Intermediate Concepts

1. Functions

 
echo $0 #filename
echo $1 #first argument for the file
echo $2 #second arguemtn for the file
echo $@ #whole argument list as a string
#!/bin/bash
function greet() {
  echo "Hello, $1!" #first argument for the function
  echo $2 #second arguemtn for the function
 
}
greet "Srinath"

2. File Operations

#!/bin/bash
echo "Creating a file named test.txt"
touch test.txt
echo "This is a test file." > test.txt
cat test.txt

3. Command-Line Arguments

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

Run with:

./script.sh arg1 arg2

Step 4: Advanced Concepts

1. Error Handling

#!/bin/bash
file="nonexistent.txt"
if [ -e $file ]; then
  echo "$file exists."
else
  echo "$file does not exist."
fi

2. Cron Jobs

  • Schedule your script to run automatically.
  • Example: crontab -e to schedule ./backup.sh every day at 3 AM:
    0 3 * * * /path/to/backup.sh

3. Advanced Loops and Arrays

#!/bin/bash
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"; do
  echo "I like $fruit"
done

4. Debugging

  • Add set -x at the beginning of your script to debug it.

Step 5: Practice Projects

  1. System Monitoring Script: Display CPU, memory, and disk usage.
  2. Backup Script: Automate file or database backups.
  3. User Management Script: Add, delete, and manage system users.
  4. Log Analysis: Parse logs and extract information.
  5. File Organizer: Move files into directories based on their extensions.

Resources