Scripts

Executable Files


A Linux file is executable if it contains instructions that can be directly interpreted by the operating system. Basically, an exectuable file is a ready-to-run program. They’re also referred to as binaries or executables.

We will become very familiar with a particular kind of executable called a script, which is a program written in a scripting language. You’ll learn all about shell scripting, or more specifically Bash scripting, which is writing scripts in Bash (born-again shell), a very popular shell scripting language. A shell script is a plain text file that can be interpreted by a shell.

Formally speaking, for a text file to be considered an executable shell script for a given user, it needs to have two things:

  1. Execute permissions set for that user

  2. A directive, called a “shebang”, in its first line to declare itself to the operating system as a binary

  • Script is a list of commands that can be interpreted
  • Commands can be entered interactively
  • Scripting languages are interpreted at runtime not compiled
  • Slower to run but faster to develop
  • Widely used to automate processes such as ETL, file backups…
  • Shell script is an executable text file with an interpreter directive in the first line
  • knows as a ‘shebang’ directive: #!interperter [optional-argument]
  • where interpreter is the absolute path to an executable program
  • optional argument is a single argument string representing a single argument
  • shell script directives: #!/bin/sh invoke the shell program from the bin directory
  • #!/bin/bash shebang invokes the Bash shell
  • Shebangs aren’t limited to shell programs, we can use python #!/usr/bin/env python3

Commands


Shell Variable

It’s just that a variable used in a shell script. The only difference from variables in other languages is you call them/invoke them by using $variable_name

$ firstname=Jeff
$ echo $firstname
Jeff

The first line assigns the value Jeff to a new variable called firstname. The next line accesses and displays the value of the variable, using the echo command along with the special character $ in front of the variable name to extract its value, which is the string Jeff.

Thus, we have created a new shell variable called firstname for which the value is Jeff.

This is the most basic way to create a shell variable and assign it to a value all in one step.

List All Shell Variables

set

List All Environment Variables

env

Extend vars to Child Processes

export my_planet
export my_galaxy='Milky Way'

Command Line Variable

We can define and use a variable at the command line:

  • here the read command waits for the user input
  • then assigns the value to the variable lastname
$ read lastname
Santa
$ echo $lastname
Santa

Example 1

  • use $ touch hello_world.sh to create a text file .sh (shell script file)
  • Then turn your text file into a bash script by echoing the bash shebang
  • and appending that echoed test to your file using the >> which is the bash output redirection operator used for appending output to a file.
  • Use the echo command to print the statement hello world and again redirect the output to your bash script
  • Before you can run it you need to make the file an executable file.
    • first, check the permission by using the ls -l
    • r and w entries indicate read and write permissions but you lack the x which is for executable
    • Since I am the owner let’s set the permissions for all users to be executable: chmod +x
    • Running ls -l will show the file is executable for all users
$ touch hello_world.sh
$ echo '#! /bin/bash' >> hello_world.sh
$ echo 'echo hello world!!!' >> hello_world.sh

$ ls -l hello_world.sh
-rw-rw-r-- 1 jgrom jgrom 12 Jun 27 09:13 hello_world.sh

$ chmod +x hello_world.sh
 ls -l hello_world.sh
-rwxrwxr-x 1 jgrom jgrom 12 Jun 27 09:13 hello_world.sh
 
# Excute the file
$ ./hello_world.sh
 
 hello world!!!

Example: Create Script File

  • Create this script file and save to greet.sh
# This script accepts the user\'s name and prints 
# a message greeting the user

# Print the prompt message on screen
echo -n "Enter your name :"     

# Wait for user to enter a name, and save the entered name into the variable \'name\'
read name               

# Print the welcome message followed by the name    
echo "Welcome $name"

# The following message should print on a single line. Hence the usage of \'-n\'
echo -n "Congratulations! You just created and ran your first shell script "
echo "using Bash"
  • Open a new terminal
  • check permissions
  • We have read permission so let’s run it since it requires user input
$ ls -l greet.sh
-rw-r--r-- 1 theia users 522 Sep 25 10:46 greet.sh

$bash greet.sh
Enter your name :Santa
Welcome Santa
Congratulations! You just created and ran your first shell script using Bash

Example: Make file Executable

This is done to ensure that the name of the script can be used like a command. Adding this special shebang line lets you specify the path to the interpreter of the script - in this case, the Bash shell.

  • Find the path of the command bash which bash
  • Edit the .sh file and add this to the beginning: #! /bin/bash
  • Save greet.sh file again
  • Check permissions, should be the same as it was above
  • Add execute permission for the user chmod u+x greet.sh or for all users: chmod +x greet.sh
  • Execute the script with: ./greet.sh
$ which bash
/bin/bash

$ ls -l greet.sh
-rw-r--r-- 1 theia users 535 Sep 25 10:54 greet.sh
$ chmod u+x greet.sh
total 4
-rwxr-xr-x 1 theia users 535 Sep 25 10:54 greet.sh

./greet.sh
Enter your name :santa
Welcome santa
Congratulations! You just created and ran your first shell script using Bash 

Example F & L Names

Create a script named greetnew.sh that

  • takes the first and last names of the user
  • saves them in corresponding variables firstname and lastname,
  • prints a welcome message, such as "Hello <firstname> <lastname>"
#! /bin/bash

# This script accepts the user\'s name and prints 
# a message greeting the user

# Print the prompt message on screen
echo -n "Enter your firstname :"        

# Wait for user to enter a name, and save the entered name into the variable \'name\'
read firstname              

# Print the prompt message on screen
echo -n "Enter your lastname :"     

# Wait for user to enter a name, and save the entered name into the variable \'name\'
read lastname   

# Print the welcome message followed by the name    
echo "Hello $firstname $lastname."


# save file in greetnew.sh
# Add execute permission for the owner
$chmod u+x greetnew.sh

# execute filr
$ ./greetnew.sh