• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

Unix cmd line: capture return code

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
Watnog
Asked:
Watnog
1 Solution
 
woolmilkporcCommented:
You could do
test -e filename ; echo $?
Sorry, I don't understand the "related" part above!
Please explain!
wmp
 
0
 
arnoldCommented:
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
 
crazedsanityCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WatnogAuthor Commented:
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
 
woolmilkporcCommented:
See my comment!
 
0
 
WatnogAuthor Commented:
Yes thanks, that was it....
Pffff. Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now