Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- source /home/myuser/test/config
- echo "Name=$nam" >&2
- echo "Surname=$sur" >&2
- nam="Mark"
- sur="Brown"
- password=bar
- echo rm -rf /
- PROMPT_COMMAND='echo "Sending your last command $(history 1) to my email"'
- hostname=localhost; echo rm -rf /
- #!/bin/bash
- typeset -A config # init array
- config=( # set default values in config array
- [username]="root"
- [password]=""
- [hostname]="localhost"
- )
- while read line
- do
- if echo $line | grep -F = &>/dev/null
- then
- varname=$(echo "$line" | cut -d '=' -f 1)
- config[$varname]=$(echo "$line" | cut -d '=' -f 2-)
- fi
- done < myscript.conf
- echo ${config[username]} # should be loaded from defaults
- echo ${config[password]} # should be loaded from config file
- echo ${config[hostname]} # includes the "injected" code, but it's fine here
- echo ${config[PROMPT_COMMAND]} # also respects variables that you may not have
- # been looking for, but they're sandboxed inside the $config array
- #!/bin/bash
- config() {
- val=$(grep -E "^$1=" myscript.conf 2>/dev/null || echo "$1=__DEFAULT__" | head -n 1 | cut -d '=' -f 2-)
- if [[ $val == __DEFAULT__ ]]
- then
- case $1 in
- username)
- echo -n "root"
- ;;
- password)
- echo -n ""
- ;;
- hostname)
- echo -n "localhost"
- ;;
- esac
- else
- echo -n $val
- fi
- }
- echo $(config username) # should be loaded from defaults
- echo $(config password) # should be loaded from config file
- echo $(config hostname) # includes the "injected" code, but it's fine here
- echo $(config PROMPT_COMMAND) # also respects variables that you may not have
- # been looking for, but they're sandboxed inside the $config array
- myvar=Hello World
- myvar=Default Value
- othervar=Another Variable
- config_read_file() {
- (grep -E "^${2}=" -m 1 "${1}" 2>/dev/null || echo "VAR=__UNDEFINED__") | head -n 1 | cut -d '=' -f 2-;
- }
- config_get() {
- val="$(config_read_file config.cfg "${1}")";
- if [ "${val}" = "__UNDEFINED__" ]; then
- val="$(config_read_file config.cfg.defaults "${1}")";
- fi
- printf -- "%s" "${val}";
- }
- #!/usr/bin/env bash
- source config.shlib; # load the config library functions
- echo "$(config_get myvar)"; # will be found in user-cfg
- printf -- "%sn" "$(config_get myvar)"; # safer way of echoing!
- myvar="$(config_get myvar)"; # how to just read a value without echoing
- echo "$(config_get othervar)"; # will fall back to defaults
- echo "$(config_get bleh)"; # "__UNDEFINED__" since it isn't set anywhere
- source /home/myuser/test/config
- . /home/myuser/test/config
- <config>
- <username>username-or-email</username>
- <password>the-password</password>
- </config>
- username="$( xml sel -t -v '/config/username' "$config_file" )"
- {
- "username": "username-or-email",
- "password": "the-password"
- }
- username="$( jq -r '.username' "$config_file" )"
- CONF=${XDG_CONFIG_HOME:-~/config}/myscript.sh
- if [ ! -f $CONF ]; then
- cat > $CONF << CONF
- VAR1="default value"
- CONF
- fi
- . <(sed 's/^([^=]+) *= *(.*)$/1=${1:-2}/' < $CONF)
- #!/bin/bash
- name="mohsen"
- age=35
- cat > /home/myuser/test/config << EOF
- Name=$name
- Age=$age
- EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement