Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created November 23, 2016 05:21
Show Gist options
  • Save ldante86/2c15fa0d8ea4f9836ff4541f06ed52cd to your computer and use it in GitHub Desktop.
Save ldante86/2c15fa0d8ea4f9836ff4541f06ed52cd to your computer and use it in GitHub Desktop.
create JSON objects from bash functions
#!/bin/bash -
PROGRAM="${0##*/}"
if [ $# -eq 0 ]; then
echo "$PROGRAM: create JSON objects from bash functions"
echo "Usage: $PROGRAM file1.sh file2.sh [> file.json]"
exit 1
fi
in_file="$1"
if [ ! -e "$in_file" ]; then
echo "$in_file does not exist"
exit 1
elif [ -d "$in_file" ]; then
echo "$in_file is a directory"
exit 1
fi
egrep -on '[A-Za-z0-9_]+[(]+[)]' "$in_file" >/dev/null
if [ $? -eq 1 ]; then
echo "$in_file doesn't have functions"
exit 1
fi
lineno=()
fname=()
for i in $(egrep -on '[A-Za-z0-9_]+[(]+[)]' "$in_file" | awk -F ":" '{print $1}')
do
lineno+=( "$i" )
done
for i in $(egrep -on '[A-Za-z0-9_]+[(]+[)]' "$in_file" | awk -F ":" '{print $2}')
do
fname+=( "$i" )
done
for ((i=0; i<${#fname[@]}; i++))
do
echo "{\"${in_file}\":[\"$(echo ${fname[i]}\" | tr -d '()'), ${lineno[i]}]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment