Use of If Statement on Bash Scripting

Hacking / 4 May, 2020

Use of If Statement on Bash Scripting

Use of If Statement on Bash Scripting – This is a bash scripting post that I am aiming for an individual memo in case the duration is required. Like someone sysadmin, I myself need some tasks that are made automatically, and because I can not coding (lol), bash scripting is more or less very helpful.

Well, in this guidance I want to review the If Statement in bash. By way of outline, the use of if is like the following:

if [first condition]; then

initial order;

elif [second condition]; then

second command;

else

substitution order;

fi

But the situation above can be reduced again so there is only one if statement (without elif), or added to more situations (more elif).

Conditional Expression

In bash scripting we understand conditional expressions. You can use the man bash command to read further. Or you can also see the next link:

https: or or www. gnu. org or application or bash or user guide or html_node or Bash-Conditional-Expressions. html

http: or or man7. org or linux or man-pages or man1 or bash. 1. html

Straight to the illustration, here I want to demonstrate the use of “- z” which is used to check whether the string length of the valuable input is empty.

We have 2 files where one file has content inside and the other is empty.

touch empty file

echo “content”
notempty

Then create a file checker with the next bash script:

#! or bin or bash

if [- z $ (cat $ 1)]

then

echo “This file is empty”;

else

echo “File is not empty”

fi

We check whether the script is in charge:

Well, there are actually a lot that need to be reviewed. But make this while first. I am gathering a desire to write again.