Scheduling conditional statements with crontab

 

Recently while trying to auto-restart a daemon which is down by identifying with ps aux and if condition, it is not worked as intended with crontab.

I used following command which is perfectly running in from command line but not through crontab.

if [ `ps aux | grep nrpe | grep -v grep | wc -l` -eq 0 ]; then service nagios-nrpe-server restart ;fi

After trying with different commands following thing worked for me:

pgrep nrpe; [ $? != 0 ] && /etc/init.d/nagios-nrpe-server restart

where pgrep returns non zero exit code if the process nrep is not running and with $? is used to get the exit code of previous command (in this case pgrep) and start the process.

Lists/Remove all empty files in a directory

 

To list all empty files in a directory use follwoing command:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; fi; done

To remove all empty files in a directory use following commad:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; rm $file; fi; done

awk print Nth line after matching a pattern

 

By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt

1
2
3
4
5
6
7
8
9

Use following awk code to extract Nth line:

Syntax:

$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt

Output:

6

In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

bash – get previous command status

 

To get previous command status in bash use “$?” variable.

echo $? will return zero if previous command is success & if its non zero it indicates failure.

Example:

$ ls ; echo $?

Output of the above command is list of files, directories and next line it will return number.

We can use condition for check if previous command is success or failure:

any_command
if [ $? -eq 0 ] 
then
    echo SUCCESS
else
    echo FAIL
fi

awk – print last field

 

NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'

Output:
c

By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'

Output:
b

To print only number of fields with awk:

$ echo a b c | awk '{print NF}'

Output:
3

bash loop though multi line variable

 

I have a multi line variable in my bash script and wanted to iterate through each line of a variable.

To do this task I am using while loop.

Following is my variable:

var="a
b
c
d
e"

As I mentioned to loop through my multi line variable(var) I am using while as shown below:

while read -r line
do
  echo "line: $line"
done <<< "$var"

Just carefully observer last line in above code, here I am using three less than symbols(<<<) and variable in double quotes.

Output:
line: a
line: b
line: c
line: d
line: e

Above example only works with bash.

Bash key value store

 

Bash supports storing key & value pairs with associative arrays.

Associative arrays concept is available only in bash version 4 and above.

With following piece of code I will show how to story key values with associative arrays:

Declare an associative array, here am using keyValues as its name

declare -A keyValues

Assign key1 to val1

keyValues["key1"]=val1

Multiple key value assignments

keyValues+=( ["key2"]=val2 ["key3"]=val3 )

To iterate through each key value

for key in ${!keyValues[@]}
do
  echo ${key} ${keyValues[${key}]}
done

To get specific key’s value

echo ${keyValues[key1]}

By combining all above line my script for key values is following:

#!/bin/bash
declare -A keyValues
keyValues["key1"]=val1
keyValues+=( ["key2"]=val2 ["key3"]=val3 )
for key in ${!keyValues[@]}
do
    echo ${key} ${keyValues[${key}]}
done

echo "Value of key1: "${keyValues[key1]}

Output:
key3 val3
key2 val2
key1 val1
Value of key1: val1

Bash get nth argument

 

Bash arguments are stored in a array.

Getting nth argument is same as getting nth element from an array.

To get nth argument or nth element from an array use following code in your bash script:

n=4
echo ${!n}

where n=4 is to get 4th argument.

I have added above code in bash script and named it as temp.sh

Following is code in my script temp.sh

#!/bin/bash
n=4
echo ${!n}

By using following command execute temp.sh script:

$ bash temp.sh a b c d e

Output:

d

Since 4th argument for our script is d we got output as d.

Another way to get nth argument or nth element from an array is by suing array slicing.

Instead of using echo ${!n} use echo ${@:$n:1} in temp.sh script.

With array slicing my script temp.sh looks like following:

#!/bin/bash
n=4
echo ${@:$n:1}

Again execute script temp.sh:

$ bash temp.sh a b c d e

Output:

d

** Both ways explained in this article only works with bash **

Bash print last argument

 

To print last argument passed to bash script use following command in your script:

echo "${@: -1}"

Lets try with example, write following code in a file (t.sh)

#!/bin/bash

echo LastArg: "${@: -1}"

Now execute t.sh script with following command:

$ bash t.sh 1 2 3

Output:

LastArg: 3

** It works only with bash **