Linux Classroom Series – 04/Sept/2020

Reading input from files

  • Lets assume we have list of server ip addresses or hosts in the file called as servers.txt.
  • We are asked to find which servers are up
  • This script is working but it is unable to redirect the output to a text
#!/bin/bash

# Usage: ./checkservers <servers-filepath>
# servers-filepath is a text file with each server in new line

if [ ! -f "$1" ] ;
then
    echo "The input to $0 should be a file"
fi
echo "The following servers are up on $(date +%x)" > checkservers.out
while read server; 
do
    ping -c1 "$server"&& echo "Serverup $server" >> checkservers.out
done < $1

cat checkservers.out

Building Blocks for reusability : Functions

  • DRY principle(Don’t Repeat Your Self)

  • We will cover the following aspects

    • Introduction
    • Passing Parameters to the functions
    • Variable scope
    • Returning values from functions
    • Recursive functions
  • Functions are internally represented as blocks of code in memory as named elements. These elements can be created within shell environment, as well as within the script execution.

  • Execute declare -F in the bash. The output of this command might vary with distribution

  • Functions can be created using the following two syntaxes

  • Syntax 1:

function-name() {
    <code to be executed>
}
  • Syntax 2:
function <function-name> {
    <code to be executed>
}
  • Lets start with a simple function
show_system_details() {
    echo "Uptime is"
    uptime
    echo "Cpu details"
    lscpu
    echo "User list"
    who
}

is_file() {
    if [ ! -f "$1" ]; then
        echo "$1 is not a file"
        exit 2
    fi
}

backup_file() {
    is_file "$1"
    new_file_loc="${1}.bak" 
    cp $1 $new_file_loc
    echo "file is copied to $new_file_loc"
}

backup_file "/home/ubuntu/1.txt"


Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Network Integration by Acurax Social Media Branding Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%