bashclip / sample.sh /
99a07fd 5 years ago
1 contributor
118 lines | 2.43kb
#!/bin/bash

# Temporary files
BASE=$(basename $0 .sh)
TEMPHTML="$BASE.html"
TEMPTXT="$BASE.txt"
TEMPJSON="$BASE.json"
TEMPCOOKIE="$BASE.cookie"
TEMPCSV="$BASE.csv"
RCFILE="$HOME/.$BASE""rc"
LOG_ENABLED=1
LOGFILE="$PWD/$BASE.log"

# Colors in scripts
GREEN="\\033[1;32m"
RED="\\033[1;31m"
MAGENTA="\\033[1;35m"
BLUE="\\033[1;34m"
WHITE="\\033[0;02m"
LIGHTGREY="\\033[1;08m"
YELLOW="\\033[1;33m"
CYAN="\\033[1;36m"
NORMAL="\\033[0;39m"

function log() {
  if [ 1 -eq $LOG_ENABLED ]
  then
    DATELOG=$(date)
    echo -e "$@"
    echo -e "$DATELOG $(hostname) $(basename $0): $@" >> $LOGFILE
  fi
}

# Die
function die()
{
  RETCODE=$1
  if [ "" == "$1" ]
  then
    RETCODE=255
  fi
  echo " FAILED"
  exit $RETCODE
}

# Minimal RC management
function readrc()
{
  echo "+- Reading credentials from $RCFILE"
  USER=$(grep "user=" $RCFILE | awk -F'=' '{print $2}')
  PASSWD=$(grep "password=" $RCFILE | awk -F'=' '{print $2}')  
}

# Clean Exit
function clean_temp_files()
{
  log "Exiting."
  rm -f $TEMPHTML $TEMPJSON $TEMPCOOKIE $TEMPCSV
}


urldecode() {
    # urldecode <string>

    local url_encoded="${1//+/ }"
    printf '%b' "${url_encoded//%/\\x}"
}
urlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"    # You can either set a return variable (FASTER)
  REPLY="${encoded}"   #+or echo the result (EASIER)... or both... :p
}

:> $LOGFILE
trap clean_temp_files EXIT

if [ ! -e $RCFILE ]
then
  echo "+- Credentials file does not exist"
  read -e -p "   +- Username      : " TEMP
  USER=$TEMP
  TEMP=""
  read -e -s -p "   +- Password      : " TEMP
  PASSWD=$TEMP

  # Make the file unreadable by others *BEFORE* putting password.
  touch $RCFILE
  chmod go-r $RCFILE

  echo "user=$USER">$RCFILE
  echo "password=$PASSWD" >> $RCFILE
  echo ""
else
  readrc                                                                                                                                                                                                       
fi
```


DATE_START_ALL=$(date +%s)
log "############################################################"
log "Started @ $DATE_START_ALL"
```

echo http://url/q?=$(rawurlencode "$args")