Skip to content

Instantly share code, notes, and snippets.

@ccampanale
Last active October 28, 2016 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccampanale/d1c4416390a4dcb840cd9641de863e2c to your computer and use it in GitHub Desktop.
Save ccampanale/d1c4416390a4dcb840cd9641de863e2c to your computer and use it in GitHub Desktop.
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