Solved

Unix cmd line: capture return code

Posted on 2010-09-23
6
587 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 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 78

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

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