if-then Script

An “if-then” script, also known as a conditional script, is a common programming construct in Linux shell scripting that allows you to perform different actions depending on whether a specific condition is true or false.

The purpose of an if-then script in Linux is to automate repetitive tasks by executing a set of commands or actions based on certain conditions. For example, if a certain file exists, then perform a specific action; if a specific command succeeds or fails, then execute another command, and so on.

The if-then script is a fundamental tool for automating tasks in Linux system administration, such as backup, monitoring, and security. It allows you to create complex workflows that can handle multiple conditions and actions, making it a powerful tool for system automation and maintenance.

For example, let’s start by creating a file

touch if-then

Very important, you need to give execute permission to the file, in order to edit it

chmod u+x if-then

Open the file by using an editor

vi if-then

Add the following script

#!/bin/bash
clear
echo
echo "Welcome to my little corner of the internet, where penguins rule and Linux is king! My name is Arctic Guru. What is your name?"
echo
read p
echo
echo Hello $p , it's a pleasure to meet you. Let's waddle our way to success together
echo
echo "Do you like scripting? (y/n)"
read Like
echo
if [ "$Like" == y ]
then
echo That's great! Our Linux-loving penguin pals have got all the tips, tricks, and tutorials you need to take on the world of open-source software.
elif [ "$Like" == n ]
then
echo You should try scripting, it’s a good field
echo
fi

This code is a Bash script that greets the user, asks for their name, and prompts them with a question about scripting. It then gives a response based on their answer.

The #!/bin/bash line at the top of the script specifies that the script should be executed using the Bash shell.

The clear command clears the terminal screen.

The echo command is used to print text to the terminal screen. The first two echo commands print the welcome message and ask for the user’s name. The next echo command greets the user by name.

The read command reads input from the user and stores it in the variable p.

The script then asks the user if they like scripting by printing the question “Do you like scripting? (y/n)” and reads their response using the read command, storing it in the variable Like.

The if statement checks the value of the Like variable using the syntax [ "$Like" == "y" ]. If the value is “y”, the then block is executed, which prints the message “That’s great! Our Linux-loving penguin pals have got all the tips, tricks, and tutorials you need to take on the world of open-source software.” If the value is “n”, the elif block is executed, which prints the message “You should try scripting, it’s a good field”. The fi keyword marks the end of the if-then statement.

Overall, this script is a simple interactive program that greets the user, asks them a question, and gives a response based on their answer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top