?
Solved

Unix cmd line: capture return code

Posted on 2010-09-23
6
Medium Priority
?
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 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 79

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

762 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