Author Topic: Bash help required, please  (Read 853 times)

Offline satuser083

  • Hero Member
  • *****
  • Posts: 510
Bash help required, please
« on: January 14, 2011, 06:55:53 AM »
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.
Code: [Select]
#!/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

Offline satuser083

  • Hero Member
  • *****
  • Posts: 510
Re: Bash help required, please
« Reply #1 on: January 14, 2011, 09:38:33 AM »
OK, panic over. After some (more) web-searching I've discovered that the syntax needs to be
            
Code: [Select]
file_tmp01=ebg_ht001.tmp ; touch "${dir_tmp}/${file_tmp01}"
 file_tmp02=ebg_ht002.tmp ; touch "${dir_tmp}/${file_tmp02}"

instead of

Code: [Select]
export file_tmp01="$(touch ebg_ht001.tmp )"
 export file_tmp02="$(touch ebg_ht002.tmp )"

Actually, much simpler and more logical, IMO.
But, WTH this works and the other doesn't is a total mystery to me. Perhaps it's just another of bash's peculiarities ??? .

PS apologies for originally posting this in the wrong forum, didn't know this one existed   ::)  .
« Last Edit: January 16, 2011, 04:32:53 AM by satuser083 »

vaisarger

  • Guest
Re: Bash help required, please
« Reply #2 on: April 05, 2011, 12:29:38 AM »
OK, panic over. After some (more) web-searching I've discovered that the syntax needs to be
            
Code: [Select]
file_tmp01=ebg_ht001.tmp ; touch "${dir_tmp}/${file_tmp01}"
 file_tmp02=ebg_ht002.tmp ; touch "${dir_tmp}/${file_tmp02}"

instead of

Code: [Select]
export file_tmp01="$(touch ebg_ht001.tmp )"
 export file_tmp02="$(touch ebg_ht002.tmp )"

Actually, much simpler and more logical, IMO.
But, WTH this works and the other doesn't is a total mystery to me. Perhaps it's just another of bash's peculiarities ??? .

PS apologies for originally posting this in the wrong forum, didn't know this one existed   ::)  .

Really it's not such a total mistery...  :)
Command substitution (  "$(...)"  ) simply take output of command inside brackets and store it in variable.
If you write:
file_list="$(ls -1 ./)"
output of command inside brackets "ls -1 ./" is stored in variable called "file_list".

So, in this row:
export file_tmp01="$(touch ebg_ht001.tmp )"
variable "file_tmp01" will be empty because "touch" command is mute, that is it makes its job but doesn't put anything in its StdOut.
« Last Edit: April 05, 2011, 12:31:15 AM by vaisarger »