Operating System - HP-UX
1753925 Members
9340 Online
108810 Solutions
New Discussion юеВ

warning in a KSH script , which uses for HP-UX/LINUX

 
SOLVED
Go to solution
Billa-User
Regular Advisor

warning in a KSH script , which uses for HP-UX/LINUX

hello,
i am working mainly with KSH/HP-UX (11.31/ KSH Version ksh-88). but i have extend a KSH script for Linux (SLES 10 / KSH Version ksh-93t).

i check my scripts for warnings,errors with "ksh -n <script>"

for following statement i get warnings for Linux?
is HP-UX KSH or LINUX KSH right ?

i want to check , if a environment variable has only digit(s) :

Linux warning:
warning: line 121: $ not preceded by \

statement:
echo "${CHECK_DIGIT}" |grep -q "^[0-9]*$"

if [ "$?" != "0" ]
then
echo "${CHECK_DIGIT} must be a numeric-integer value"
exit 2
fi
6 REPLIES 6
Matti_Kurkela
Honored Contributor
Solution

Re: warning in a KSH script , which uses for HP-UX/LINUX

The message is a warning only: the shell understands what you mean, but wants to tell that it might be better to do something in a different way.

I guess the warning comes from this:
> echo "${CHECK_DIGIT}" | grep -q "^[0-9]*$"

In this, it would be the last $ sign at the end of search expression of the grep command: when seeing an unescaped $ inside double quotes, the shell expects to see a variable expansion of some sort. But since $" is not a valid variable expansion, the shell figures you meant a literal dollar sign, and the double quote is the end quote for the search expression.

If you don't need to have any variables expanded inside grep's search expression, and the search expression includes characters with a special meaning to the shell (like "!" or "$"), then you might want to use single quotes instead of double quotes.

echo "${CHECK_DIGIT}" | grep -q '^[0-9]*$'

MK
MK
Billa-User
Regular Advisor

Re: warning in a KSH script , which uses for HP-UX/LINUX

> echo "${CHECK_DIGIT}" | grep -q '^[0-9]*$'

thank you very much, you fix my problem. and i learned more about KSH
Dennis Handly
Acclaimed Contributor

Re: warning in a KSH script , which uses for HP-UX/LINUX

>echo "${CHECK_DIGIT}" | grep -q "^[0-9]*$"

As MK said, you typically use single quotes around grep/sed patterns that don't contain variables. Especially if they contain backslash.

>if [ "$?" != "0" ]

You should do a numeric compare on the exit status:
if [ $? -ne 0 ]; then
Billa-User
Regular Advisor

Re: warning in a KSH script , which uses for HP-UX/LINUX

a new question about KSH:

i wrote, i check my scripts for warnings,errors with "ksh -n <script>"

i changed my shell script, but how i change this warning ?
example statement:

teststring="aaa"
echo "aaa " | grep -e "${teststring} *$"

This is Linux warning:
warning: line 1: $ not preceded by \

i can not change " to ' in this case ?!?

is "ksh -n" a good way to check script for HP-UX, LINUX for the right syntax?

HP-UX doesn't show a warning !
James R. Ferguson
Acclaimed Contributor

Re: warning in a KSH script , which uses for HP-UX/LINUX

Hi:

> i changed my shell script, but how i change this warning ?

You can't, unless, perhaps, you managed your own, local message catalog. Welcome to the subtle differences between GNU, HP-UX and the other Unixes. Try AIX sometime. Its messages are even more different and contain "standard" message numbers meaningful only to AIX.

You don't want to concern yourself with the exact error/warning message text but rather the return code. Return codes should be consistent among the various UNIX and LINUX implementations. One similar example is with the 'ftpd' daemon where you are warned by the documentation not to rely on the exact text of the message, but rather examine the three-digit response code.

> is "ksh -n" a good way to check script for HP-UX, LINUX for the right syntax?

This checks for *errors* not warnings. Of course, syntactically error free code still doesn't necessarily execute without warnings or unforeseen consequences :-)

Remember that the shell is *interpreted* code, and thus to some extent only until a piece is interpreted are all the states relative to the data known.

I do find, that the GNU tools are far richer than the standard HP-UX (or AIX, etc.) ones, though .

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: warning in a KSH script , which uses for HP-UX/LINUX

>warning: line 1: $ not preceded by \

You can simply add that "\" before the "$".

>I can not change " to ' in this case?!?

You can but that will stutter your quotes and make it less readable:
...| grep -e "${teststring}"' *$'