ini.sh (1137B)
1 #!/usr/bin/env bash 2 # c90839f9-835f-4f11-b6cd-86d17220195d 3 shopt -s extglob 4 5 readonly PROGNAME=$(basename $0) 6 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 7 8 # Usage string 9 usage=" 10 Usage: 11 ${PROGNAME} ini-file [token] 12 " 13 14 # No file = no data 15 inifile="${1}" 16 if [[ ! -f "$inifile" ]]; then 17 exit 18 fi 19 20 # Process the file line-by-line 21 SECTION= 22 while read line; do 23 24 # Remove surrounding whitespace 25 line=${line##*( )} # From the beginning 26 line=${line%%*( )} # From the end 27 28 # Remove comments and empty lines 29 if [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then 30 continue 31 fi 32 33 # Handle section markers 34 if [[ "${line:0:1}" == "[" ]]; then 35 SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/') 36 SECTION=${SECTION##*( )} 37 SECTION=${SECTION%%*( )} 38 SECTION="${SECTION}." 39 continue 40 fi 41 42 # Output found variable 43 NAME=${line%%=*} 44 NAME=${NAME%%*( )} 45 VALUE=${line#*=} 46 VALUE=${VALUE##*( )} 47 48 # Output searched or all 49 if [[ -z "${2}" ]]; then 50 echo "${SECTION}${NAME}=${VALUE}" 51 fi 52 if [[ "${SECTION}${NAME}" = "${2}" ]]; then 53 echo "${VALUE}" 54 fi 55 56 done < "${inifile}"