- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script Help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2009 04:50 AM
тАО12-02-2009 04:50 AM
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
Solved! Go to Solution.
- Tags:
- Test
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2009 05:06 AM
тАО12-02-2009 05:06 AM
Re: Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2009 05:25 AM
тАО12-02-2009 05:25 AM
SolutionOK 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2009 05:42 AM
тАО12-02-2009 05:42 AM
Re: Script 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-02-2009 05:54 AM
тАО12-02-2009 05:54 AM
Re: Script 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?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2009 12:53 AM
тАО12-03-2009 12:53 AM
Re: Script Help
>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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2009 06:35 AM
тАО12-03-2009 06:35 AM
Re: Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2009 04:48 AM
тАО12-04-2009 04:48 AM
Re: Script Help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2009 05:59 AM
тАО12-04-2009 05:59 AM
Re: Script Help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2009 06:28 PM
тАО12-04-2009 06:28 PM
Re: Script Help
>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