1752580 Members
3036 Online
108788 Solutions
New Discussion юеВ

Re: Script Help

 
SOLVED
Go to solution
Acxiom Unix Team
Frequent Advisor

Script Help

As someone who will admit i am utterly rubbish at scripting can someone possibly help with what i am guessing is not a difficult question..

I have been asked to look at a test script someone did that checks for the existance of a file on a remote server.

I have attached the script in a notepad document but i dont understand really what it is doing especially with the line that i think is trying to establish what the variable OK is..

So if anyone would be kind enough to offer some guidance as to what this script is doing particulary round the section at the end of line 3 and then why the 2 IF statements have different brackets.

Many Thanks
Where is my beer...??
11 REPLIES 11
Stephan._1
Trusted Contributor

Re: Script Help

Hi,

timestamp=$(date) # Put the actual date in a variabel
USER=sweb # define username sweb
OK=$(remsh uk208 -l $USER "ls /census/script/jobsub 1>&2 && echo ok || echo err") # list the file /census/script/jobsub on host uk208 via remshell if it is there, OK else err
if (( $? != 0 )) # this is crap it, it check for the errorlevel of the previous command with a wrong syntax, it should be if [[ $? -ne 0 ]]
...

feel free to ask, if you want to know more.

Stephan
Share what you know, learn what you don't.
Bill Hassell
Honored Contributor
Solution

Re: Script Help

timestamp is a variable that is assigned the current result of the date command. USER is a variable assigned the text string sweb.

OK is a variable that is assigned the result of everything inside $(...). Inside $(...) there is a remsh command to server uk208 which logs in as user sweb. ERverything inside the double quotes "" is run on the remote system uk208. Ths ls command simply returns the status of the named file.

The 2>&1 redirects error messages (such as file not found) to stdout rather than stderr. The && and || are true and false conditions -- if the ls command is successful, then OK is returned as the value for $(...) and if not, then err is returned.

The next line ((...)) is a bit non-standard but works OK. The ((...)) could be replaced with [[...]] as seen in the next test, or even [...] -- the results are the same in this context. The (( $?!= 0 )) tests the return code from remsh. If remsh fails, it could not connect or resolve the name uk208. If the return code from remsh ($?) is not zero then the message is sent using mailx with option -s being the subject of the email.

The last test checks $OK ($OK means substitute the contents of the variable OK) and mails the failure message using mailx.

In both tests, the return code from the script is -1 for problems and by default, 0 if the script reaches the bottom.


Bill Hassell, sysadmin
Acxiom Unix Team
Frequent Advisor

Re: Script Help

Thanks guys for the help.

I need to find some good places to learn this stuff as just get lost trying to understand what everything means...

Any good sites/books/forums you can recommend?

Thanks again.
Where is my beer...??
James R. Ferguson
Acclaimed Contributor

Re: Script Help

Hi Andrew:

> I need to find some good places to learn this stuff as just get lost trying to understand what everything means...Any good sites/books/forums you can recommend?

There are any number of good books on shell programming. Avoid the C-shell ('csh') which is dysfunctional. For HP-UX use the Posix shell ('/usr/bin/sh') which is the HP standard. It's very close to the Korn88 shell ('/usr/bin/ksh').

A free guide to begin with is:

http://docs.hp.com/en/B2355-90046/index.html

An excellent site for examples and code is:

http://www.shelldorado.com/

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Script Help

>svo: if (( $? != 0 ))
>with a wrong syntax, it should be if [[ $? -ne 0 ]]

This syntax is perfectly valid. It uses C syntax within (( )) for arithmetic expressions.

>Bill: The next line ((...)) is a bit non-standard but works OK.

This "if (())" is Posix standard, so no need to change it.

>as seen in the next test, or even [...]

If using [[ ]] or [ ], that != should be replaced by -eq, for arithmetic compare.

>the return code from the script is -1 for problems

This isn't really a good idea to use -1 for UNIX, since exit status is typically truncated to 0..255.
Stephan._1
Trusted Contributor

Re: Script Help

I'm always willing to learn something new but what he does is shorten the test command and if a look at 'man test' i find:

test(1)

NAME
test - condition evaluation command

SYNOPSIS
test expr

[ expr ]

DESCRIPTION
The test command evaluates the expression expr and, if its value is True, returns a Zero (true) exit status; otherwise, a nonzero (false) exit status is returned. test also returns a nonzero exit status if there are no arguments.
...
( expr ) Parentheses for grouping.
...

I have as well never seen an explanation to use () for test, but perhaps you can give me hand and point to some documentation.

Thanks in advance
Share what you know, learn what you don't.
Dennis Handly
Acclaimed Contributor

Re: Script Help

>svo: but what he does is shorten the test command and if a look at 'man test' i find:

No test(1) commands here? [ ] is the shorthand for that.
(( )) is sh/ksh arithmetic expression evaluation.

>I have as well never seen an explanation to use () for test

You use () if you have to evaluate things other than operator precedence:
if [ $a -eq 1 -a \( $b -eq 1 -o $c -eq 1 \) ]; then

And you can toss that and use C style:
if (( a == 1 && (b == 1 || c == 1) )); then
Stephan._1
Trusted Contributor

Re: Script Help

Hmm, ok :-)

So just to confuse me completely:

test $? != 0

[ $? -ne 0 ]

[[ $? -ne 0 ]]

(( $? != 0 ))

is all correct syntax and give the same result? In my opinion i should go with one version in a script and not mix it up.

Anyway thanks for the explanation.
Share what you know, learn what you don't.
Dennis Handly
Acclaimed Contributor

Re: Script Help

>svo: So just to confuse me completely:
>test $? != 0

This isn't valid for all possible cases. I.e. it is a string compare.

>[ $? -ne 0 ] [[ $? -ne 0 ]] (( $? != 0 ))
>is all correct syntax and give the same result?

These are valid. The first is more efficient than: test $? -ne 0