In part 1 of this blog series, We have seen how to work on essential Linux commands. In part 2, We will assist you to understand the basic logic and loop format while creating your first bash script.
Prerequisites: I am hoping you have basic knowledge and experience with Linux.
- Use scripting to automate system maintenance tasks
Scripting: #! Shebang - #!/bin/bash , # comment a line or text
- Conditional Logic(if)
IF Example:
if [condition block]
then
Command section
elif [condition block]
then
Command section
else
Command section
fi
Conditional Operators
-Processing shell command exit codes
Exit Code: run 'echo $?' post running any command to know the exit code
- Use Looping constructs (for, etc.) to process file, command line input
For Loop: Perform repetitive tasks
Example:
for mission in $(cat mission-names.txt)
do
create-and-launch-rocket $mission
done
while Loop usage: wait in the loop until specific condition matches/create infinite loops/Menu driven programs
Example:
while [$rocket_status = "launching"]
do
sleep 2
rocket_status = rocket-status $mission_Name
done
No comments:
Post a Comment