This widget could not be displayed.
1845515 Members
3320 Online
110244 Solutions
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
This widget could not be displayed.
New Discussion
This widget could not be displayed.
This widget could not be displayed.

Re: If sentence ..

 
SOLVED
Go to solution
Manuales
Super Advisor

If sentence ..

hi ..
how cay i use if to indicate a sentence:
if $a (is bigger than) $b
then ...

"is bigger than" how can i indicate it in if sentence? is like -ge? is like -aq? what is, i don't remember it ...!!!

Thanks, Manuales.
27 REPLIES 27
Geoff Wild
Honored Contributor
Solution

Re: If sentence ..

if (( $a > $b ))

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Slawomir Gora
Honored Contributor

Re: If sentence ..


if [ $a -gt $b ]
then
...
fi
Peter Godron
Honored Contributor

Re: If sentence ..

Manuales,
if you type man test it will show some useful syntax.
Manuales
Super Advisor

Re: If sentence ..

thanks, buy if you use "test"

if test $a ...... Bb

whad do you put in space "...." ?

Tnanks, Manuales.
Oviwan
Honored Contributor

Re: If sentence ..

ksh:

for strings:
"=" equal
"!=" not equal

for numbers:
"-eq" equal
"-ne" not equal
"-gt" greater than
"-lt" less than

Regards
James R. Ferguson
Acclaimed Contributor

Re: If sentence ..

Hi Manuales:

I assume that you want the Posix (or 'ksh') shell syntax.

If so, see the man pages for 'test' and the man pages for 'sh-posix'.

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: If sentence ..

Manuales,

#!/usr/bin/ksh
a="A"
b="B"
if [[ "$a" > "$b" ]]
then
echo a greater than b
else
echo a not greater than b
fi

Rodney Hills
Honored Contributor

Re: If sentence ..

The "test" command and its equivalent "[ ... ]" syntax are different then the "[[ ... ]]" syntax used in "ksh".

A review of "man test" and "man ksh" will explain the differences.

For instance-
if [ ab = a* ]... is false since it is a string compare, whereas
if [[ ab = a* ]]... is true since it is doing a pattern match.

read the "man" pages for the details...

Rod Hills
There be dragons...
Manuales
Super Advisor

Re: If sentence ..

i'm using csh
why do i have next error?
serverunix:girl1 279> prueba.csh
if: Expression syntax.

my script is:
if ($bb = 1)
then
echo "hola"
else
echo "adios"
eif

why is the problem??

Thanks, Manuales.
Oviwan
Honored Contributor

Re: If sentence ..

try this:

if ($bb = 1) then
echo "hola"
else
echo "adios"
endif
Patrick Wallek
Honored Contributor

Re: If sentence ..

CSH is a completely different beast from sh or ksh.

Your script should be:

if ( $bb == 1 ) then
echo "hola"
else
echo "adios"
endif


Note the differences.

1st line -- You needs space between the open and close parentheses and the expressions. Also you needed a '==' (2 equal signs together) and the 'then' needs to be on the first line.

The last line of the script has to be 'endif'.

# man csh

will give you a LOT of info on the C-shell.
Manuales
Super Advisor

Re: If sentence ..

Thanks Patrick !!

i've worked with ksh ..

do you know if with csh you can make functiions llike ksh? it means, with ksh there is a variable specially to redirect a path where there will be functions .. do you kwno if this exists to csh?

Thanks, Manuales.
James R. Ferguson
Acclaimed Contributor

Re: If sentence ..

Hi Manuales:

The 'csh' shell is far inferior to the Posix ('usr/bin/sh'), 'ksh', and 'bash' shells.

See here, for one discussion of why *not* to use 'csh':

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Beyond, that, on HP-UX, for the 'root' account, in '/etc/passwd', NEVER specify any shell other than '/sbin/sh'. To do so will render your server unusable.

The Posix shell lives in '/sbin/sh' as a staticly-linked binary. As such, everything it needs to run is available in single-user mode (when '/usr' isn't mounted). The Posix shell in '/usr/bin/sh' is for general users and uses dynamically linked libraries in '/usr'. Non-root users should specify this shell in preference to '/sbin/sh'.

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: If sentence ..

I don't know that much about csh. I would advise you to read the man page if you must use csh.

I try to avoid it if at all possible. I much prefer the POSIX shell.
Manuales
Super Advisor

Re: If sentence ..

Hi !!
i have a problem,
running script manually is ok. but if i run it from cron i have an error:

/home/scripts/test.sh[21]: Syntax error at line 56 : `else' is
not matched.
why?

my script is:

#!/usr/bin/csh
set p_monit=/home/scripts/MONITORING
#--->12132005 This is to take correct name in script
cd /interface/Towers/exe/backup
set aa=`ls -l | grep earn | awk '{ print $NF }'`
set bb=`echo $aa | wc -w`
if ($bb == 1) then
mv $aa db.opo
chmod 775 db.opo
else
set mensaje=`cat $p_monit/mail_messages.txt | grep 01 | cut -d":" -f2`
echo $mensaje | mailx -s "01 ORANGE ALERT to Mexico" me@patito.com.mx
endif

Thanks, Manuales.

James R. Ferguson
Acclaimed Contributor

Re: If sentence ..

Hi Manuales:

The 'cron' environment is quite sparse, containing:

HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

That is, *any* variables that are usually in your profile or login file are *not* available unless you specifically include them.

Regards!

...JRF...

Manuales
Super Advisor

Re: If sentence ..

but problem is with if sentence, now i have this problem:

/home/scripts/test.sh[21]: Syntax error at line 21 : `if' is not matched

Thanks, Manuales.
Oviwan
Honored Contributor

Re: If sentence ..

Hi,

put su - user before the script in your crontab

Regards
Oviwan
Honored Contributor

Re: If sentence ..

Hi,

put su - USERNAME in front the script in your crontab

Regards
Geoff Wild
Honored Contributor

Re: If sentence ..

Try this, in cron :

(csh /path/to/yourscript > /tmp/yourscript.cronlog 2>&1 )

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Manuales
Super Advisor

Re: If sentence ..

but problem is with if sentence, now i have this problem:

/home/scripts/test.sh[21]: Syntax error at line 21 : `if' is not matched

or another error:

if: Empty if.

Thanks, Manuales.
James R. Ferguson
Acclaimed Contributor

Re: If sentence ..

Hi Manuales:

This is one of the reasons most of us avoid the 'csh'. 'cron' expects to process scripts with the Posix shell.

Wrap your 'csh' script thusly and then 'cron' it:

#!/usr/bin/sh
/path_to_your_csh_script

You can see this by doing this, assuming your 'csh' script is named "/tmp/manuales":

# sh /tmp/manuales
# csh /tmp/manuales

The first will syntax; the second will work.

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: If sentence ..

Have a read of:

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

And then change your script from csh to the posix shell - /usr/bin/sh

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Manuales
Super Advisor

Re: If sentence ..

Hi .. with if sentences, what is next meaning?

if [[ -s /home/file1 ]]
then
....
fi

what is the meaning of "s"?
do you know any link speaks about parameters to if: -gt, -n, etc?

Thanks, Manuales.