Linux Shell Scripting

Linux Shell Scripting

What is Kernel ?

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system.

What is Shell ?

A shell is special user program which provide an interface to user to use operating system services. Shell accept human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that execute 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. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.


Explain in your own words and examples, what is Shell Scripting for DevOps.

In DevOps, shell scripting is used to automate repetitive tasks such as software installation, configuration, testing, and deployment. It helps to streamline processes and reduce human error, improving the efficiency of software development and deployment. Popular shell scripting languages used in DevOps include Bash, PowerShell, and Python.

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

#!/bin/bash is called a shebang or hashbang, which specifies the interpreter that should be used to execute the script. In this case, it specifies that the Bash shell should be used to interpret and execute the script.

Yes, #!/bin/sh can also be used to specify that the Bourne shell should be used to execute the script. However, it's important to note that there are some differences between the Bash shell and the Bourne shell, so certain commands or features may behave differently depending on which shell is used. Bash is a more powerful and modern shell that includes additional features and enhancements over the Bourne shell, so it is more commonly used in modern shell scripting. However, if compatibility with older systems or scripts is a concern, then using #!/bin/sh may be more appropriate.

Write a Shell Script which prints `I will complete #90DaysOofDevOps challenge`

Sure, here is a simple shell script that prints the message "I will complete #90DaysOfDevOps challenge" when executed:

#!/bin/bash 
echo "I will complete #90DaysOfDevOps challenge"         

Save this script in a file, for example, challenge.sh, and make the file executable using the command chmod +x challenge.sh. Then, you can execute the script by running ./challenge.sh in the terminal, which should output the message "I will complete #90DaysOfDevOps challenge"

sh script.sh         

Save this script in a file, for example, challenge.sh, and make the file executable using the command chmod +x challenge.sh. Then, you can execute the script by running ./challenge.sh in the terminal, which should output the message "I will complete #90DaysOfDevOps challenge".

Write a Shell Script to take user input, input from arguments and print the variables.

Sure, here's an example shell script that takes user input, command line arguments, and prints the values of the variables:


#!/bin/bash

# Take user input
echo "Enter your name: "
read name


# Take input from arguments
if [ $# -eq 0 ]; then
? ? echo "No arguments provided"
else
? ? arg1=$1
? ? arg2=$2
fi


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

In this script, the read command is used to take user input and store it in the name variable. The script also checks if any command line arguments are provided and stores them in the arg1 and arg2 variables if they exist.

To execute this script and pass command line arguments, you can use the following command:

./script.sh argument1 argument2        

Replace script.sh with the name of the file that you saved the script in.

When you execute the script, you will be prompted to enter your name. After entering your name and providing command line arguments (if any), the script will print the values of the variables. If no arguments are provided, the script will print a message indicating that no arguments were provided.

Write an Example of If else in Shell Scripting by comparing 2 numbers

example shell script that compares two numbers using an if statement:


#!/bin/bash


# Assign values to variables
num1=5
num2=10


# Compare the two numbers
if [ $num1 -gt $num2 ]; then
? ? echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
? ? echo "$num1 is less than $num2"
else
? ? echo "$num1 is equal to $num2"
fi
        

In this script, the values of the variables num1 and num2 are assigned to 5 and 10, respectively. The script then uses the -gt and -lt operators to compare the two numbers and prints a message depending on the result of the comparison.

If num1 is greater than num2, the script will print the message "5 is greater than 10". If num1 is less than num2, the script will print the message "5 is less than 10". If num1 is equal to num2, the script will print the message "5 is equal to 10".

You can modify the values of num1 and num2 to test different scenarios and see how the script responds.

要查看或添加评论,请登录

OMKAR KHEDKAR的更多文章

社区洞察

其他会员也浏览了