Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Created June 2, 2012 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirogoeth/2858595 to your computer and use it in GitHub Desktop.
Save pirogoeth/2858595 to your computer and use it in GitHub Desktop.
string processor and array tool for the upstream building feature im about to add to my compile.sh
#!/usr/bin/env bash
function checkIfElementIsBlank () {
test ! -z ${1}
}
# quick awk lesson
# ============================================================================================
# when using awk as a splitter, you will do awk '{print $#};', where # is the index number
# 0: entire string AFTER any proceeding formatting
# 1: first element in the string
# 2: second element in the string
# ... so on and so forth.
# ============================================================================================
function getKey () {
echo ${1//','/ } | awk '{print $1};'
}
function getValue () {
echo ${1//','/ } | awk '{print $2};'
}
s="
OpenAuth,/home/pirogoeth/OpenAuth;
OpenEconomy,/home/pirogoeth/OpenEconomy;"
s=${s//$'\n'/}
s=${s//";"/ }
array=( ${s} )
for (( i=0; $i<${#array[@]}; i++ ))
do
checkIfElementIsBlank ${array[$i]}
echo "BLANK: $?"
echo "KEY: $(getKey ${array[$i]}), VALUE: $(getValue ${array[$i]})"
echo "Exporting.."
export "$(getKey ${array[$i]})"="$(getValue ${array[$i]})"
done
upstream=( "OpenAuth" "OpenEconomy" )
for (( i=0; $i<${#upstream[@]}; i++ ))
do
echo ${!upstream[$i]}
done
# yay for indirect references!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment