Welcome, DevOps enthusiasts! We're diving into Linux shell scripting for DevOps Engineers. π§π§π
πΉ What is Shell Scripting? Write your commands on black screen, shell is also called as terminal. A shell is a special user program which provides an interface for user to use operating system services. Shell accepts human readable commands from user and converts them into something which kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal. π₯οΈ
πΉWhat is Linux Shell Scripting? A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages.
Getting Started To begin, open your favorite terminal π₯οΈ. Most Linux distributions come with Bash (Bourne Again SHell), a popular shell. Type echo $SHELL
to verify if you're using Bash.
πΉWhat is #!/bin/bash? The line #!/bin/bash
is called a "shebang" (also known as a "hashbang" or "pound-bang"). It is a special construct used in Unix-like operating systems.
πΉ !#/bin/bash vs !#/bin/sh
If you are using features that are only available on bash
you are better with !#/bin/bash
. If you are distributing your shell scripts publicly, you can use !#/bin/sh
as in most modern unix operating systems, this will be a symbolic link to the default system shell. In Ubuntu, it defaults to bash.
πΉ Creating Your First Script π Let's create a basic script to print "I will complete #90DaysOfDevOps challenge!". Using a text editor like vim
, create a new file named hello.sh
write 'i' to insert and write the following lines:
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge!"
Save the file using ":wq!", and in the terminal, Execute hello.sh.
#Run/Execute hello.sh
bash hello.sh
In the terminal, you'll see the message output! "I will complete #90DaysOfDevOps challenge!"π.
πΉ Write a Shell Script to take user input, input from arguments and print the variables.
Here are most easy, simple and relatable examples of capturing user input and displaying a personalized message:
########### WRITE A SIMPLE CODE TO TAKE INPUT FROM THE USER & PRINT ##########
#!/bin/bash
echo "Hello, who am I talking to?" # Ask the user for their name
read varname
echo "It is nice to meet you $varname"
########################### LOGIN TO HERE! ##################################
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar #-s indicates secure
echo
echo "Thankyou $uservar, Now we have your login details"
πΉ Conditional Statements You can make your scripts smarter using conditional statements. Let's check if a number is positive, negative, or zero:
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 0 ]; then
echo "It's a positive number!"
elif [ $num -lt 0 ]; then
echo "It's a negative number!"
else
echo "It's zero!"
fi
πΉ Loops Looping allows you to repeat tasks. The following script counts from 1 to 5:
#!/bin/bash
#Now we are printing 1 to 5
for ((i=1; i<=5; i++))
do
echo $i
done
πΉ Conclusion Congratulations! π You've taken your first steps into the world of Linux shell scripting for DevOps. Embrace the power of automation and keep exploring new possibilities. The command-line universe is vast, so keep learning and honing your skills. Happy scripting! ππ»π
Remember, practice makes perfect, and don't hesitate to Google for more examples and resources. Happy scripting, DevOps superheroes! π¦ΈββοΈπ¦ΈββοΈπͺ
#Linux #whatislinux #whylinux #secure #kernel #shell #hardware #software #application #LinuxArchitecture #LinuxFileHierarchy