0% found this document useful (0 votes)
7 views3 pages

Shell Scripting Cheatsheet Guide

This document is a comprehensive cheatsheet for shell scripting, covering essential commands for file management, input/output redirection, conditional statements, loops, and functions. It also includes information on handling signals, automatic script execution, text processing tools like sed and awk, and shell descriptions for Dash and Zsh. Additionally, it provides examples of string manipulation and database interaction within scripts.

Uploaded by

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

Shell Scripting Cheatsheet Guide

This document is a comprehensive cheatsheet for shell scripting, covering essential commands for file management, input/output redirection, conditional statements, loops, and functions. It also includes information on handling signals, automatic script execution, text processing tools like sed and awk, and shell descriptions for Dash and Zsh. Additionally, it provides examples of string manipulation and database interaction within scripts.

Uploaded by

rahul21042005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Shell Scripting Exam Cheatsheet — One Page Edition

1. Listing Files and Directories


Used to view files, directories, and their details.
ls # List files
ls -l # Long format
ls -a # Show hidden files
pwd # Show current path
tree # Display directory structure

2. Managing Files and Directories


Commands to create, move, copy, rename, or delete files/folders.
mkdir dir1 # Create directory
rmdir dir1 # Remove empty directory
rm -r dir1 # Delete directory with contents
cp f1 f2 # Copy file
mv f1 newname # Move or rename
touch f1 # Create empty file
rm f1 # Remove file

3. Input and Output Redirection


Redirect command input/output to files or between commands.
cmd > file # Redirect output
cmd >> file # Append output
cmd < file # Input from file
cmd1 | cmd2 # Pipe output to next command

4. If, Then, and Nested If Statements


Used for conditional execution in scripts.
if [ condition ]; then
commands
elif [ condition ]; then
commands
else
commands
fi

5. Test Command Classes


Used to evaluate string, numeric, or file conditions.
[ "$a" = "$b" ] # String comparison
[ $a -gt $b ] # Numeric comparison
[ -f file ] # File exists/type check

6. Looping Statements (for, until, while)


Used to repeat commands until a condition changes.
for i in 1 2 3; do echo $i; done
while [ $n -le 5 ]; do echo $n; ((n++)); done
until [ $n -gt 5 ]; do echo $n; ((n++)); done
7. Passing Parameters
Send arguments to a script while running it.
#!/bin/bash
echo "1st: $1, 2nd: $2"
echo "All: $@"
echo "Count: $#

8. Tracking Parameters
Special variables to track arguments and process info.
$0 # Script name
$1 # First argument
$@ # All arguments
$# # No. of arguments
$$ # Script PID
$? # Exit status

9. Handling Signals
Trap system signals (like Ctrl+C) to handle them gracefully.
trap "echo 'Ctrl+C pressed!'; exit" SIGINT

10. Automatic Script Execution


Run scripts automatically using schedulers (cron/at).
crontab -e
0 17 * * * /path/[Link] # Run daily at 5PM
at 17:00 -f [Link] # One-time run

11. Creating and Using Functions


Reusable code blocks to organize scripts better.
greet() { echo "Hello $1"; }
greet "Rahul

12. Recursive Function


A function that calls itself until a condition is met.
fact() {
if [ $1 -le 1 ]; then echo 1
else echo $(( $1 * $(fact $(( $1 - 1 )) ) ))
fi
}
fact 5

13. Arrays and Variables in Functions


Store multiple values and use local variables inside functions.
arr=(10 20 30)
echo ${arr[0]}
func() { local x=5; echo "x=$x"; }

14. sed and gawk Editors


Text processing tools for searching, replacing, and formatting data.
sed 's/old/new/g' file
awk '{print $1, $3}' file
15. Regular Expressions
Patterns to match and filter text strings.
grep "^R" file
grep "[0-9]$" file

16. Multiline Commands


Used to write long commands or blocks of text.
echo "This is \
a long line"
cat <<EOF
Hello Rahul
Multi-line text
EOF

17. Reexamining gawk


gawk uses patterns and actions to process fields/records.
awk '{print NR, $0}' file

18. Using Variables in gawk


Pass shell variables into awk for dynamic processing.
awk -v name="Rahul" '{print name, $1}' file

19. Describe Dash Shell


Lightweight POSIX-compliant shell — fast, minimal, and efficient.
Default /bin/sh in Ubuntu
No arrays or advanced bash features

20. Describe Zsh Shell


An advanced shell with themes, plugins, and smart features.
Auto-suggestions, spell check, Oh-My-Zsh customization
Bash-compatible and user-friendly

21. Writing Database Shell Script


Interact with databases (like SQLite or MySQL) inside a script.
sqlite3 [Link] "CREATE TABLE users(id INT, name TEXT);"
sqlite3 [Link] "INSERT INTO users VALUES(1,'Rahul');"
sqlite3 [Link] "SELECT * FROM users;

22. String Manipulation


Modify, slice, or transform string variables.
name="RahulM"
echo ${#name}
echo ${name:0:3}
echo ${name/M/mona}
echo ${name^^}
echo ${name,,}

You might also like