Add ini_val, megamount, parse_url

This commit is contained in:
Kevin van Zonneveld 2014-11-04 13:19:50 +01:00
parent 7b01d19b19
commit 61d23a4d03
3 changed files with 111 additions and 0 deletions

38
src/ini_val.sh Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env bash
function ini_val() {
local file="${1}"
local sectionkey="${2}"
local val="${3}"
local delim=" = "
local section=""
local key=""
# Split on . for section. However, section is optional
read section key <<<$(IFS="."; echo ${sectionkey})
if [ -z "${key}" ]; then
key="${section}"
section=""
fi
local current=$(awk -F"${delim}" "/^${key}${delim}/ {for (i=2; i<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
if [ -z "${val}" ]; then
# get a value
echo "${current}"
else
# set a value
if [ -z "${current}" ]; then
# doesn't exist yet, add
if [ -z "${section}" ]; then
# no section was given, add to bottom of file
echo "${key}${delim}${val}" >> "${file}"
else
# add to section
sed "/\[${section}\]/a ${key}${delim}${val}" -i "${file}"
fi
else
# replace existing
sed "/^${key}${delim}/s/${delim}.*/${delim}${val}/" -i "${file}"
fi
fi
}

32
src/megamount.sh Normal file
View File

@ -0,0 +1,32 @@
__dir=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
source "${__dir}/parse_url.sh"
function megamount () {
local url="${1}"
local target="${2}"
proto=$(parse_url "${url}" "proto")
user=$(parse_url "${url}" "user")
pass=$(parse_url "${url}" "pass")
host=$(parse_url "${url}" "host")
port=$(parse_url "${url}" "port")
path=$(parse_url "${url}" "path")
umount -lf "${target}" || true
mkdir -p "${target}"
if [ "${proto}" = "smb://" ]; then
mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}"
elif [ "${proto}" = "afp://" ]; then
# start syslog-ng
# afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1
mount_afp "${url}" "${target}"
elif [ "${proto}" = "nfs://" ]; then
mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" ${host}:/${path} ${target}
else
echo "ERR: Unknown protocol ${proto}"
exit 1
fi
# chmod 777 "${target}"
ls -al "${target}/"
}

41
src/parse_url.sh Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
function parse_url() {
# Based on http://stackoverflow.com/a/6174447/151666
local parse="${1}"
local need="${2}"
local proto="$(echo $parse | grep :// | sed -e's,^\(.*://\).*,\1,g')"
local url="$(echo ${parse/$proto/})"
local userpass="$(echo $url | grep @ | cut -d@ -f1)"
local user="$(echo $userpass | grep : | cut -d: -f1)"
local pass="$(echo $userpass | grep : | cut -d: -f2)"
local hostport="$(echo ${url/$userpass@/} | cut -d/ -f1)"
local host="$(echo $hostport | grep : | cut -d: -f1)"
local port="$(echo $hostport | grep : | cut -d: -f2)"
local path="$(echo $url | grep / | cut -d/ -f2-)"
[ -z "${user}" ] && user="${userpass}"
[ -z "${host}" ] && host="${hostport}"
if [ -z "${port}" ]; then
[ "${proto}" = "http://" ] && port="80"
[ "${proto}" = "https://" ] && port="443"
[ "${proto}" = "mysql://" ] && port="3306"
[ "${proto}" = "redis://" ] && port="6379"
fi
if [ -n "${need}" ]; then
echo ${!need}
else
echo ""
echo " Use second argument to return just 1 variable."
echo " parse_url() demo: "
echo ""
echo " proto: ${proto}"
echo " user: ${user}"
echo " pass: ${pass}"
echo " host: ${host}"
echo " port: ${port}"
echo " path: ${path}"
echo ""
fi
}