I need some help with this script, please. The code was lifted from the 'easybashgui' library, then modified for my script; Vittorio's part works (of course).
Basically, using the variable 'tempfile_sw' I want to use either (mktemp) temporary files, which are destroyed on exit, or 'user' files, which remain and can be examined later. With tempfile_sw=0 everything works as expected, but, when tempfile_sw=1, bash does not recognise the filename. The strange thing is that the commands are just copies of the original (library) lines, suitably modified for this new state, so I was surprised that they failed. I assume that there's a syntax snag in there somewhere, but I can't see it for the life of me. Can anyone out there spot the problem? Thanks.
#!/bin/sh
#
function rmfile() # remove file
# Syntax: rmfile filename
{
if [ -e $1 ];
then
{
rm $1
# echo $1" deleted"
}
fi
}
function init_tmp() # initialise temp-files
# Syntax: init_tmp
{
if [ -d "${HOME}/tmp" ]; then
dir_tmp="${HOME}/tmp"
else
dir_tmp="/tmp"
fi ; [ ! -d "${dir_tmp}" ] && mkdir "${dir_tmp}"
: dir_tmp
if [ $tempfile_sw -eq 0 ]; then # force SCRIPT-internal temp files
{
if [ -f "${dir_tmp}/${file_tmp01}" ]; then
{
: 1> "${dir_tmp}/${file_tmp01}"
: 1> "${dir_tmp}/${file_tmp02}"
}
else
{
cd "${dir_tmp}"
export file_tmp01="$(mktemp "XXXXXXXXXXXXXXXXXXXX" )"
export file_tmp02="$(mktemp "XXXXXXXXXXXXXXXXXXXX" )"
echo "*********"
echo "file_tmp01="${dir_tmp}/${file_tmp01}
echo "file_tmp02="${dir_tmp}/${file_tmp02}
cd - 1>/dev/null
}
fi
}
else # force USER-retained temp files
{
cd "${dir_tmp}"
export file_tmp01="$(touch ebg_ht001.tmp )"
export file_tmp02="$(touch ebg_ht002.tmp )"
echo "** ** ** **"
echo "file_tmp01="${dir_tmp}/${file_tmp01}
echo "file_tmp02="${dir_tmp}/${file_tmp02}
cd - 1>/dev/null
}
fi
}
function clean_tmp() # clean-up temp-files
# Syntax: clean_tmp
{
# local FUNCT_NAME="clean_temp"
# local IFS=$' \t\n'
#
# if [ $tempfile_sw -eq 0 ]; then # clean-up SCRIPT-internal temp files
# {
rmfile ${dir_tmp}/$file_tmp01
rmfile ${dir_tmp}/$file_tmp02
# }
# fi
}
# Script proper starts here . . . . . . .
#
tempfile_sw=0 # =0 forces SCRIPT-internal temp files to be used (=1 for USER-retained)
#
init_tmp
echo "fileaa_tmp01="${dir_tmp}/${file_tmp01}
echo "fileaa_tmp02="${dir_tmp}/${file_tmp02}
echo "# Some text or another . . ." > "${dir_tmp}/${file_tmp01}"
cat "${dir_tmp}/${file_tmp01}"
clean_tmp
exit 0