Skip to content

Instantly share code, notes, and snippets.

@ldante86
Last active November 9, 2016 01:03
Show Gist options
  • Save ldante86/08aeb2bbe2d302ab12dd0b0f31a7fd71 to your computer and use it in GitHub Desktop.
Save ldante86/08aeb2bbe2d302ab12dd0b0f31a7fd71 to your computer and use it in GitHub Desktop.
Generate ctag list of bash functions in files
#!/bin/bash -
PROGRAM="${0##*/}"
if [ $# -eq 0 ]; then
echo "Usage: $PROGRAM file1.sh file2.sh ..."
exit 1
fi
while [ $# -gt 0 ]
do
in_file="$1"
if [ ! -e "$in_file" ]; then
echo "$in_file does not exist"
shift
continue
elif [ -d "$in_file" ]; then
echo "$in_file is a directory"
shift
continue
fi
egrep -on '[A-Za-z0-9_]+[(]+[)]' "$in_file" >/dev/null
if [ $? -eq 1 ]; then
echo "$in_file doesn't have functions"
shift
continue
fi
out_file="${in_file%%.*}-index.txt"
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
printf "{%s}\t{%s}\t{%s}\n" "$(echo ${fname[i]} | tr -d '()')" "${in_file}" "${lineno[i]}" >> "$out_file"
#echo "{$(echo ${fname[i]} | tr -d '()')} {${in_file}} {${lineno[i]}}" >> "$out_file"
done
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment