Last active
October 28, 2016 17:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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