Shell Scripting

Your first Shell script!!


  • Open the terminal
  • type the below command: this is to create a file
    • vi helloworld.sh
  • hit escape button (to enter the insert mode in vi editor)
  • type the below
    • echo "helloworld..!!"
    • to save the script hit escape and colon (:)type wq! (like this :wq!)and hit enter
  • now to execute the program use the below command
    • . ./helloworld.sh
    • hit enter
  • output is as below
    • helloworld..!!

Printing on screen

Example:1

This statement prints the statement as is on screen
echo Hello, how are you doing?

output:

Hello, how are you doing?

Example:2

Printing the value of any variable
a=10
echo $a

output:

10

Reading the user input


the below shell script reads 2 inputs from the user and prints it on the screen.

echo "Enter the name:"
read name
echo"Enter last name:"
read lname
echo "hello $name $lname, how are you?? "

Operators in Shell

Mathematical

[x -eq y] => x and y values are equal??
[x -ne y] => x and y are not equal ??
[x -lt y] => x is less than y??
[x -le y] => x is less than or equal to y??
[x -gt y] => x is greater than y??
[x -ge y] => x is greater than or equal to y??

String

string1 =string2 => string1 and string2 are equal??
string1 != string2 => string1 and string2 are not equal??
string1 => string1 is not NULL
-n string1 => string1 is not NULL and does exists
-z string1  => string1 is NULL and exists


Logical

AND (&&)
OR (||)
NOT (!)

AND (&&)

Syntax:

command1 || command2

Here command2 is only when command 1 is successful.

Example: 
Make a directory and change the directory to newly created one.

mkdir new_dir && cd new_dir

OR (||)

Syntax:
command1 || command2

Here command2 is only when command 1 fails.

Example: 
Make a directory if it doesn't exists.


[-d new_dir] || mkdir new_dir

Comments

Popular posts from this blog

Cross Browser testing for React Apps

Fixing the appium installation with NPM

Challenges in testing ChatBots