Files
hpr_website/www/eps/hpr3013/hpr3013_bash21_ex6.sh

40 lines
1003 B
Bash
Executable File

#!/usr/bin/env bash
#-------------------------------------------------------------------------------
# Example 6 for Bash Tips show 21: a poor way to make a configuration file
#-------------------------------------------------------------------------------
#
# Example configuration file using 'export'
#
CFG1='bash21_ex6_1.cfg'
if [[ ! -e $CFG1 ]]; then
echo "Unable to find $CFG1"
exit 1
fi
#
# Alternative configuration file using 'declare', converted from the other one
#
CFG2='bash21_ex6_2.cfg'
#
# Strip out all of the 'export' commands with 'sed' in a process substitution,
# turning the lines into simple variable declarations. Use 'source' to obey
# all of the resulting commands
#
source <(sed 's/^export //' $CFG1)
#
# Scan the (simple) variables beginning with '_CFG_' and convert them into
# a portable form by saving the output of 'declare -p'
#
declare -p "${!_CFG_@}" > $CFG2
#
# Now next time we can 'source' this file instead when we want the variables
#
cat $CFG2
exit