Skip to content

Commit a3e70db

Browse files
committed
Added crash course for bash scripting
Signed-off-by: Aakarsh MJ <[email protected]>
1 parent 2ec9274 commit a3e70db

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Crash Course/cheatsheet.sh

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#! /usr/bin/bash
2+
3+
#echo "Hello World!"
4+
# echo Hello World
5+
6+
# Creating Variables
7+
# Syntax:
8+
# a) Capital letters
9+
# b) alphabets, numbers and underscores including no-spaces
10+
# c) no spaces around the equal sign
11+
# NAME="Jerry"
12+
13+
# Using Variables
14+
# Syntax: $variable_name or ${variable_name}
15+
# echo My name is $NAME
16+
# echo ${NAME} is a KDE developer
17+
18+
# simple if statement
19+
# if [ "$NAME" == "Aakarsh" ]
20+
# then
21+
# echo Your name is $NAME
22+
# fi
23+
24+
# If-else statement
25+
# if [ "$NAME" == "Aakarsh" ]
26+
# then
27+
# echo Your name is Aakarsh
28+
# else
29+
# echo Yout name is not Aakarsh
30+
# fi
31+
32+
# else-if statement
33+
# if [ "$NAME" == "Aakarsh" ]
34+
# then
35+
# echo Your name is Aakarsh
36+
# elif [ "$NAME" == "Abhilash" ]
37+
# then
38+
# echo Your name is Abhilash
39+
# else
40+
# echo Your name is neither Aakarsh or Abhilash
41+
# fi
42+
43+
# COMPARISIONS
44+
# val1 -eq val2 means it checks if val1 is equal to val2
45+
# val1 -ne val2 means it checks if val1 is not equal to val2
46+
# val1 -gt val2 means it checks if val1 is greater than val2
47+
# val1 -ge val2 means it checks if val1 is greater than or equal to val2
48+
# val1 -lt val2 means it checks if val1 is lesser than val2
49+
# val1 -le val2 means it checks if val1 is less than or equal to val2
50+
51+
# NUM1=31
52+
# NUM2=5
53+
54+
# if [ "$NUM1" -eq "$NUM2" ]
55+
# then
56+
# echo both numbers are equal
57+
# else
58+
# echo both numbers are not equal
59+
# fi
60+
61+
# if [ "$NUM1" -gt "$NUM2" ]
62+
# then
63+
# echo $NUM1 is greater than $NUM2
64+
# else
65+
# echo $NUM1 is less than $NUM2
66+
# fi
67+
68+
# File conditions
69+
# -d <file> True if the given string <file> is a directory
70+
# -f <file> True if the given string <file> is a file
71+
# -e <file> True if the given file exists or not but this is not portable and -f is heavily used instead
72+
# -g <file> True if group id is set on the given file
73+
# -u <file> True if user id is set on the given file
74+
# -r <file> True if the given file is readable
75+
# -w <file> True if the given file is writable
76+
# -x <file> True if the given file is executable
77+
# -s <file> True if the file has non zero size
78+
79+
# FILE="test.txt"
80+
# if [ -w "$FILE" ]
81+
# then
82+
# echo The given directory is writeable
83+
# else
84+
# echo The given directory is NOT writable
85+
# fi
86+
87+
# Case statements
88+
# each condition statement ends with ')'
89+
# instead of break we make use of ';;'
90+
# to make use of default make use of '*)' => break down into '*' and ')', latter is used to end the case statement
91+
92+
# read -p "Are you 21 or older: " ANSWER
93+
# case "$ANSWER" in
94+
# [yY] | [yY][eE][sS])
95+
# echo "You may have a drink"
96+
# ;;
97+
# [nN] | [nN][oO])
98+
# echo "You cannot have a drink"
99+
# ;;
100+
# *)
101+
# echo "Please reply with a y/yes or n/no"
102+
# ;;
103+
# esac
104+
105+
# Loops
106+
# Simple For loop
107+
# NAMES="Aakarsh Achintya Manik Tushar"
108+
# for NAME in $NAMES
109+
# do
110+
# echo $NAME
111+
# done
112+
113+
# Using for loops to rename files
114+
# FILES=$(ls *.txt)
115+
# for FILE in $FILES
116+
# do
117+
# mv $FILE "NEW"-$FILE
118+
# done
119+
120+
# LINE=1
121+
# while read -r CURRENT_LINE
122+
# do
123+
# echo $LINE: $CURRENT_LINE
124+
# ((LINE++))
125+
# done < "1.txt"
126+
127+
# Functions
128+
# functions with no parameters
129+
# function helloworld() {
130+
# echo "Hello World"
131+
# }
132+
133+
# helloworld
134+
135+
# #Functions with parameters
136+
# function greeting() {
137+
# echo Hello $1 and you are assigned age $2
138+
# }
139+
140+
# greeting "Aakarsh" 23

0 commit comments

Comments
 (0)