Lecture # 3 - Basic Shell Script

Lecture # 3 - Basic Shell Script

Writing a simple shell script

Shebang:

All the shell script files have the same extension that is .sh . There can be multiple shells installed in an operating system. The operating system will know which shell to use to execute the shell script using shebang.

  • For sh:#!/bin/sh

  • For bash:#!/bin/bash

Basics of a Shell Script:

  • Shebang Line (The first line of a shell script should start with a shebang)

  • Comments

  • Commands

  • Variables

  • Control Structures

  • Functions

  • Exit Status

  • File Permissions

  • Executing the Script

Operators:

  • Relational Operators:

    These operators works only for numeric values.

  • String Operators:

Basic Shell Script:

#!/bin/bash
echo "This is my first Shell Script."
  • Create a file with .sh extension.

  • Open the file in any editor and write the script.

  • Give execute permission to the file using sudo chmod +x [script-name].sh .

  • Execute your script using ./[script-name].sh .

Taking input from the user:

#!/bin/bash
echo "Please enter your name:"
read name
echo "Hi, $name!"