Need help redirecting output to a file including errors

Need help redirecting output to a file including errors if any,I have 2 script namely push.ksh and run.ksh, I'm scp'ing push.ksh to another server and executing remotely via run.ksh, the script run.ksh runs locally but does not capture any errors in "servername.out" file (I tried testing various failed conditions like the one below in push.ksh )and always echoes the message "echo "INFO: No Errors found" from run.ksh and moreover I could only see 'uname -n' and 'ls -ltr awk 'NR!=1 && NR!=2 {print $5}' values in servername.out (ofcourse this file is local)even though I'm redirecting both stderr and stdout

push.ksh

#!/bin/ksh

echo "`uname -n`"

echo "`ls -ltr awk 'NR!=1 && NR!=2 {print $5}'`

ls -l /jdfkds # to test fail condition

if [ $? -ne 0 ];then
	echo "ERROR: Unable to list files"
else
	echo "INFO: Files listed successfully"
fi

run.ksh

scp <servername> "/tmp/push.ksh"
if ssh <servername> "/tmp/run.ksh 2>&1" | tee "servername.out";then

	if grep -q -i "ERROR" "servername.out";then
		echo "ERROR: Errors found"
	else
		echo "INFO: No Errors found"
	fi
else
	echo "INFO: Success"
fi

push script had errors!

push.ksh

#!/bin/bash
uname -n

#what is this line supposed to do?
#echo "`ls -ltr awk 'NR!=1 && NR!=2 {print $5}'`

ls -l /jdfkds # to test fail condition

if [ $? -ne 0 ];then
  echo "ERROR: Unable to list files"
else
  echo "INFO: Files listed successfully"
fi

--ahamed

---------- Post updated at 03:37 PM ---------- Previous update was at 03:34 PM ----------

run.ksh

#!/bin/bash

if ssh <servername> "/tmp/push.ksh 2>&1" | tee "servername.out"
then
  if grep -q -i "ERROR" "servername.out";
  then
    echo "ERROR: Errors found"
  else
    echo "INFO: No Errors found"
  fi
else
  echo "INFO: Success"
fi

--ahamed

1 Like

Do any of these options work?

BASH Programming - Introduction HOW-TO: All about redirection

Ahamed:
Thanks for correcting the typos it should have been the below line in push.ksh, it just prints the file or directory names and also /tmp/push.ksh in run.ksh script:
echo "`ls -ltr | awk 'NR!=1 && NR!=2 {print $9}'`"

COKEDUDE:I tried different variations to redirect stdout and stderr with/without tee but for any condition it just goes straight to echo "INFO: No Errors found" part instead of "ERROR: Errors found" whenever it encounters the errors.

so we good? are you able to proceed?

--ahamed

I just had the typos while posting the script here and still having same issues as replied to COKEDUDE in my previous reply.

Can you execute push.ksh in the remote server and see if it working?

And the syntax is incorrect. If you want to print it, it should be

ls -ltr | awk 'NR>2 {print $9}'

--ahamed

using &>filename will redirect your STDIN adn STDERR to given filename.

Ahamed...The syntax works for me...

$ echo "`ls -ltr | awk 'NR!=1 && NR!=2 {print $9}'`"
list
list.sort
sudo.out
tcp.out
X11.out
open.out
AX.out
AY.out

Can you execute push.ksh in the remote server manually and see if it working? In case there are any errors. paste the output of

bash -x push.ksh

--ahamed

push.ksh itslef runs successfuly on the remote server as seen below, I ran it for both the instances success and error (by including the line in red to test the error part) and run.ksh executes locally its if..then..else conditions based on the result of push.ksh on remote server.

$ bash -x push.ksh
++ uname -n
+ echo testserver
testserver
++ ls -ltr
++ awk 'NR!=1 && NR!=2 {print $9}'
list
list.sort
sudo.out
tcp.out
X11.out
open.out
AX.out
AY.out
push.ksh
+ '[' 0 -ne 0 ']'
+ echo 'INFO: Files listed successfully'
INFO: Files listed successfully



$ bash -x push.ksh
++ uname -n
+ echo testserver
testserver
list
list.sort
sudo.out
tcp.out
X11.out
open.out
AX.out
AY.out
+ ls -l /jdfkds
ls: 0653-341 The file /jdfkds does not exist.
+ '[' 2 -ne 0 ']'
+ echo 'ERROR: Unable to list files'
ERROR: Unable to list files