Posts Tagged ‘then’

Bash Script 1.2

#!/bin/bash

set -e
set -u

# Removed the _SRC_ROOT variable and replaced with input defined by the user

if [ $# -ge 1 ] && [ -d $1 ]; then
_SRC_ROOT=$1
else
echo “No valid directory provided, please use ./src-sanity-check <directory>. Script is exiting”
exit 1
fi

_ACTION=all

if [ $# -ge 2 ]; then
_ACTION=$2
fi

function list_src_dirs {

find $_SRC_ROOT -type d -printf ‘%m\t%u\t%g\t%p\n’ 2>/dev/null | \
sed  ‘/\/__/d; /RCS$/d’

}

function list_src_dirs_perm_err {

list_src_dirs | sed -nr ‘/\t(source|sys)\t/p’ | sed -r ‘/^277[05]/d’ | sort -k 2

}

echo $_SRC_ROOT
echo $_ACTION

When you define a directory or any information along with the script its assigned a value, for example:

./src-sanity-check.sh /src/config/redhat

/src/config/redhat will be defined as $1

Example:

./src-sanity-check.sh /src/config/redhat xxx

xxx will be defined as $2

so:

if [ $# -ge 1 ] && [ -d $1 ]; then
_SRC_ROOT=$1

This means if the $# input is greater or equal to 1 and is a directory then assign the value of $1 to _SRC_ROOT (which can be used later)

else
echo “No valid directory provided, please use ./src-sanity-check <directory>. Script is exiting”
exit 1
fi

So, if a value is not assigned to $1 provide the error stated above and exit the script.

We then define a new variable:

_ACTION=all

and start the next if statement:

if [ $# -ge 2 ]; then
_ACTION=$2
fi

Meaning if the second input $2 is defined then assign this value to _ACTION

Currently at the end of the script the two values are called:

echo $_SRC_ROOT
echo $_ACTION

Obviously this will be changed later.

Advertisements