Basic Linux Shell Scripting for DevOps Engineers

Β·

3 min read

Table of contents

No heading

No headings in the article.

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 shell scipting!"

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! πŸ¦Έβ€β™‚οΈπŸ¦Έβ€β™€οΈπŸ’ͺ

Β