umma.dev

Shell Scripts

Setting up a shell script and basic commands.

What are Shell Scripts?

Linux command line provides a program called shell. These scripts include a series of commands written in a .sh file, which are executed line by line. For example you can create a folder, navigate to a folder, start up a program etc.

Everything within in a shell script is done sequentially.

Set Up

First create a file [file-name].sh:

touch [file-name].sh

To find the path of your bash shell: which bash

Open up [file-name].sh and add the following to the file, and change the first line to match the path above:

#! usr/bin/bash
echo "Hello World"

When the above is executed, it will print out “Hello World”

Commands Within Shell Scripts

This script creates a directory and checks there isn’t a directory with the same name.

#! usr/bin/bash

echo "Enter directory name"
read dirname

if [ ! -d "$dirname" ]
then
    echo "File doesn't exist. Creating now"
    mkdir ./$dirname
    echo "File created"
else
    echo "File exists"
fi

This script enables you to cd into a directory.

#!/usr/bin/env bash

set -e
read name

APPLICATION_PATH=~/Documents/meteor/apps/$name

cd "${APPLICATION_PATH}"
pwd

Execute Shell Script

./[file-name].sh
bash [file-name].sh

To modify file permissions: chmod u+x [file-name].sh