BASH Examples


Examples:


Sample 1: This was a script that was similar to the one I used to update some PFDS-HDSC files with CGI logging within a Docker container.

        
        #!/usr/bin/env bash
        #

        REPLACE="import cgitb\ncgitb.enable(display=0, logdir='/var/log/cgi/', format='text')\n"

        # ~/hdsc-website/cgi-bin/new/cgi_FDEplots_smoothed.py:10:import cgi
        sed -i "11i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_FDEplots_smoothed.py

        # ~/hdsc-website/cgi-bin/new/cgi_readH5_hi.py:5:import cgi, tables, os
        sed -i "6i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_readH5_hi.py

        # ~/hdsc-website/cgi-bin/new/tmp_testcgi.py:2:import cgi
        sed -i "3i $REPLACE" ~/hdsc-website/cgi-bin/new/tmp_testcgi.py

        # ~/hdsc-website/cgi-bin/new/cgi_readTempSubRegs.py:13:import cgi, os, tables, re, decimal
        sed -i "14i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_readTempSubRegs.py

        # ~/hdsc-website/cgi-bin/new/cgi_FDEplots_test.py:10:import cgi
        sed -i "11i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_FDEplots_test.py

        # ~/hdsc-website/cgi-bin/new/cgi_ssnPlots.py:10:import cgi
        sed -i "11i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_ssnPlots.py

        # ~/hdsc-website/cgi-bin/new/cgi_readH5_akt.py:13:import cgi, cgi_H5_funs_akt, decimal
        sed -i "14i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_readH5_akt.py

        # ~/hdsc-website/cgi-bin/new/tmp_cgi_readH5.py:3:import cgi
        sed -i "4i $REPLACE" ~/hdsc-website/cgi-bin/new/tmp_cgi_readH5.py

        # ~/hdsc-website/cgi-bin/new/cgi_readH5.py:13:import cgi, cgi_H5_funs, decimal
        sed -i "14i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_readH5.py

        # ~/hdsc-website/cgi-bin/new/cgi_DFplot_dur.py:11:import cgi
        sed -i "12i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_DFplot_dur.py

        # ~/hdsc-website/cgi-bin/new/cgi_ssnBarPlots.py:14:import cgi
        sed -i "15i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_ssnBarPlots.py

        # ~/hdsc-website/cgi-bin/new/tmp_testcgi2.py:2:import cgi
        sed -i "3i $REPLACE" ~/hdsc-website/cgi-bin/new/tmp_testcgi2.py

        # ~/hdsc-website/cgi-bin/new/getRegionNumber.py:3:import cgi
        sed -i "4i $REPLACE" ~/hdsc-website/cgi-bin/new/getRegionNumber.py

        # ~/hdsc-website/cgi-bin/new/cgi_ssnBarPlots_akt.py:10:import cgi
        sed -i "11i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_ssnBarPlots_akt.py

        # ~/hdsc-website/cgi-bin/new/cgi_DFplots.py:12:import cgi
        sed -i "13i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_DFplots.py

        # ~/hdsc-website/cgi-bin/new/cgi_rvv.py:14:import cgi, cgi_H5_funs
        sed -i "15i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_rvv.py

        # ~/hdsc-website/cgi-bin/new/cgi_FDEplots.py:13:import cgi
        sed -i "14i $REPLACE" ~/hdsc-website/cgi-bin/new/cgi_FDEplots.py

        # ~/hdsc-website/cgi-bin/tmp_testcgi.py:2:import cgi
        sed -i "3i $REPLACE" ~/hdsc-website/cgi-bin/tmp_testcgi.py
        
    

Sample 2: A crawler script I used locally to check for links within the PFDS-HDSC application and code base. The source code was from an older system and was not something I had worked on before.

        
        #!/usr/bin/bash
        #
        # refs #104294 "Testing/Small Bug fixes (HDSC Migration)"
        #
        # This script crawls the HDSC-PFDS container (http://localhost:12355) site and looks
        # for a links and reports on their status.
        #

        AHPS_URL='http://localhost:12355/'

        declare -a URIs=("hdsc/" "pfds/")

        for uri in ${URIs[@]}; do
            echo "------{ checking uri ${AHPS_URL}${uri} `date` }------" >> run.log
            wget --spider e robots=off -r -nd -nv -H -l 1 -w 5 -o run.log "${AHPS_URL}${uri}"

            echo
            echo
        done

        echo "completed `date`"
        
    

Sample 3: This is for the Nation Water Prediction Service (NWPS) application and updating the LRO data within the local development environment for testing.

        
        #!/usr/bin/env bash
        #
        # taken from: https://vlab.noaa.gov/gitlab-licensed/NWS/Systems/NCO/NEC/cloud-applications/hydrovis/national-water-prediction-service-nwps/web/nwps/nwps-core/-/merge_requests/15
        #


        TODAY=$(date +%Y%m%d)
        cd ~ && curl -O https://water.noaa.gov/resources/downloads/lro/long_range_river_flood_risk_data.csv
        convert_lro.py
        mv ~/long_range_river_flood_risk_data."$TODAY".sql ~/nwps-core/storage/postgres/data/
        docker exec -it nwps_postgres_1 sh -c $'psql -f /data/long_range_river_flood_risk_data.'"$TODAY"'.sql -U wps_website owp_water_website'
        docker exec -it nwps_postgres_1 sh -c $'psql -U wps_website owp_water_website -c "UPDATE ahps_lro SET produced_time = NOW();"'
        docker exec -it nwps_postgres_1 sh -c $'psql -U wps_website owp_water_website -c " DELETE FROM ahps_lro WHERE interval = \'NDJ\'; UPDATE ahps_lro SET interval = \'SON\' WHERE interval = \'OND\'; "'