Managing awk return code over SSH command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Managing awk return code over SSH command
# 1  
Old 09-20-2011
Managing awk return code over SSH command

Hello all,

This bellow code works. I'm just trying to find a simplified way to achieve this. I'm sure there is an easier way and it must be to simple for me to find.

Verify that the OS version is 6.1. If not exit the script. Of course if i just put a && exit 1 at the end of the ssh command it returns true so the script exits.

Thanks.

Code:
#!/bin/bash

OSVER=$(ssh -l root server1 "awk '\$7 != 6.1 {print \"NOTGOOD\"}' /etc/redhat-release")

if [ "$OSVER" == NOTGOOD ]; then
        echo "ERROR: This release is for RHEL 6.1"
        exit 1
fi

# 2  
Old 09-20-2011
Code:
OSVER=$(ssh -l root server1 " grep -q '6.1' /etc/redhat-release && echo OK || echo NOTOK")
[ "$OSVER" = "NOTOK" ]  && echo 'bad OS version ' &&  exit 1

Is this what you mean? Using OK, NOTOK or words like that as returns from ssh calls is very clear and easy to maintain.
# 3  
Old 09-20-2011
Do you really need root to read /etc/redhat-release?? This seems dangerous.

You can use and check the return code instead of backticking text:
Code:
if ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" /etc/redhat-release
then
        echo "Found version 6.1"
else
        echo "Not version 6.1"
fi

# 4  
Old 09-21-2011
of course i don't need root for that. That was mainly for testing purposes. Good catch tho :-)

What i'm trying to do is get rid of the if loop.

[test the remote host for good version] - [good do nothing/not good exit]

I don't even need to manage the return code. Just exit if it doesn't find 6.1

A cleaner way than with an if loop.

Something like that....

Code:
[[ ssh -l user server1 "awk '\$7 !=6.1 {print \"NOTOK\"}' /etc/redhat-release" ]] && exit

# 5  
Old 09-21-2011
what is an "if loop"?

You can use my example directly since the logic for 'if program ; then ; else somethingelse ; fi' is the same logic for 'program || somethingelse'

Code:
ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'" || exit 1

# 6  
Old 09-21-2011
i get the logic behind your example but the exit code (on the remote host) somehow doesn't get back to the shell (on the local host).

It hangs and of course when i kill it it gets exit again.

Replacing the || exit 1 by || echo test from your example returns me

Code:
# ssh -l user server1 "awk '\$7 == /6.1/ { exit 0 } END { exit 1 }'" || echo test


ctrl-c
Killed by signal 2.
test
bash#

# 7  
Old 09-21-2011
Somehow the filename got left off of that. awk was trying to read data from standard input.

Code:
ssh -l username host awk "' \$7 == \"6.1\" { exit 0 } END { exit 1 }'"  /etc/redhat-release || exit 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

Losing carriage return (X0D) after running awk command

Hi Forum. I'm running the following awk command to extract the suffix value (pos 38) from the "AM00" record and append to the end of the "AM01" record. awk 'substr($0,13,4)=="AM00" {SUFFIX = substr($0,38,2)} substr($0,13,4)=="AM01" {$0 = $0 SUFFIX} 1' before.txt > after.txt Before.txt:... (2 Replies)
Discussion started by: pchang
2 Replies

3. Shell Programming and Scripting

[solved] awk: return code from system() always 0 ??

Hi all, I currently have the following problem: In an awk script, I am calling a predifend function from the END{} and handing over a command string. This string arrives flawless and is executed like this: function send_msg( cmd_str ) { ... (7 Replies)
Discussion started by: zaxxon
7 Replies

4. Shell Programming and Scripting

refine awk command in replacing carriage return

Hi, need your help in below,I have 4 types of file need to be processed so that it will replace carriage return in Remarks column with <:::> Remarks column position may varies in different types of file. sample file: col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10... (8 Replies)
Discussion started by: agathaeleanor
8 Replies

5. Shell Programming and Scripting

script submitted using ssh does not return to command prompt

Gurus, I have written a shell script to backup a database and applications. This is how the script works: 1. As applications user (say applmgr), the main backup script, say backup.sh, is kicked off. 2. backup.sh does ssh to oracle user and kicks off the database backup using nohup and... (1 Reply)
Discussion started by: sunpraveen
1 Replies

6. UNIX for Dummies Questions & Answers

Command does not want to return on ssh

Hi, I am trying to use this command to backup my remote server: ssh ftp nice -19 bru -cXiAf mail-02:/dev/nst1 /etc When I run I get: connect to address 205.150.86.5 port 544: Connection refused connect to address 205.150.86.5 port 544: Connection refused trying normal rsh... (1 Reply)
Discussion started by: mojoman
1 Replies

7. Shell Programming and Scripting

Return code of command assigned to variable

How do I evaluate the result of a command assigned to a variable?? Example: var1=`cmd` rc=$? rc will be the result of the assignment rather than cmd since it executes after. How do I evaluate the result of the command itself? Cheers..:confused: (2 Replies)
Discussion started by: browndr
2 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

return code of a unix command

How to find out whether the command I executed is successful or unsuccessful(at commandlinet) Eg: say i execute the following command at command line rm * How do i find out whether my previous command is a success or failure. Thankyou. Best Regards, Ram. (1 Reply)
Discussion started by: ramky79
1 Replies

10. UNIX for Advanced & Expert Users

Move command return with exit code of 2

I have a script which loads data files into Oracle and then moves each file into a 'processed' directory when each file has finished loading. Last night I found that the script was failing on the mv statement (with a return code 2) and the following message, mv: cannot access... (1 Reply)
Discussion started by: handak9
1 Replies
Login or Register to Ask a Question