- My Bash script is 'ucked up.. I really need help!!
- Posted by Phil Powell on December 24th, 2003
I am sorry but I just have no idea whatever I'm doing. Bottom line is
that nearly every line of my bash script fails and I am unable to
determine how to fix it, in spite of numerous bash script tutorials
I've visited in the past few hours.
Could someone do a code review and tell me where on earth am I either
partially failing or totally made an arse of myself? Everything is
commented for ease of determining how bad at bash script I am.
Thanks
Phil
#!/bin/bash
#----------------------------------------------------------------------------------------------------------------------------------
# Script: cmds.sh
# Function: Command-line processing of files wrapped into an
"ease-of-use" process. Will perform file transactions in the safer
# environment of the command line.
#
# Dependencies:
# 1) existing back-end TCL script to perform process
# 2) existing back-end CSV file to contain file names and/or
directory names
#
# Syntax: .../cmds.sh -option
#
# Created: by Phil Powell on 12/24/2003. Merry Christmas/God
Jul/Glaedig Jul/Meli Kamikimaka!
#----------------------------------------------------------------------------------------------------------------------------------
#
# 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 -d = Download from source "
echo " $0 -u = Upload to source "
echo " $0 -a = Archive files (perform gzip) "
echo
exit 0
fi
#
# Get initial reading of last modification date on cmds.err
#
if ! [ -f $PWD/cmds.err ]; then # ERROR LOG MUST EXIST
cat > $PWD/cmds.err << CREATE ; CREATE
chmod 0777 $PWD/cmds.err
fi
stats=`stat -t $PWD/cmds.err`
read name size blocks unk uid gid dev inode links unk2 unk3 lastAccess
lastMod lastChange ioblock << $(echo $stats)
echo "lastMod = $lastMod"
#
# Pull up appropriate TCL file depending on option
#
case $1 in
'-t') script="$PWD/cmds.tcl";;
'-d') script="$PWD/download.tcl";;
'-u') script="$PWD/upload.tcl";;
'-a') script="$PWD/archive.tcl";;
*) script='';;
esac
#
# Run TCL script and throw appropriate errors if found
#
if [ -f $script ]; then
myDate=`date +[%d/%b%Y:%H:%I:%S]`
tclsh < $script 2>>$PWD/cmds.err
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
#
now=`date +%s`
stats=`stat -t $PWD/cmds.err`
read name size blocks unk uid gid dev inode links unk2 unk3 lastAccess
lastMod lastChange ioblock << THROW $(echo $stats)
THROW
if [ $lastMod ]; then
timeDiff=`expr $now - $lastMod`
echo $timeDiff
fi
- Posted by Ed Morton on December 24th, 2003
Phil Powell wrote:
What is the above trying to do? Is it trying to execute a command called
"CREATE" and store it's result, or trying to use an empty HERE document,
or something else? Whatever it is, it's wrong ;-). I think it's most
likely that you're just trying to create an empty file, so just do this:
touch cmds.err
No need to specify $PWD since that's the dir you're already in. If
you're trying to put a semi-colon in a file, you could do this:
echo ";" > cmds.err
or this:
cat <<CREATE > cmds.err
;
CREATE
Note that there must be no white-space before the second CREATE to
terminate the here document.
That "<<" makes the shell think you're using a here document. Do this
instead:
echo "$stats" | read name ....
if [ -f "$script" ]; then
read ... << THROW
$stats
THROW
would be the right format for what you're trying to do. Just not sure
that's really the right thing TO do. Why not do it the same way as
earlier in the script?
if [ "$lastMod" ]; then
There may be other problems, but try fixing those and see how it goes.
Ed.
- Posted by Paul Colquhoun on December 25th, 2003
On 24 Dec 2003 09:30:18 -0800, Phil Powell <soazine@erols.com> wrote:
| I am sorry but I just have no idea whatever I'm doing. Bottom line is
| that nearly every line of my bash script fails and I am unable to
| determine how to fix it, in spite of numerous bash script tutorials
| I've visited in the past few hours.
|
| Could someone do a code review and tell me where on earth am I either
| partially failing or totally made an arse of myself? Everything is
| commented for ease of determining how bad at bash script I am.
|
| Thanks
| Phil
|
| #!/bin/bash
|
| #----------------------------------------------------------------------------------------------------------------------------------
| # Script: cmds.sh
| # Function: Command-line processing of files wrapped into an
| "ease-of-use" process. Will perform file transactions in the safer
| # environment of the command line.
| #
| # Dependencies:
| # 1) existing back-end TCL script to perform process
| # 2) existing back-end CSV file to contain file names and/or
| directory names
| #
| # Syntax: .../cmds.sh -option
| #
| # Created: by Phil Powell on 12/24/2003. Merry Christmas/God
| Jul/Glaedig Jul/Meli Kamikimaka!
| #----------------------------------------------------------------------------------------------------------------------------------
|
| #
| # Check to see if the user has entered a valid option if any
| #
|
| if ! [ $1 ]; then
This will probably give an error if no options were supplied.
Try:
if [ -z "$1" ]; then
to test if $1 is zero length.
| echo
| echo 'You must enter an option: '
| echo '------------------------- '
| echo " $0 -t = Transfer files "
| echo " $0 -d = Download from source "
| echo " $0 -u = Upload to source "
| echo " $0 -a = Archive files (perform gzip) "
| echo
| exit 0
| fi
|
| #
| # Get initial reading of last modification date on cmds.err
| #
|
| if ! [ -f $PWD/cmds.err ]; then # ERROR LOG MUST EXIST
It's probably just a style thing, but I would have used:
if [ ! -f ....
| cat > $PWD/cmds.err << CREATE ; CREATE
As others have pointed out
touch $PWD/cmds.err
is much more straight forward.
| chmod 0777 $PWD/cmds.err
| fi
|
| stats=`stat -t $PWD/cmds.err`
| read name size blocks unk uid gid dev inode links unk2 unk3 lastAccess
| lastMod lastChange ioblock << $(echo $stats)
| echo "lastMod = $lastMod"
OK, all this stuff with "stats" is just to see if the error file has changed.
Replace this bit with something like:
touch $PWD/cmds.err.timestamp
then you can see if the file has changed with a line like:
if [ $PWD/cmds.err -nt $PWD/cmds.err.timestamp ]; then
|
| #
| # Pull up appropriate TCL file depending on option
| #
|
| case $1 in
| '-t') script="$PWD/cmds.tcl";;
| '-d') script="$PWD/download.tcl";;
| '-u') script="$PWD/upload.tcl";;
| '-a') script="$PWD/archive.tcl";;
| *) script='';;
| esac
|
| #
| # Run TCL script and throw appropriate errors if found
| #
|
| if [ -f $script ]; then
Again, since $script can be empty, put it in quotes like "$script"
| myDate=`date +[%d/%b%Y:%H:%I:%S]`
| tclsh < $script 2>>$PWD/cmds.err
| 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
| #
|
========================
| now=`date +%s`
| stats=`stat -t $PWD/cmds.err`
| read name size blocks unk uid gid dev inode links unk2 unk3 lastAccess
| lastMod lastChange ioblock << THROW $(echo $stats)
|
| THROW
=======================
The whole bit above is not needed if you use the "-nt" (Newer Than) test option.
| if [ $lastMod ]; then
Here is where you use:
if [ $PWD/cmds.err -nt $PWD/cmds.err.timestamp ]; then
and follow it with echoing the contents of $PWD/cmds.err (or
whatever it was you wanted to do with it).
Dont forget to touch the timestamp again to reset the time,
or all subsequent commands will look like they had an error.
| timeDiff=`expr $now - $lastMod`
| echo $timeDiff
| fi
--
Reverend Paul Colquhoun, ULC. http://andor.dropbear.id.au/~paulcol
Asking for technical help in newsgroups? Read this first:
http://catb.org/~esr/faqs/smart-questions.html#intro
- Posted by P.T. Breuer on December 25th, 2003
In comp.os.linux.misc Paul Colquhoun <postmaster@andor.dropbear.id.au> wrote:
Hmm .. the real problem is missing quotes, no? What if the variables
value has a space in it? Other than that, ! [ stuff ] is not at all
bad, since it catches what happens if the test goes wrong. [ ! -f ]
will fail if there is a syntax error, for example, whereas ! [ -f ]
will succeed.
or > "$PWD/cmds.err" :-).
yo mama.
Peter