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 ; doneLink to originaldnf 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
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
- 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.
 - Editor: Use a text editor like 
vim,nano, orgedit. Alternatively, Visual Studio Code with a shell extension works well. - Interpreter: Shell scripts usually start with a “shebang” line to specify the interpreter. Use 
#!/bin/bashfor 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."
fi5. Loops
#!/bin/bash
for i in {1..5}; do
  echo "Iteration $i"
doneStep 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 arg2Step 4: Advanced Concepts
1. Error Handling
#!/bin/bash
file="nonexistent.txt"
if [ -e $file ]; then
  echo "$file exists."
else
  echo "$file does not exist."
fi2. Cron Jobs
- Schedule your script to run automatically.
 - Example: 
crontab -eto schedule./backup.shevery 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"
done4. Debugging
- Add 
set -xat the beginning of your script to debug it. 
Step 5: Practice Projects
- System Monitoring Script: Display CPU, memory, and disk usage.
 - Backup Script: Automate file or database backups.
 - User Management Script: Add, delete, and manage system users.
 - Log Analysis: Parse logs and extract information.
 - File Organizer: Move files into directories based on their extensions.
 
Resources
- Books:
- “Linux Shell Scripting Cookbook”
 - “The Linux Command Line”
 
 - Online Platforms:
 - Practice Sites: