0% found this document useful (0 votes)
3 views2 pages

Shell Scripting Cheat Sheet Detailed

This document is a detailed cheat sheet for shell scripting, covering topics such as script creation, basic syntax, variables, conditionals, loops, file handling, and string operations. It includes examples of common programs like palindrome checks, reversing numbers, and calculating factorials. The guide serves as a quick reference for automating tasks in a Linux environment.

Uploaded by

infinxrealone
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)
3 views2 pages

Shell Scripting Cheat Sheet Detailed

This document is a detailed cheat sheet for shell scripting, covering topics such as script creation, basic syntax, variables, conditionals, loops, file handling, and string operations. It includes examples of common programs like palindrome checks, reversing numbers, and calculating factorials. The guide serves as a quick reference for automating tasks in a Linux environment.

Uploaded by

infinxrealone
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

■ Shell Scripting Cheat Sheet (Detailed Guide)

1■■ Introduction to Shell Scripting


A shell script is a file with a series of commands executed by the Linux shell.
It automates tasks such as file handling, loops, and condition checks.

To create and run a script:


nano [Link]
#!/bin/bash
chmod +x [Link]
./[Link]

2■■ Basic Syntax & Variables


name="Param"
echo $name
read name
a=5; b=3
echo $((a + b))

3■■ Conditionals
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi

Common Operators
-eq (equal), -ne (not equal), -gt (greater), -lt (less), -ge, -le
=, !=, -z (empty string)
-f (file), -d (directory), -r, -w, -x (permissions)

4■■ Loops
For Loop:
for i in 1 2 3; do echo $i; done

While Loop:
i=1
while [ $i -le 5 ]; do echo $i; i=$((i+1)); done

5■■ File Handling


cat, touch, rm, cp, mv, ls, wc, grep
File Tests: -e (exists), -f (file), -d (dir), -r, -w, -x, -s (not empty)
6■■ String Operations
${#str} (length)
${str:2:4} (substring)
echo $str | rev (reverse)
${str,,} (lowercase)
${str^^} (uppercase)

7■■ Common Programs


Palindrome:
read s; r=$(echo $s | rev); if [ "$s" = "$r" ]; then echo Palindrome; else echo
Not; fi

Reverse Number:
read n; rev=0; while [ $n -gt 0 ]; do r=$((n%10)); rev=$((rev*10+r));
n=$((n/10)); done; echo $rev

Even/Odd:
read n; if [ $((n%2)) -eq 0 ]; then echo Even; else echo Odd; fi

Factorial:
read n; f=1; for((i=1;i<=n;i++)); do f=$((f*i)); done; echo $f

File Exists:
read f; if [ -e $f ]; then echo Exists; else echo Missing; fi

Line Count:
read f; wc $f

Largest of 3:
read a b c; if [ $a -ge $b ] && [ $a -ge $c ]; then echo $a; elif [ $b -ge $c ];
then echo $b; else echo $c; fi

Sum of Digits:
read n; s=0; while [ $n -gt 0 ]; do r=$((n%10)); s=$((s+r)); n=$((n/10)); done;
echo $s

Fibonacci:
read n; a=0; b=1; for((i=0;i
Empty File Check:
read f; if [ ! -s $f ]; then echo Empty; else echo Not empty; fi

You might also like