Solved

Unix cmd line: capture return code

Posted on 2010-09-23
6
583 Views
Last Modified: 2012-05-10
Hi EE,
This can't be very hard, yet I could not put my finger on it.
On several (>100) servers I need to check if a file is present.
A cmd to check that out can be done in an automated way (through TWS, job scheduling software) but I don't want to have a script for it put on each system.
Therefore a cmd is execute on each server, the output of which is written in a "job log". So far no problem.
What I need to see in that joblog is the return code.
If I work with a [test]  cmd I would need to see a 0 if exist, a 1 if not.
How would such a cmd look like?
test filename | echo $?  
returns always a 0 whether the file is there or not.

Related: I don''t think I can parse a script into a cmd line, can I? If so that woud solve my problem too...

Thanks for  your help!

I
0
Comment
Question by:Watnog
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 125 total points
Comment Utility
You could do
test -e filename ; echo $?
Sorry, I don't understand the "related" part above!
Please explain!
wmp
 
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
this presumes you are using publickeys or you would need to type in the password or set the variables to pass the password/passphrase for every ssh connection

cat file_list_of_hosts | while read host; do
ssh user@host "test -e \"filename\" \&\& echo \"filename exists on $host\""
done
0
 
LVL 9

Expert Comment

by:crazedsanity
Comment Utility
The easiest way to see if a script passed/failed is to "chain" commands together.  A really simple test would be:

===============
user@server:~$ /bin/false && echo "ok"
user@server:~$ /bin/true && echo "ok"
ok
user@server:~$
===============

The "/bin/false" returns non-zero, while "/bin/true" returns zero.  Otherwise, use the test program (i.e. "test -e /path/to/file"): there's a special variable ($?) that contains the return value.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Watnog
Comment Utility
The TWS software ensures I get on the server, there's no problem there.
TWS submits a 'job' on that server and the output will be in the job log. No problem either.
It's just the syntax of the command, that either should be an alternative to a script (the 'related' question) else a cmd that produces a return code that will be captured in the job log.
When I do a [test filename]  on the cmd line the return code is not visible, so neither it is in the job log.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
See my comment!
 
0
 

Author Comment

by:Watnog
Comment Utility
Yes thanks, that was it....
Pffff. Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now