KEMBAR78
Shell Scripting Full | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
31 views8 pages

Shell Scripting Full

Uploaded by

Arush A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

Shell Scripting Full

Uploaded by

Arush A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Shell Scripting: Complete Guide with Theory

Table of Contents
1. Introduction to Shell Scripting
2. Shells in Linux
3. Bash: The Most Popular Shell
4. Getting Started with Shell Scripts
5. Variables and Data Types
6. Input and Output
7. Expressions and Operators
8. Control Statements (Conditionals and Loops)
9. Functions
10. Arrays
11. File Operations and Permissions
12. Text Processing (grep, sed, awk, regex)
13. Environment Variables
14. Debugging Shell Scripts
15. Advanced Concepts: Automation, Scheduling, Error Handling
16. Common Examples and Scripts
17. Best Practices in Shell Scripting
18. References

1. Introduction to Shell Scripting


Shell scripting is a way to program shells, allowing users to automate repetitive tasks, handle
files, process text, and control systems. Scripts are text files containing commands executed by
the shell.[3][4][5][10]

2. Shells in Linux
Popular shells include:
Bash (Bourne Again Shell)
CSH (C shell)
KSH (Korn shell)
ZSH (Z shell)
Each shell has its own syntax and features.[3]

3. Bash: The Most Popular Shell


Bash is widely used for scripting due to its powerful features and Linux compatibility. Scripts
written for Bash start with a "shebang":

#!/bin/bash

This indicates the interpreter for the script.[4][9][10]

4. Getting Started with Shell Scripts


Create a new file, e.g. myscript.sh
Start with the shebang.
Write your commands or logic.
Save the file and make it executable:

chmod +x myscript.sh
./myscript.sh

[4][9]
Example:

#!/bin/bash
echo "Hello, World!"

5. Variables and Data Types


Variables store values. In Bash, variables are strings by default.

name="John Doe"
echo $name

To read user input:

read name
echo "Hello, $name."

Command substitution:
date_today=$(date)

[17][4]

6. Input and Output


Read input using read.
Print output with echo.
Use command-line arguments ($1, $2...)

echo "Hello, $1!"

Redirect output:

echo "Text" > output.txt

[4]

7. Expressions and Operators


Arithmetic expression:

a=5
b=10
sum=$((a + b))
echo $sum

Comparison operators:
-eq: Equal
-ne: Not equal
-lt: Less than
-le: Less equal
-gt: Greater than
-ge: Greater equal
[3][12][21][18][23]
8. Control Statements (Conditionals and Loops)

Conditionals
if-else Statement:

if [ $a -eq $b ]; then
echo "Equal"
else
echo "Not equal"
fi

elif Statement: Multiple conditions

if [ $a -gt $b ]; then
echo "a > b"
elif [ $a -lt $b ]; then
echo "a < b"
else
echo "a == b"
fi

case Statement: Switch Alternative

case $var in
1) echo "One";;
2) echo "Two";;
*) echo "Other";;
esac

[12][15][18][23]

Loops
for Loop:

for i in 1 2 3; do
echo $i
done

while Loop:

count=1
while [ $count -le 5 ]; do
echo $count
((count++))
done
[4]

9. Functions
Define reusable blocks of code.

function say_hello() {
echo "Hello, $1!"
}
say_hello "Alice"

[17][20]

10. Arrays
Store multiple values in one variable.

arr=("apple" "banana" "cherry")


echo ${arr[0]}
for fruit in "${arr[@]}"; do
echo $fruit
done

[14][11][20]

11. File Operations and Permissions


Check file permissions:

if [ -r "file.txt" ]; then echo "Readable"; fi


if [ -w "file.txt" ]; then echo "Writable"; fi
if [ -x "file.txt" ]; then echo "Executable"; fi

Change permissions:

chmod +x file.sh

Numeric representations:
755: Owner can read/write/execute; group/others can read/execute
644: Owner read/write; others/group read
[25][28][31][34]
12. Text Processing (grep, sed, awk, regex)
grep: Search text by pattern
sed: Stream editing
awk: Pattern scanning and processing
Use pipes | to combine commands:

grep "pattern" file.txt | awk '{print $2}' | sed 's/old/new/'

Regular expressions allow powerful pattern matching in all three tools.


[24][27][30][33]

13. Environment Variables


Standard variables like PATH, HOME can be viewed and set:

echo $PATH
export PATH=$PATH:/new/path
echo $HOME

Permanent changes can be added to ~/.bashrc or ~/.profile:

echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bash_profile

[26][29][32]

14. Debugging Shell Scripts


Use set -x to show command execution
Insert echo statements to show variable states
Use conditionally triggered debug messages
Tools like ShellCheck assist with static analysis
Example:

set -x
# your code
set +x

[13][16][19][22]
15. Advanced Concepts: Automation, Scheduling, Error Handling
Automation: Use scripts for backups, reports, system monitoring
Scheduling: Use cron to run scripts on schedule
Error Handling: Use exit codes ($?), conditionals, try/catch via functions

16. Common Examples and Scripts


Print name:

echo "Hello, $1!"

Check even/odd:

if ((num % 2 == 0)); then echo "Even"; else echo "Odd"; fi

Grade evaluation:

if [ $marks -ge 90 ]; then


echo "Grade: A"
elif [ $marks -ge 75 ]; then
echo "Grade: B"
else
echo "Grade: C"
fi

[9][15][21]

17. Best Practices in Shell Scripting


Use comments for code clarity
Use functions to modularize logic
Check for errors and exit codes
Keep scripts readable and maintainable

18. References
Bash Guide for Beginners (The Linux Documentation Project)[5]
GeeksforGeeks Shell Scripting Articles[3][9][12][14][17][21][31]
FreeCodeCamp Bash Scripting Tutorial[4]
The Shell Scripting Tutorial[10]
Cyberciti, W3Schools (for permissions and environment variables)[25][28][26][29]
This PDF serves as a compact, structured reference for all major components of shell
scripting, suitable for beginners and intermediate users.

You might also like