Sponsored Content
Top Forums Shell Programming and Scripting Test exit status of last cmd via ssh Post 302754853 by jack.bauer on Friday 11th of January 2013 10:16:37 AM
Old 01-11-2013
Quote:
Originally Posted by jim mcnamara
I don't get why you need ssh -n (-n option). But it won't affect what we are doing.
One easy way to get a status is to echo one. We use OK NOTOK

Code:
st=$(ssh  me@myhost  'some command >/dev/null 2>&1 &&  echo OK || echo NOTOK' )
[ "$st" = "OK" ] || echo "failed on $myhost" | mailx -s "$myhost error" me@home

cool that works. although first i thought you made an error with "st" = "OK" so i tried NOTOK first. i.e if the result of the previous cmd was not ok then report failure. but that didnt work, your line worked. why is that? how am i reading that?

this is how ive implemented it

Code:
  st=$(ssh $myuserid@$myhost 'mv $mypath/OrderDump* $mypath/archive > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$st" = "OK" ] || echo "failed on $myhost"

---------- Post updated at 09:51 AM ---------- Previous update was at 09:43 AM ----------

wait a sec, doesnt quite work well. it still echos the failure text even if the cmd is sucessfull

so in the below both myssh1 and myssh2 were OK (meaning to say they did what they were meant to i.e ls to show the files and mv to archive them)

Code:
   myssh1=$(ssh $myuserid@$myhost 'ls $mypath/OrderDump*  > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$myssh1" = "OK" ] || echo "No Order Dump files found on $myhost"

        myssh2=$(ssh $myuserid@$myhost 'mv $mypath/OrderDump* $mypath/archive > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$myssh2" = "OK" ] || echo "Nothing to clean up on $myhost"

yet the result was

1. files were archived off
2. output text below

No Order Dump files found on <host>
Nothing to clean up on <host>

---------- Post updated at 10:16 AM ---------- Previous update was at 09:51 AM ----------

and does the || apply if the ls returns

ls: /OrderDump*: No such file or directory
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

2. Shell Programming and Scripting

how to find status of last executed cmd in perl?

In shell we can find the status of last executed command by $? In perl what is the command to find the status of last executed command... Can any one please say??????????????? Thanks, Prabhu (1 Reply)
Discussion started by: prsampath
1 Replies

3. HP-UX

[Solved] ssh debug1: Exit status 254 problem

Hello; Am experiencing odd problem with ssh: ========= ssh -vvv remote_host : : debug2: channel 0: rcvd adjust 65536 debug2: channel_input_status_confirm: type 99 id 0 debug2: shell request accepted on channel 0 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0... (4 Replies)
Discussion started by: delphys
4 Replies

4. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

5. Shell Programming and Scripting

How to test for the ssh exit status in script?

Hello; I regularly run monitoring scripts over ssh to monitoring scripts But whenever a server is hung or in maintenance mode, my script hangs.. Are there anyways to trap exit status and be on my way ?? Looked at the ssh manpage and all I can see is a "-q" option for quiet mode .. Thank... (2 Replies)
Discussion started by: delphys
2 Replies

6. Shell Programming and Scripting

SSH Process monitoring and Exit Status evaluation

Hi All, I have a peculiar requirement as follows, I have a some hosts on which i have to run a script, so i am using the following code piece for i in $HOSTLIST do ssh ${i} "~/task.sh" done Now i want to run this same thing in parallel on all the hosts and then monitor the ssh process... (1 Reply)
Discussion started by: mihirvora16
1 Replies

7. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

8. Shell Programming and Scripting

Help to cd to a dirctory and not exit after cmd

cmd() { results=`eval $* 2>&1` val=$? #return the $* execution value } cmd "cd /home/tom || exit 0" how to realize two point it cd to "/home/tom" not exit from the script after the ----> cmd cd /home/tom || exit 0 (4 Replies)
Discussion started by: yanglei_fage
4 Replies

9. UNIX for Advanced & Expert Users

CMD to check status of the server using Wget

Hi All, Using Wget I'm able to get the status of the server.....only when the server is completely down or up.... but problem here in script is Suppose if the server got hang I mean to say that if the server is taking long time to login, for example normally the server takes 3 seconds to login... (3 Replies)
Discussion started by: manohar2013
3 Replies

10. Shell Programming and Scripting

Test with non-zero status does not exit shell

Hi there, I'm very used to use set -e to break my scripts if any command exits with a non-zero status. As a policy, I'm willingly expecting echo hello | grep a to break the script. The commands test 1 -eq $1 && echo hello exits with a non-zero status if $1 is not 1. BUT... It doesn't break... (2 Replies)
Discussion started by: chebarbudo
2 Replies
echo(1B)					     SunOS/BSD Compatibility Package Commands						  echo(1B)

NAME
echo - echo arguments to standard output SYNOPSIS
/usr/ucb/echo [-n] [argument] DESCRIPTION
echo writes its arguments, separated by BLANKs and terminated by a NEWLINE, to the standard output. echo is useful for producing diagnostics in command files and for sending known data into a pipe, and for displaying the contents of envi- ronment variables. For example, you can use echo to determine how many subdirectories below the root directory (/) is your current directory, as follows: o echo your current-working-directory's full pathname o pipe the output through tr to translate the path's embedded slash-characters into space-characters o pipe that output through wc -w for a count of the names in your path. example% /usr/bin/echo "echo $PWD | tr '/' ' ' | wc -w" See tr(1) and wc(1) for their functionality. The shells csh(1), ksh(1), and sh(1), each have an echo built-in command, which, by default, will have precedence, and will be invoked if the user calls echo without a full pathname. /usr/ucb/echo and csh's echo() have an -n option, but do not understand back-slashed escape characters. sh's echo(), ksh's echo(), and /usr/bin/echo, on the other hand, understand the black-slashed escape characters, and ksh's echo() also understands a as the audible bell character; however, these commands do not have an -n option. OPTIONS
-n Do not add the NEWLINE to the output. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), echo(1), ksh(1), sh(1), tr(1), wc(1), attributes(5) NOTES
The -n option is a transition aid for BSD applications, and may not be supported in future releases. SunOS 5.11 3 Aug 1994 echo(1B)
All times are GMT -4. The time now is 02:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy