Skip to content

Instantly share code, notes, and snippets.

@ccampanale

ccampanale/env2fs.sh

Last active Oct 28, 2016
Embed
What would you like to do?
env2fs is a simple script to facilitate the creation of files from environment variables
#!/bin/bash -e
#
# env2fs is a simple script to facilitate the creation of files from environment variables
# check for help
if [[ $1 =~ ^--help|^-h ]]
then
echo "usage: env2fs.sh"
echo
echo "env2fs is a simple script to facilitate the creation of files from environment variables."
echo "Exported environment variables with the following predicate \"ENV2FS_VAR\" are parsed to "
echo "create files from a path provided in the variable value and data separated by a colon ':'."
echo ""
echo "Example: export ENV2FS_VAR1='/tmp/mydir/myfile.out:This is my temporary file with data'"
echo "This will generate a file at /tmp/mydir/myfile.out"
echo "with the content: This is my temporary file with data"
exit 0
fi
VARS=${!ENV2FS_VAR*}
echo $VARS
for var in $VARS; do
var=$(printf '%s\n' "${!var}")
IFS=":"
f=($var)
new_file=${f[0]}
data=${f[1]}
mkdir -vp $(dirname ${new_file})
echo $data > $new_file
echo "ENV2FS[INFO]: Created $new_file from environment..."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.