DEV Community

Cover image for Shell Scripting Basics: Beginner's Guide - Part II
Abdullah Adeel
Abdullah Adeel

Posted on

Shell Scripting Basics: Beginner's Guide - Part II

In my last post about shell scripting, I talked about the basics of shell scripting. In this article, I am going to discuss some unfamous commands/uses of shell scripting extending the previous ones.

If you're a complete beginner to shell scripting, you should still be able to get everything in this article but it is recommended that you first read this and join me back here. I'll be waiting.

waiting


Referencing the script itself

In your shell script flow, there may be some scenarios where you need to access the information about the currently running script. Let's say you want to know the name of the script currently being executed at the time of execution. You can simply do that by using $0. You might have seen this syntax in the previous article. In that case, you could use similar syntax to access the flags/argument passed to the script when executing. But in this case $0 is just the reference to the current script.

#! /bin/bash

echo "The name of the current file is $0"
Enter fullscreen mode Exit fullscreen mode

Declaring an Array

Arrays are one of the most commonly used data structures out there and bash also supports arrays with pretty much straightforward syntax.
An array can be initialized using ( ) and you can pass default values separated by spaces. A simple example is shown below.

#! /bin/bash/

my_friends=("Aley" "John" "Doe")

Enter fullscreen mode Exit fullscreen mode

To access the values stored in an array, you can simply use the index to access the value. Keep in mind that indexes start from 0.

#! /bin/bash

echo ${my_friends[0]}

: '
Output: 
>> Aley
'

Enter fullscreen mode Exit fullscreen mode

Similarly, the negative index can be used to access the value from the end of the array.

#! /bin/bash

echo ${my_friends[-1]}

: '
Output: 
>> Doe
'

Enter fullscreen mode Exit fullscreen mode

Until Loop

Until loops work similarly to while loops with the only difference being it on terminate when the condition returns True.

until [ your_condition ]
do
        <some_command>
done

Enter fullscreen mode Exit fullscreen mode

A great use case of until would be waiting for the database to be available before starting the main server to prevent crashes.

#! /bin/bash

postgres_ready() {
python << END
import sys
import psycopg2
try:
    psycopg2.connect(
        dbname="${POSTGRES_DB}",
        user="${POSTGRES_USER}",
        password="${POSTGRES_PASSWORD}",
        host="${POSTGRES_HOST}",
        port="${POSTGRES_PORT}",
    )
except psycopg2.OperationalError:
    sys.exit(-1)
sys.exit(0)
END
}
until postgres_ready; do
  >&2 echo 'Waiting for PostgreSQL to become available...'
  sleep 1
done
>&2 echo 'PostgreSQL is available'

: '
# src ๐Ÿ‘‡
https://bit.ly/3nlrS2q
'
Enter fullscreen mode Exit fullscreen mode

At this point, you're ready to take charge of handling automated shell scripts in your team workflow.
For more web dev content and resources, please join my Twitter family by following @abdadeel_.

Top comments (0)