Solved

Unix cmd line: capture return code

Posted on 2010-09-23
6
584 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
ID: 33744028
You could do
test -e filename ; echo $?
Sorry, I don't understand the "related" part above!
Please explain!
wmp
 
0
 
LVL 77

Expert Comment

by:arnold
ID: 33744264
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
ID: 33744315
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Watnog
ID: 33744322
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
ID: 33744334
See my comment!
 
0
 

Author Comment

by:Watnog
ID: 33744473
Yes thanks, that was it....
Pffff. Thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort command HPUX 11 75
cron job says it ran, no results 25 118
Linux "time" command output redirection 16 144
How to make a Bash alias that takes parameters 5 53
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

785 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