Friday, April 9, 2021

My first ever Bash script in kali Linux.

Here is the bash.sh file 

01  #! user/bin/bash

02 echo "this is my first bash script"

03 echo "what is your name ?"

04 read name 

05 echo "WOW you are: $name" 

06 echo $(whoami)

07 if [ $name ];then

08        echo "you are the owner of this system $name"

09 else

10       echo "Who are you !"

11 fi

and the output is like this 


The first line is for the terminal that it is a bash code  " #! user/bin/bash" and you have to find it by typing "which bash" command in the terminal. 

then the second line shows the string "this is my first bash script" by using "echo "this is my first bash script""

line number three is similar to line no two it is just showing a string output.

In the fourth line I asked for user input in the next line I used that input to show inside of a string as output. Like in the picture I passed a string of "mahabub" and got the output "WOW you are: mahabub".

Line number six is different though, by the command "echo $(whoami)" I actually executed a command. Where whoami shows the current user. you can use it for all types of commands like ipconfig, cal, time, etc. It is an interesting thing to learn.


Finally from line no 07 to 11 I implemented a conditional statement using if and else. 

 if [ $name ];then

        echo "you are the owner of this system $name"

 else

       echo "Who are you !"

 fi

 Here, the syntax is 

 if [condition ];then

        executable code

 else

       executable code

 fi

using this one can easily implement conditions in Bash.


No comments:

Post a Comment

My first ever Bash script in kali Linux.

Here is the bash.sh file   01   #! user/bin/bash 02 echo "this is my first bash script" 03 echo "what is your name ?" ...