Bash clipboard of snippets
sed '$!N;s/\n/ /' infile
function clean_temp_files()
{
rm -f $TEMPHTML $TEMPJSON $TEMPCOOKIE $TEMPCSV
}
trap clean_temp_files EXIT
function die()
{
RETCODE=$1
if [ "" == "$1" ]
then
RETCODE=255
fi
echo " FAILED"
exit $RETCODE
}
RCFILE="$HOME/.rcfile"
function readrc()
{
echo "+- Reading credentials from $RCFILE"
USER=$(grep "user=" $RCFILE | awk -F'=' '{print $2}')
PASSWD=$(grep "password=" $RCFILE | awk -F'=' '{print $2}')
}
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
LOG_ENABLED=1
LOGFILE="<path>/to.log"
function log() {
if [ 1 -eq $LOG_ENABLED ]
then
DATE=$(date)
echo "$DATE $(hostname) $(basename $0): $@" >> $LOGFILE
fi
}
DATE_START_ALL=$(date +%s)
log "############################################################"
log "Started @ $DATE_START_ALL"
rawurlencode() {
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
}
args=$1
echo http://url/q?=$(rawurlencode "$args")
Output example :
bash
$ ./toto.sh "toto@gmail.com&truc=bidule&c=0x1234&tutu=%3"
http://url/q?=toto%40gmail.com%26truc%3dbidule%26c%3d0x1234%26tutu%3d%253
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
: > filename
lsof -i4 -n