Bash calls TCL, TCL calls Bash, 2nd Bash never reads input

From: Phil Powell (soazine_at_erols.com)
Date: 01/02/04


Date: 2 Jan 2004 14:29:01 -0800

I'm sorry but it's the ONLY way I can write this. I don't know how to
read standard in in TCL; I don't know how to do complex string
manipulation in Bash. So I have to write in the languages based on
what I know.

The bash script cmds.sh will read in various files. Check it out:

#
# Check to see if the user has entered a valid option if any
#

if ! [ $1 ]; then
 echo
 echo 'You must enter an option: '
 echo '------------------------- '
 echo " $0 -t = Transfer files "
 echo " $0 -a [--transferfiles] [--delete] [-d directory] [-f
file(s)|*.*] = Archive files (perform gzip) "
 echo
 exit 0
fi

#
# To ensure that the current working directory always points to the
location of cmds.sh you will read the value of the HOME location
# where this script is housed (e.g.: '/home/phillip/scripts'). You
will create a file called 'root.txt' that will be housed in the
# scripts folder and will simply contain the HOME location path and
nothing more.
#

pwd=`more /../root.txt`

#
# Get initial reading of last modification date on cmds.err
#

if ! [ -f $pwd/cmds.err ]; then # ERROR LOG MUST EXIST
 touch $pwd/cmds.err
 chmod 0777 $pwd/cmds.err
 hasCreatedErrorLog=1
else
 fileSizeErrorLogThen=`stat -t $pwd/cmds.err | cut -d " " -f2`
 hasCreatedErrorLog=0
fi

#
# Pull up appropriate TCL file depending on option
#

case $1 in
 '-t') script="$pwd/transfer/transfer.tcl";;
 '-a') script="$pwd/archive/archive.tcl";;
 *) script='';;
esac

#
# Run TCL script and throw appropriate errors if found
#
if [ -f $script ]; then
 tclsh < $script $*; # USING OPTIONAL PARAMETER LISTINGS ENCAPSULATED
INTO "$*" WILDCARD ARG VARIABLE
else
 echo " $script does not exist"
 exit 0
fi

#
# If cmds.err was modified the lastModified date will change on it, if
different display the error message in the log
#

fileSizeErrorLogNow=`stat -t $pwd/cmds.err | cut -d " " -f2`

if [ $fileSizeErrorLogThen ]; then
 fileSizeDiff=$[$fileSizeErrorLogNow - $fileSizeErrorLogThen] # FILE
SIZE DIFF SHOULD TELL YOU IF ERROR WAS RECENTLY LOGGED OR NOT
else
 fileSizeDiff=0
fi

if [ $hasCreatedErrorLog -eq 0 -a $fileSizeDiff -gt 0 ] || [
$hasCreatedErrorLog -eq 1 -a $fileSizeErrorLogNow -gt 0 ]; then
  errorStuff=`more $pwd/cmds.err`
  tclsh $pwd/cmds.tcl $errorStuff
fi

-------------------------------------------------------------------------------

Let's just say now I want to archive some files. cmds.sh will call
archive.tcl and run it. Here's archive.tcl the part that doesn't
work:

puts "files: ${lb}$fileList$rb - now reside in TAR file $tarFileName"

if {$willDeleteOrigTransferredFiles && $willArchiveTransferredFiles} {
 # USE DOUBLE BOOLEAN CONDITIONAL FOR EXTRA SECURITY
# source {/home/phillip/scripts/fileremoval.tcl}
# fconfigure stdin -blocking 0
# puts -nonewline stdout {Do you want to delete all archived transfer
files? (Y/N) }
# fconfigure stdout -blocking 0
# gets stdin deleteChar
# puts "deleteChar = $deleteChar"
# foreach fyl $fileList {
# FileRemoval::setFileName $fyl
# #FileRemoval::remove
  set cannotRemoveFiles [catch {
   eval "exec /home/phillip/scripts/fileremoval.sh $fileList"
  } errMsg]
  if {$cannotRemoveFiles && [regexp -nocase
"$root/scripts/fileremoval\\.sh" $errMsg]} {
   writeErr $errMsg
   exit
  } else {
   puts "files : ${lb}$fileList$rb - original files deleted"
  }
 }
}
 

------------------------------------------------------------------------------

Here is fileremoval.sh:

#!/bin/bash

if ! [ $1 ] || [ $# -lt 1 ]; then
 error 'Usage: $0 filename'; exit
fi

echo -n 'Do you wish to delete these files (Y/N)? '; read deleteChar

for file in $*; do
 if [ $deleteChar == 'Y' ] && ! [ -f $file ]; then
  error "File $file is not found"; exit
 elif [ $deleteChar == 'Y' ]; then
  rm $file
 fi
done

-------------------

Bottom line is that cmds.sh calls archive.tcl, archive.tcl, on certain
conditions, calls fileremoval.sh - I did it this way syntactically
because I know the TCL syntax (with an ENORMOUS amount of input from
the entire TCL community here) for one part while I know the Shell
Script syntax of another.

Problem arises in fileremoval.sh - it just plain does not work, here
is the error:

/home/phillip/scripts/fileremoval.sh: [: ==: unary operator expected

So basically I'm now having to choose between a TCL solution to remove
files upon an input prompt, or using bash script to remove files upon
an input prompt, and right now I can't do either. I'm tempted to
forego all of this in favor of content management web-based solutions
with PHP.

Help!
Phil



Relevant Pages