Skip to content

Commit d6ab10b

Browse files
Make the script generator more robust
Instead of direcroty write to the destination file, write to a temp file first, after the write finished, then overwirte the destination file.
1 parent 568d2a1 commit d6ab10b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

β€Žgenerator.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ for shell in sh bash fish ksh zsh; do
8484
exit 1
8585
fi
8686

87-
echo "#!/usr/bin/env ${shell}" > "${newDist}"
87+
tempDist="$(mktemp --suffix=ColorEcho)"
88+
echo "#!/usr/bin/env ${shell}" > "${tempDist}"
8889

89-
cat << SH_ECHO >> "${newDist}"
90+
cat << SH_ECHO >> "${tempDist}"
9091
9192
# ColorEchoForShell
9293
# https://github.com/PeterDaveHello/ColorEchoForShell
@@ -109,7 +110,7 @@ for shell in sh bash fish ksh zsh; do
109110
SH_ECHO
110111

111112
if [ "${shell}" = "sh" ]; then
112-
cat << SH_ECHO >> "${newDist}"
113+
cat << SH_ECHO >> "${tempDist}"
113114
if [ "\$(uname)" = "FreeBSD" ]; then
114115
ECHO="echo -e"
115116
elif [ "\$(uname)" = "Darwin" ]; then
@@ -147,15 +148,15 @@ SH_ECHO
147148
fi
148149
echoFunction="$(printf "%secho%s%s%s%s%s" "${fn}" "${dot}" "${light}" "${style}" "${style2}" "${color}")"
149150
echoFunctionShuffle="$(printf "%secho%s%s%s%s%s" "${fn}" "${dot}" "${light}" "${style2}" "${style}" "${color}")"
150-
if ! ( grep -q "${echoFunction}" "${newDist}" || grep -q "${echoFunctionShuffle}" "${newDist}") ; then
151+
if ! ( grep -q "${echoFunction}" "${tempDist}" || grep -q "${echoFunctionShuffle}" "${tempDist}") ; then
151152
{
152153
echo ""
153154
printf "%s%s" "${echoFunction}" "${brackets}"
154155
# write the code down
155156
echo "${startSym}"
156157
echo " ${echo}"' "\\033['"${finalStyleCode}${code}""$(grep "${color}" "${table}" | awk '{print $2}')"'m$'"${para}"'\\033[m"'
157158
echo "${endSym}"
158-
} >> "${newDist}"
159+
} >> "${tempDist}"
159160
fi
160161
done
161162
done
@@ -176,7 +177,7 @@ SH_ECHO
176177
;;
177178
esac
178179

179-
cat << LOLCAT >> "${newDist}"
180+
cat << LOLCAT >> "${tempDist}"
180181
${fnName}${startSym}
181182
${ifCond}
182183
echo "\$${para}" | lolcat
@@ -188,11 +189,12 @@ LOLCAT
188189

189190
# echo.Reset to remove color code on output
190191
fnName="${fn}echo${dot}Reset${brackets}"
191-
cat << RESET >> "${newDist}"
192+
cat << RESET >> "${tempDist}"
192193
${fnName}${startSym}
193194
echo "\$${para}" | tr -d '[:cntrl:]' | sed -E "s/\\\\[((;)?[0-9]{1,3}){0,3}m//g" | xargs
194195
${endSym}
195196
RESET
197+
mv -f "${tempDist}" "${newDist}"
196198
} &
197199
done
198200

0 commit comments

Comments
Β (0)