Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn script - file found check 4

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am modifying a AIX Korn shell script

#! /bin/ksh

cd /usr/lawson/lawson/ocft/edi/in/810
if [[ -s GHX81P.out]] then
cat GHX81P.out >> GHX810.out
fi
file=GHX810.out
sed -e "s/","/""/g" $file > file1
cp file1 $file

This errors on the "then". If I remove the "then" it errors on the "cat".

The -s should work, can somebody tell me what I am doing wrong?
 
Try this...
Code:
#! /bin/ksh

cd /usr/lawson/lawson/ocft/edi/in/810
if [[ -s GHX81P.out]]
[b]then[/b]
   cat GHX81P.out >> GHX810.out
fi
file=GHX810.out
sed -e "s/","/""/g" $file > file1
cp file1 $file
You either need to put the "then" on it's own line, or put a semicolon between the conditional and the "then".

 
You could also shorten it to this:

Code:
#! /bin/ksh

cd /usr/lawson/lawson/ocft/edi/in/810
if [[ -s GHX81P.out]]
then
   sed -e s/,//g GHX81P.out >> GHX810.out
fi

The quotes in the sed script are having no real effect there (You can't have " quotes inside another set of " quotes anyway), so I left those out. If you did really want to replace a comma surrounded by " quotes with a pair of " quotes, you could use sed -e 's/","/""/g'.

Annihilannic.
 
I purposely didn't rewrite the script since it was looking like a school assignment. I just addressed the syntax error. I had some problems with the quotes in the sed and the left over temporary file "file1".

If you look at the logic of what's being done, it can actually be reduced to the following while remaining functionally identical...
Code:
#!/bin/ksh

cd /usr/lawson/lawson/ocft/edi/in/810

sed -e s/,//g GHX81P.out >> GHX810.out
The "-s" test is to see if the file is non-empty. Sed doesn't care if the file's empty or not. If it has something in it, it will be edited and appended. If there's nothing in it, sed will still run, but nothing will be appended because it's an empty file.

I guess if GHX81P.out doesn't exist, and error will result, but the file GHX810.out will still end up as desired.

 
Indeed... my philosophy is to avoid error messages appearing if they can be reasonably expected under normal operation, or handle them explicitly with a "no file today, doing nothing!" informational message.

jcarrott... is your nick inspired by your own real name? Or the comedian?

Annihilannic.
 
First, this is for work and being in my 60's school is long past.

Second, thank you for the comment about the name, the comedian is a distant family memeber. The name is John Carrott so the nick name (handle) seemed to work for me.

Third, and more to the topic, I was using the -s test to attempt to avoid the error message. The sed statement was working just fine. The point was that sometimes I have 2 input files and sometimes only one. Either can be missing, so I wanted to do a cat that would end up with both or either in the one file, with no errors. I wanted this to run clean. So removing the if does not help.
 
The other point is that I need to edit both files for commas. The reason is that the next step is to translate this input file and the resulting translated file is a CSV file. The commas if still in the name or description fields would alter the meaning of the result.
 
I changed the code to:

#! /bin/ksh

cd /usr/lawson/lawson/ocft/edi/in/810
if [[ -s GHX81P.out]]
then
cat GHX81P.out >> GHX810.out
fi
sed -e s/,//g GHX810.out >> GHX810.out

I am still getting the same error

./ghxFileEdit[4]: 0403-057 Syntax error at line 6 : `then' is not expected

This is running on a AIX system. I don't know if that amkes a differnece.

Can somebody tell what I did wrong??
 
Hi

Whitespaces are sometime important. For example in this case.
Code:
[b]if[/b] [teal][[[/teal] -s GHX81P[teal].[/teal]out[highlight] [/highlight][teal]]][/teal]
[b]then[/b]
   cat GHX81P[teal].[/teal]out [teal]>>[/teal] GHX810[teal].[/teal]out
[b]fi[/b]

Feherke.
 
why not simply this ?
Code:
[ -s GHX81P.out ] && cat GHX81P.out >> GHX810.out

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
or this?

[tt]cat GHX81P.out >>GHX810.out [red]2>/dev/null[/red][/tt]

Sure, your cat will scream/meow/whatever... upon not finding the file but you simply ignore the resulting error message.


HTH,

p5wizard
 
Since we're thrashing this topic... :) cat -s (silent) has a similar effect on most Unix OS, unfortunately not on Linux though where they have chosen to use the option for different functionality (squeeze blanks).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top