1. What is a SHELL SCRIPT?
a. shell script it made up of a set of commands to
perform a specific task.
b. all the commands execute sequentially
c. some tasks like file manipulation, program
execution, user interaction, automation of task etc
can be done
2. First basic script
#!/bin/bash
echo "Hello World"
3. What is a Shebang?
a. the first line of the shell script (optional if
your shell is bash)
b. makes another shell compatible with bash
4. How to run a script:
5. Comments
6. Variables
Constant Variable
7. Arrays
NOTE: space separated values hoti hain array mein.
arrayek variable hai jisme multiple values store karte
hain hum
Array key-value
8. String Operations
9. Getting value from a user
10. Arithmetic Operations
How to use expressions
11. IF-ELSE
12. OPERATORS
13. ElIF
14. CASE
Jo aap enter karoge choice mein wo hi ) ke left mein
rakhna.
15. LOGICAL OPERATORS
string comparison is done by using = and not -eq.
Combine condition1 and condition2
TERNARY OPERATOR
16. LOOPS
FOR:
NOTE: Never use backticks for FILE PATHS in linux.
eg: FILE=`/home/aditya/file` because backticks are for
an arithmetic operation and not for path assignment.
17. WHILE LOOPS
18. UNTIL LOOP
Q: Why do we even use the until loop when we have a
while loop?
A:
19. INFINITE LOOP
Q: How to stop an infinite loop?
A: Ctrl + C
20. While Loop with File
Use NR!=1
cat file.csv | awk -F ',' 'NR!=1{print}'
21. FUNCTIONS
call the function welcomeNote three times
22. Argument passing in Shell Scripting
Using for-loop to print all the arguments entered using
the command line argument
SHIFTING THE ARGUMENTS
23. BREAK AND CONTINUE
BREAK
CONTINUE
SLEEP & EXIT
SLEEP
EXIT
24. CONNECTIVITY SCRIPT
CHECK IF FILE DIR EXIST
agar script file dependant hai ya directory dependant
hai to we use the file/dir check
Agar file exist nahi karti hai to use bana do
Aapke code mein kuch errors hain, particularly where
you are trying to use basename directly within the cat
and echo commands. basename ko direct use nahi kiya ja
sakta bina command substitution ke.
25. BASH VARIABLES
RANDOM
UID
UID of Root is 0
RANDOM NUMBER GENERATION
ROOT USER CHECK
26. DEV/NULL in SHELL SCRIPT
Idhar jo $site &> /dev/null means ki jo beech mein
dikhati hai na ping command, pura process packet loss
wagerah to wo sab show nahi hoga idhar, direct hamara
output ayega.
kaam ki cheez print hogi baaki nahin hogi
PRINT THE NAME OF THE SCRIPT
27. LOGGING IN SHELL
28. DEBUGGING SCRIPTS
cd /root pe permission denied hogi to set -e hone ke
falswaroop ye file wahi ruk jayegi
RUNNING SCRIPT IN THE BACKGROUND
sleep lagake let’s say humne loop print kiya to jab tak
sari values iterate nahin ho jaati tab tak wo hog kar
lega terminal ko aur aap koi dusra kaam nahi kar sakte
so it is a waste of time and resources, so run this in
background.
- script terminal se judi hui hai, terminal band ho
gaya to apki script chalti rehni chahiye.
use: nohup ./script
29. AT & CRONTAB (automate your script, REDUCE manual
interference)
at command tab use hota hai jab tumhe ek specific
time pe ek hi baar koi command ya script run karni
ho. Jaise tumne bola, ek script ek baar chalani hai,
to at perfect hai uske liye. 🕒
cron command ya crontab tab use hota hai jab tumhe
koi command ya script baar-baar, regular intervals pe
chalani ho. Jaise har din, har hafte, ya har mahine,
crontab us case mein kaafi useful hota hai. 📅🔄
atq se aap jobs dekhte ho jo scheduled hain
agar at kisi date pe chalani hai to:
CRONTAB: