Top
JohnKlann / Linux

Bash Execute Script At Specific Time Without Cron

#!/bin/bash

#Set Execution time to 3:15:01 PM
TimeToExecute="15:15:01"

while true; do
    #Convert Execution Time to Epoch
    GoalTime=$(date -d "$TimeToExecute" +"%s")
    #Get epoch of 12:00:00 AM
    BaseTime=$(date -d "00:00:00" +"%s")
    #Get the current epoch
    CurrentTime=$(date +"%s")
    #Get the epoch difference between the goal execution time and current time
    TimeDifference=$(date -d "0 $GoalTime seconds - $CurrentTime seconds" +"%s")
    #Get the difference between the "TimeDifference" and 12:00:00 AM 
    BaseDifference=$(( $BaseTime -$TimeDifference ))
    #If BaseDifference is equal to zero then execute script
    if [[ "$BaseDifference" -eq 0 ]]; then
            echo "Executing Script- date"
            /path/to/my/script.sh 
    fi
    sleep 1
done

 

jklann

List Files with their absolute path using Bash

function lsfp(){
local dirpath="$1"
local recurse="$2"

if [[ "$dirpath" == "" ]]; then
echo "no path provided!"
exit
else
local dirpath=$(echo "$dirpath" | sed 's:/$::g')
fi

local DirArr=($(ls -l "$dirpath" | grep -P "^d" | grep -oP "(?<= \d\d\:\d\d ).*"))
local FileArr=($(ls -l $dirpath | grep -oP "(?<= \d\d\:\d\d ).*$"))

for file in "${FileArr[@]}"; do
local fp="$dirpath/$file"
echo "$fp"
done

if [[ "$recurse" != "" ]]; then
for direc in "${DirArr[@]}"; do
lsfp "$dirpath/$direc" "$recurse"
done
fi
}
jklann

Basic Array Syntax, Iteration, Manipulation

So I recently took on a project where one of the requirements is that it has to be completed in Linux Bash and have as little outside dependencies as possible. So As Bash does not support multi-dimensional arrays, i’ll just cover array basics for now lol.

  1. Array Declaration:
    ARR=()
    
  2. Adding to Array
    1. values
      ARR+=('value')
      
    2. variables
      ARR+=("$VAR")
      
  3. Getting Array Length
    ARRLEN=${#ARR[@]}
    
  4. Iterating/Looping through Array (be mindful of your spacing)
    for (( i=0; i&lt;${ARRLEN}; i++ )); do
    	echo "${ARR[$i]}"
    done
    
  5. Putting it all together:
    #!/bin/bash
    
    ARR=()
    
    VAR="World"
    
    echo "adding value to array"
    
    ARR+=('hello')
    
    ARRLEN=${#ARR[@]}
    
    echo "Array length is now: $ARRLEN"
    
    echo "adding variable to array"
    
    ARR+=("$VAR")
    
    ARRLEN=${#ARR[@]}
    
    echo "Array length is now: $ARRLEN"
    
    echo "loop through array"
    
    for (( i=0; i&lt;${ARRLEN}; i++ )); do
            echo "Array Key/Index: $i holds Value: ${ARR[$i]}"
    done
    
  6. Returns:

linux_bash_basic_array_operations

jklann

Postgres How To Get List of Functions Like PGAdmin III

Recently I had the need to get a bunch of object stats and counts of two postgres databases to compare them. The goal was to confirm both via query results as well as visually via PGAdmin III, just in case I missed something in excel :P. Anyways I quickly realized that some of my object counts did not match the UI counts. After a little playing with the query I realized the UI splits the Functions into aggregates, functions, and trigger functions. However by default the UI hides the views for aggregates and trigger functions and you have to manually specify these to be shown in schema tree.

File -> Options
pg_admin_III_file_options

Browser -> Display -> Objects
pg_admin_III_file_options_browser_display_objects

So If you are wanting to get only user functions like the list that is in PGAdmin III under each schema in the function tree. You will want to remember to exclude aggregate functions and trigger functions.

This Query does just that. It gets the count of functions per schema just like displayed in the UI.

select ns.nspname, count(*)
from pg_proc pr
join pg_namespace ns on 
    pr.pronamespace = ns.oid
--Exlcude system schemas
where ns.nspname <> 'pg_catalog' 
    and ns.nspname <> 'information_schema' 
    --Exclude Trigger Functions
    and pr.oid not in 
        (select tgfoid from pg_trigger)
    --Exclude aggregate functions
    and pr.proisagg = 'f'
group by ns.nspname
order by ns.nspname;

This Query Gets a count of Aggregate functions per schema.

select ns.nspname, count(*)
from pg_proc pr
join pg_namespace ns on 
      pr.pronamespace = ns.oid
--Exlcude system schemas
where ns.nspname <> 'pg_catalog' 
    and ns.nspname <> 'information_schema' 
    --Exclude Trigger Functions
    and pr.oid not in 
         (select tgfoid from pg_trigger)
    --include aggregate functions
    and pr.proisagg = 't'
group by ns.nspname
order by ns.nspname

Lastly this Query Gets a count of Trigger functions per schema.

select ns.nspname, count(*)
from pg_proc pr
join pg_namespace ns on 
    pr.pronamespace = ns.oid
--Exlcude system schemas
where ns.nspname <> 'pg_catalog' 
    and ns.nspname <> 'information_schema' 
    --Include Trigger Functions
    and pr.oid in
         (select tgfoid from pg_trigger)
group by ns.nspname
order by ns.nspname

Now you have a set of Function count queries that will match the PGAdmin III UI.

jklann