Basic Linux Shell Scripting for DevOps

Basic Linux Shell Scripting for DevOps

Article: About shell scripting basic commands

Linux shell scripting refers to the process of creating and running scripts written in the Linux shell language, also known as the bash shell. These scripts are used to automate tasks, such as managing files and directories, running programs and applications, and setting up system configurations.

Here are some basic concepts and commands related to Linux shell scripting:

  1. Shebang line: This line at the beginning of a script specifies the interpreter to be used to run the script. For example, #!/bin/bash indicates that the script is to be run using the bash shell.

  2. Variables: Variables in shell scripts are used to store values that can be referenced later in the script. To assign a value to a variable, use the syntax variable_name=value.

  3. Conditional statements: Conditional statements are used to test conditions and perform actions based on the results. The if statement is used to test a condition, and the case statement is used to test multiple conditions.

  4. Loops: Loops are used to perform repeated actions. The for loop is used to iterate over a list of items, and the while loop is used to repeat an action until a condition is no longer true.

  5. Functions: Functions are used to group a series of commands into a single unit that can be called repeatedly throughout a script. To define a function, use the syntax function_name() { commands }.

  6. Command-line arguments: Command-line arguments are values passed to a script when it is run. They can be accessed using the variables $1, $2, $3, and so on, which correspond to the first, second, third, and so on arguments.

  7. Input/output redirection: Input/output redirection is used to redirect the standard input, output, and error streams of a command. The > operator is used to redirect output to a file, and the < operator is used to redirect input from a file.

  8. Pipes: Pipes are used to connect the output of one command to the input of another command. The | operator is used to create a pipe.

These are just a few of the many concepts and commands involved in Linux shell scripting. With practice, you can become proficient in creating and running shell scripts to automate tasks and manage your Linux system more efficiently.

#!/bin/bash is called a "shebang" or "hashbang" line, and it's used to indicate to the operating system what interpreter should be used to execute the script. In this case, #!/bin/bash is specifying that the script should be executed using the Bash shell.

Yes, you can also use #!/bin/sh to specify that the script should be executed using the default shell interpreter on most Unix-like systems, which is usually the Bourne shell or a compatible shell. However, note that Bash is a superset of the Bourne shell, so if you use Bash-specific features in your script, you should specify #!/bin/bash to ensure that the script runs correctly.

Here's a simple shell script that prints the message "I will complete #90DaysOfDevOps challenge" to the console:

#!/bin/bash

echo "I will complete #90DaysOfDevOps challenge"

Save this code to a file with a .sh extension (e.g. challenge.sh), and then make it executable by running chmod +x challenge.sh. Finally, execute the script by running ./challenge.sh in your terminal. The script should print the message "I will complete #90DaysOfDevOps challenge" to the console.

Here's an example shell script that takes user input, input from command line arguments, and prints the variables:

#!/bin/bash

# Take user input
echo "Please enter your name:"
read name

# Input from command line arguments
arg1=$1
arg2=$2

# Print the variables
echo "Name: $name"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"

Save this code to a file with a .sh extension (e.g. input_script.sh), and then make it executable by running chmod +x input_script.sh.

You can then run the script with command line arguments by running ./input_script.sh arg1 arg2, where arg1 and arg2 are the values you want to pass as arguments. The script will prompt you to enter your name and then print the values of the variables name, arg1, and arg2.

Here's an example of an if-else statement in a shell script that compares two numbers:

#!/bin/bash

# Compare two numbers
num1=10
num2=20

if [ $num1 -gt $num2 ]
then
    echo "$num1 is greater than $num2"
else
    echo "$num1 is less than or equal to $num2"
fi

In this example, we're comparing the values of num1 and num2. The if statement checks whether num1 is greater than num2 using the -gt operator. If the condition is true, it prints the message "$num1 is greater than $num2". Otherwise, it prints the message "$num1 is less than or equal to $num2".

You can modify the values of num1 and num2 to test different scenarios and see how the if-else statement behaves.

#Linux #DevOps #Shellscript

Please free to write your thoughts and can connect with me on LinkedLn