Discussion Locked

Shell script problem

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm new to shell scripting, but have managed to get a couple of scripts written that will automatically ftp inbound files from external clients to our HP3000 for processing. I have a Sun Ultra 10 w/Solaris 8 OS. I now have a couple clients that sendin files in a date type format with a .txt or .k or .cu extensions. For example: 03202001.k or mar2001.txt. When the files come in they are given a batch id and I just use the variable for that, $batchid. (which is just the name of the file). However, our HP does not handle extensions and what I need to do is rename them. And because each client sends in multiple files I need to distinguish between them. Basically, I am scripting so that they get forwarded to a temp file and then ftp to our HP3000 from there (I have different scripts for each file that comes in). My if statements looks something like this:
if [ $batchid = *.[Tt][Xx][Tt] ]
then
....
elif [ $batchid = *.[Kk] ]
then
.....
fi
fi
Again, the batchid is the variable for the file coming in to the system. My conditional staements aren't executing and I was wondering if someone knew where I was losing it? Thanks

9 replies

by lyons74

Few things you may have tried to do:

1. Run in verbose to see where the error is occuring.
2. Check your result variable to see what error code it's leaving with.

Without seeing the actual script and trying to recreate it. May I suggest using the awk comand and more command. Use awk to read the file name, shorten it, and set it to a changing variable like chName. The use "more" to write out the contents of the original file to a file named $chName.

Just an idea for 5:00 PM on a Wednesday.

Mike

by gregsptl

Hi,
cut from ---- to end and make executable i.e. chmod +x xx

filename xx

----
#!/usr/bin/ksh

TT=$1

# use arg 1 as option i.e. ./xx.sh sdf.txt
# where xx.sh is this file
# and sdf.txt is the file whose extension we
# are testing
# this was your $batchid

ext=`echo $TT|awk -F. '{print $2}'`

# strip off the extenstion with awk $ext now
# is set to the file name's extension

rc1=`echo $ext|grep [Tt][Xx][Tt]`

# use grep regex to find out whether or not
# this extension exists in said file name

rc=$?

# rc = 0 if extension matches so
if [ $rc -eq 0 ] ; then
./executable $1
# run this filename against this sh?
fi

echo $rc

by ghislain.levesque

to find out where you are losing it, try this command>

sh -x your-script

you will be able to see all variable,test,etc...

hope this help

by gary_w

Don't forget to enclose your variable name in quotes:
if [ "$batchid" = ... ]

That way, if the variable is null, the shell will see it and test accordingly. Otherwise,
the test will fail with a:
test: argument expected.

Have a great weekend,
gary

This discussion is closed to new replies.