DEV Community

Asad
Asad

Posted on

Shell Scripting for DevOps

What is Shell Scripting for DevOps.

Shell scripting is a way to automate tasks on a computer using a programming language called a "shell." DevOps is a way of working where software development and IT operations work together to create and maintain software systems.

Shell scripting is often used in DevOps to automate repetitive tasks, like deploying new software or configuring servers. For example, a shell script could be used to automatically install software updates on a server every night at a certain time, so that the server stays up-to-date and secure.

Here's another example: let's say you have a website that you want to deploy to a server. You could use a shell script to automate the deployment process, which might involve copying files to the server, starting up the website, and making sure everything is working properly. The shell script would handle all of these steps automatically, so you don't have to do them manually each time you want to deploy the website.

Shell scripting is a powerful tool that DevOps professionals use to automate tasks and make their work more efficient. By automating repetitive tasks, they can focus on more complex and interesting work, which ultimately leads to better software systems.

## What is #!/bin/bash? can we write #!/bin/sh as well?

#!/bin/bash is called a "shebang" or "hashbang." It's a special line at the beginning of a shell script that tells the operating system what shell to use to interpret the script. In this case, #!/bin/bash tells the system to use the bash shell.

Yes, you can also use #!/bin/sh in your shell script. The difference is that #!/bin/bash specifies the bash shell specifically, while #!/bin/sh specifies the system's default shell (which may or may not be bash).

In most cases, using either one will work fine. However, bash has some additional features and capabilities that sh does not, so if you need those features, you should use #!/bin/bash. If you don't need those features, you can use #!/bin/sh and your script will still work.

#!/bin/bash and #!/bin/sh are just ways of specifying which shell to use to run a shell script, and which one you choose depends on your specific needs.

To Print out “90DaysOfDevOps Challenge”

#!/bin/bash 
echo "I will complete #90DaysOfDevOps challenge"
Enter fullscreen mode Exit fullscreen mode

To use this script, you'll need to save it to a file (for example, "challenge.sh") and make the file executable by running the command chmod +x challenge.sh. Then, you can run the script by typing ./challenge.sh in the terminal.
When you run the script, it will print the message "I will complete #90DaysOfDevOps challenge" to the terminal.

## Shell Script to take user input, input from arguments, and print the variables

#!/bin/bash

# Read user input and store in variable 
echo "Enter your name:" read name 

# Print user input variable 
echo "Your name is: $name" 

# Print variable passed as argument 
echo "The first argument is: $1"

# Print all arguments echo 
"All arguments are: $@" 

# Exit the script 
exit 0
Enter fullscreen mode Exit fullscreen mode

The read command is used to read user input and store it in the variable name. The $name variable is then printed using the echo command.

The script also takes an argument, which is stored in the $1 variable. This variable is then printed using echo.

The script also uses $@ to print all arguments passed to the script.

To use this script, you can save it to a file (for example, "print_vars.sh") and make the file executable by running the command chmod +x print_vars.sh. Then, you can run the script and pass an argument by typing ./print_vars.sh argument. When you run the script, it will prompt you to enter your name, and then print the variables as described above.

## Example of If else in Shell Scripting by comparing 2 numbers

#!/bin/bash

# Assign values to variables
num1=10
num2=5

# Compare the two numbers
if [ $num1 -gt $num2 ]; then
  echo "$num1 is greater than $num2"
else
  echo "$num2 is greater than or equal to $num1"
fi
Enter fullscreen mode Exit fullscreen mode

In this script, the variables num1 and num2 are assigned the values 10 and 5, respectively. The script then uses the -gt (greater than) operator to compare the two variables in an if-else statement.

If num1 is greater than num2, the script will print the message "10 is greater than 5". If num1 is not greater than num2 (i.e., num2 is greater than or equal to num1), the script will print the message "5 is greater than or equal to 10".

To use this script, you can save it to a file (for example, "compare_numbers.sh") and make the file executable by running the command chmod +x compare_numbers.sh. Then, you can run the script by typing ./compare_numbers.sh in the terminal.

Top comments (1)

Collapse
 
arafique458 profile image
Asad

90daystodevopschallange