Category: Bash

  • List open or listening ports

    MacOS

    lsof -nP -i4TCP

    RedHat/Centos7

    netstat -tulpn

  • Bash string comparison


    #!/bin/bash

    function test(){
    echo ""
    echo "TEST $1"
    echo "VAR_1: $VAR_1 VAR_2: $VAR_2 "
    if [ "$VAR_1" = "false" ]; then echo " VAR_1 is false"; fi
    if [ "$VAR_2" = "false" ]; then echo " VAR_2 is false"; fi
    if [ "$VAR_1" = "false" ] || [ "$VAR_2" = "false" ]; then echo " At least one is false"; fi
    }
    VAR_1='true';
    VAR_2='true';
    test 1

    VAR_1='true';
    VAR_2='false';
    test 2

    VAR_1='false';
    VAR_2='false';
    test 3

    function test2(){
    echo ""
    echo "TEST $1"
    echo "VAR_3: $VAR_3 VAR_4: $VAR_4 "
    [ -n "$VAR_3" ] && echo " VAR_3 is not null"
    [ -z "$VAR_3" ] && echo " VAR_3 is null"
    [ -n "$VAR_4" ] && echo " VAR_4 is not null"
    [ -z "$VAR_4" ] && echo " VAR_4 is null"
    }

    VAR_3=""
    VAR_4=""
    test2 4

    VAR_3="3"
    VAR_4=""
    test2 5

    Result

    $ /c/tmp/bashtest.sh

    TEST 1
    VAR_1: true VAR_2: true

    TEST 2
    VAR_1: true VAR_2: false
    VAR_2 is false
    At least one is false

    TEST 3
    VAR_1: false VAR_2: false
    VAR_1 is false
    VAR_2 is false
    At least one is false

    TEST 4
    VAR_3: VAR_4:
    VAR_3 is null
    VAR_4 is null

    TEST 5
    VAR_3: 3 VAR_4:
    VAR_3 is not null
    VAR_4 is null

  • Crontab header


    # minute (0-59),
    # | hour (0-23),
    # | | day of the month (1-31),
    # | | | month of the year (1-12),
    # | | | | day of the week (0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday).
    # | | | | | commands

  • Simple unit test is bash file

    Consider the following 3 files:

    1. shellTestFramework.sh


    #!/bin/bash
    # Copyright (c) Ronald Pringadi

    # Before each Test
    function setUpTest(){
    #"Please overwrite this function on your unit test. Something that need to be done before each test"
    return 0;
    }

    function tearDownTest(){
    #"Please overwrite this function on your unit test. Something that need to be done after each test"
    return 0;
    }

    # Helping assertion
    function assertEqual(){
    if [ "$1" == "$2" ]; then
    echo "`caller 0`. PASS"
    else
    echo "`caller 0`. FAIL. $1!=$2"
    fi
    }

    # Helping assertion
    function assertNotEqual(){
    if [ "$1" != "$2" ]; then
    echo "`caller 0`. PASS"
    else
    echo "`caller 0`. FAIL. $1==$2"
    fi
    }

    # To run all test.
    # Will run 'setUpTest()' before each test.
    # Will run 'tearDownTest()' after each test.
    function runAllTest(){
    array=($(compgen -A function |grep -i test))
    for i in "${array[@]}"
    do
    if [ "$i" != "setUpTest" ] && [ "$i" != "runAllTest" ] && [ "$i" != "tearDownTest" ]; then
    echo "`setUpTest`"
    echo "`$i`"
    echo "`tearDownTest`"
    fi
    done
    }

    2. osdetector.sh


    #!/bin/bash
    # Copyright (c) Ronald Pringadi

    # Returns String ("ubuntu", "censtos", "redhat", "unknown")
    function detectAnyOS(){
    OS="unknown"

    DETECT="ubuntu"
    if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then
    echo $DETECT
    return 0
    fi

    DETECT="centos"
    if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then
    echo $DETECT
    return 0
    fi

    DETECT="Red Hat"
    if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then
    echo "redhat"
    return 0
    fi

    DETECT="RHEL"
    if [ `detectSpecificOS "$DETECT"` -eq 1 ] ; then
    echo "redhat"
    return 0
    fi
    }

    # Input String ("ubuntu", "Centos", "RedHat")
    # Returns String "1"=found, "0"=not found
    function detectSpecificOS(){
    DETECT=$1
    IS_FOUND=`cat /etc/*-release |grep -i "$DETECT"|wc -l`
    if [ $IS_FOUND -gt 0 ]; then
    echo "1"
    else
    echo "0"
    fi
    }

    # Returns String ("6", "5.5", "4")
    function detectOSVersion(){
    # Works for ubuntu
    VERSION=`cat /etc/*-release |grep 'DISTRIB_RELEASE='|grep -o '[0-9|.]\+'|head -1`
    if [ "$VERSION" != "" ]; then
    echo $VERSION
    return 0
    fi

    # Works for Centos
    VERSION=`cat /etc/*-release |grep ' release '|grep -o '[0-9|.]\+'|head -1`
    if [ "$VERSION" != "" ]; then
    echo $VERSION
    return 0
    fi

    # Final resort
    VERSION=`cat /etc/*-release |grep -i 'ubuntu\|red hat\|centos'| grep -o '\ [0-9|.]\+'|head -1`
    if [ "$VERSION" != "" ]; then
    echo $VERSION
    return 0
    fi

    }

    # Returns String ("6", "5", "4")
    function detectOSMajorVersion(){
    VERSION=`detectOSVersion`
    MAJOR_VERSION=`echo "$VERSION" |grep -o '[0-9]\+'|head -1`
    echo "$MAJOR_VERSION"
    }

    # Input String OS ("ubuntu", "Centos", "RedHat")
    # Input String OSVersion name
    # Returns String "1"=supported, "0"=not supported
    function isOSSupported(){
    SUPPORTED="0"
    OS=`echo "$1"| tr '[:upper:]' '[:lower:]'`
    VERSION="$2"
    MAJOR_VERSION=`echo "$VERSION" |grep -o '[0-9]\+'|head -1`
    if [ "$OS" == "ubuntu" ]; then
    if [ "$MAJOR_VERSION" == "12" ] || [ "$MAJOR_VERSION" == "13" ] || [ "$MAJOR_VERSION" == "14" ]; then
    SUPPORTED="1"
    fi
    fi
    if [ "$OS" == "red hat" ] || [ "$OS" == "redhat" ] || [ "$OS" == "centos" ]; then
    if [ "$MAJOR_VERSION" == "6" ]; then
    SUPPORTED="1"
    fi
    fi
    echo $SUPPORTED
    }

    3. osdetector.test.sh


    #!/bin/bash
    # Copyright (c) Ronald Pringadi
    my_dir="$(dirname "$0")"
    source "$my_dir/osdetector.sh"
    source "$my_dir/shellTestFramework.sh"

    # Before each Test
    function setupTest(){
    RELEASE_CONTENT="Ubuntu 14.04.3 LTS"
    echo "$RELEASE_CONTENT">/etc/test-release
    }

    function adetectSpecificOSTest(){
    DETECT="Ubuntu"
    RESULT=`detectSpecificOS $DETECT`
    assertEqual "$RESULT" "1"

    DETECT="Centos"
    RESULT=`detectSpecificOS $DETECT`
    assertEqual "$RESULT" "0"
    }

    function detectAnyOSTest(){
    RESULT=`detectAnyOS`
    assertEqual "$RESULT" "ubuntu"
    assertNotEqual "$RESULT" "centos"
    }

    function detectOSAndOsVersionTest(){
    RELEASE_CONTENT="Ubuntu 14.04.3 LTS"
    echo "$RELEASE_CONTENT" > /etc/test-release
    RESULT=`detectOSVersion`
    assertEqual "$RESULT" "14.04.3"
    RESULT=`detectOSMajorVersion`
    assertEqual "$RESULT" "14"
    RESULT=`detectAnyOS`
    assertEqual "$RESULT" "ubuntu"

    RELEASE_CONTENT="CentOS release 6.6 (Final)"
    echo "$RELEASE_CONTENT" > /etc/test-release
    RESULT=`detectOSVersion`
    assertEqual "$RESULT" "6.6"
    RESULT=`detectOSMajorVersion`
    assertEqual "$RESULT" "6"
    RESULT=`detectAnyOS`
    assertEqual "$RESULT" "centos"

    RELEASE_CONTENT="CentOS Linux release 7.1.1503 (Core)"
    echo "$RELEASE_CONTENT" > /etc/test-release
    RESULT=`detectOSVersion`
    assertEqual "$RESULT" "7.1.1503"
    RESULT=`detectOSMajorVersion`
    assertEqual "$RESULT" "7"
    RESULT=`detectAnyOS`
    assertEqual "$RESULT" "centos"

    RELEASE_CONTENT="Red Hat Enterprise Linux Server release 6.3 (Santiago)"
    echo "$RELEASE_CONTENT" > /etc/test-release
    echo "$RELEASE_CONTENT" >> /etc/test-release
    RESULT=`detectOSVersion`
    assertEqual "$RESULT" "6.3"
    RESULT=`detectOSMajorVersion`
    assertEqual "$RESULT" "6"
    RESULT=`detectAnyOS`
    assertEqual "$RESULT" "redhat"
    }

    function isOSSupportedTest(){

    RESULT=`isOSSupported "ubuntu" "11"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "ubuntu" "12"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "ubuntu" "13"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "ubuntu" "14"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "ubuntu" "15"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "redhat" "5"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "redhat" "6"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "redhat" "7"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "red hat" "5"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "red hat" "6"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "red hat" "7"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "centos" "5"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "centos" "6"`
    assertEqual "$RESULT" "1"

    RESULT=`isOSSupported "centos" "7"`
    assertEqual "$RESULT" "0"

    RESULT=`isOSSupported "Fedora" "50"`
    assertEqual "$RESULT" "0"

    }

    runAllTest

  • Linux find files and total their size


    find ./ -type f -newerct "1 May 2015" ! -newerct "1 Jul 2015" -print0 | du --files0-from=- -hc| tail -n1

  • Search and replace withing a file


    sed -i -e"s/SEARCH_TEXT/REPPLACE_TEXT/g" FILENAME.txt

    # another example using bakup
    sed -i.bak -e"s/SEARCH_TEXT/REPPLACE_TEXT/g" FILENAME.txt

    # another example using variable
    sed -i -e"s/\/pathname/\/pathname-$date/g" "filename-"$date".xml"

  • Bash Scripting Basics (Variable Assignment, If, Loop)


    #!/bin/bash
    ##################################################
    # Benchmark the processing time when wkhtmltopdf
    # converts an html file to a pdf file
    ##################################################

    if [[ $1 = "" || $2 = "" || $3 = "" ]]; then
    echo "Usage: `basename $0` sourceFile.html destinationFile.pdf iteartionRunTime"
    else
    _min=9999
    _max=0
    _total=0
    _iteration=$3
    for (( _i=1; _i<=$_iteration; _i++ )) do _start_time=`date +%s` echo "running $_i" ~MYUSER/bin/benchmarkWkhtmltopdf.sh $1 $2 > /dev/null 2>&1
    _end_time=`date +%s`
    _processing_time=$((_end_time-_start_time))
    if [ "$_processing_time" -lt "$_min" ]; then
    _min=$_processing_time
    fi
    if [ "$_processing_time" -gt "$_max" ]; then
    _max=$_processing_time
    fi
    _total=$(($_total+$_processing_time))
    done
    _processing_time_average=$(echo - | awk "{print $_total/$_iteration}")
    echo "Processing time avg: $_processing_time_average min:$_min max:$_max total:$_total"
    fi

  • Compress and Extract Files in Linux

    Extract:

    .tar.bz2
    To extract a tar (Tape ARchiver) file, type the following in the shell prompt:

    tar xvjf yourFileName.tar.bz2

    Which will untar it to the current directory. Depending on the file structure that being compressed, it might create sub-directories.
    Parameters:
    x – extract
    v – verbose output (lists all files as they are extracted)
    j – deal with bzipped file
    f – read from a file, rather than a tape device

    .tar.gz or .tgz

    tar xvzf file.tar.gz
    tar xvzf file.tgz

    Parameters:
    -x, –extract, –get extract files from an archive
    -v, –verbose verbosely list files processed
    -z, –gzip, –gunzip, –ungzip filter the archive through gzip
    -f, –file=ARCHIVE use archive file or device ARCHIVE

    .bz2 file

    tar jxf YOUR_FILE_NAME.bz2

    .zip file

    unzip YOUR_FILE_NAME.zip

  • Delete files older than X number of days in Linux

    Say you backup your database daily and you need to delete the old backup files.
    You can issue the command bellow which will erase any files within the specified directory that are older than 7 days.


    $ find /your/target/directory* -mtime +7 -exec rm {} \;