0

i have thoroughly searched for an answer before asking my question. i searched google and stackoverflow for "stderr to function failed compare -file" i found on google these: http://mywiki.wooledge.org/BashPitfalls ,

Redirect stdout and stderr to Function ,

echo that outputs to stderr

but none of them really answers my question. they focus on how to transfer the stderr correctly rather than how to compare the error string.

im having trouble to understand why the compare doesn't work and how to fix it.

also if the input to the function is not from stderr it compares successfully. if the input to the function does come from stderr of other command it compares successfully

im sorry but my question is very specific to these conditions so i don't know how many people will benefit from the answer (probably why i didn't find the answer myself)

and now to the question itself:

this is my function:

helpPrintError(){
  read line
  error='Pseudo-terminal will not be allocated because stdin is not a terminal.'
  if [[ "$line" == "$error" ]]; then
    :
  else
    echo -e "\e[31m\e[1m$line\e[0m"
  fi
}

this is how i tried to make this work:

( cat $commandsFile | sshpass -p "$userPswrd" ssh -o StrictHostKeyChecking=no admin@${mainIP} > $resultsFile ) 2>&1 | helpPrintError

well it's only one way i tried to solve this. i tried in many other ways.

result:

Pseudo-terminal will not be allocated because stdin is not a terminal.

problem and intent: i want to see (in bold red) any error messages that are not: "Pseudo-terminal will not be allocated because stdin is not a terminal."

problem is i fail to filter the error message time after time. i tried alot of variations to solve it but no success.

additional backgroung:

  • in $commandsFile i have commands for the ssh server to execute.

  • i have no control over the ssh server keys so i use sshpass and connect with $userPswd and $mainIP

  • results of the commands executed on the server are (to be) written to $resultsFile

  • any error messages from ssh attempts are to be filtered as in the function (one error message type is not to be seen.others are to be shown in bold red)

  • i tried the -t -t option on ssh but thats just messing my commands results

any suggestions and solutions will be welcome and appreciated!

Community
  • 1
  • 1
liron t
  • 11
  • 1
  • To summarize your problem: you wish to run a remote command. The error message "Pseudo-terminal will not be allocated because stdin is not a terminal." on stderr should be supressed. All other messages on stderr should be highlighted in red. All messages on stdout should be reported normally. – Vorsprung Feb 20 '15 at 14:05
  • @Vorsprung thats correct – liron t Feb 20 '15 at 14:17
  • You redirect your error to resultsfile. You need to redirect stderr first. – stark Feb 20 '15 at 18:25
  • Firstly I think your function `helpPrintError()` needs a loop to process ***every line*** from `stderr`. Otherwise, only the first one is processed. – Jdamian Feb 20 '15 at 18:31
  • Please, use `2>/tmp/stderr.txt` instead of the pipe in order to check the error messages are being redirected properly. – Jdamian Feb 20 '15 at 18:34
  • @stark should it be like this? `( cat $commandsFile | sshpass -p "$userPswrd" ssh -o StrictHostKeyChecking=no admin@${mainIP} 2>&1 > $resultsFile ) | helpPrintError` @Jdamian true but not solves my main issue. thanks for the pointer @Jdamian i followed your second tip and i found in the stderr.txt file this line: `Pseudo-terminal will not be allocated because stdin is not a terminal.` – liron t Feb 21 '15 at 13:26
  • Please, check in the file /tmp/stderr.txt if the error message that you are interested contains a traling blank (a blank space after the last dot). – Jdamian Feb 21 '15 at 14:49
  • @Jdamian no blank space after the last dot – liron t Feb 21 '15 at 18:55
  • ok, i found what is the difference between the stderr line feed and my constant line.i did it with inserting these lines: – liron t Feb 21 '15 at 19:27
  • `1.echo "$line" > stderrorline.txt 2.echo "$error" > consterrorline.txt` in the case of them different. after that i edited each of the files putting a new line after every chararcter and then used the command diff.in the last char i got `. |.` which means there are white characters the.now i just want to know how i compare with white spaces stripped or how to add them to my constant line or how to remove them from the stderr line feed. – liron t Feb 21 '15 at 19:34
  • ok looks like i solved my own problem with some guidance of you guys. you might want to keep the solution: i just used `stdInLF="$(echo -e "${line}" | sed -e 's/[[:space:]]*$//')"` to get rid of that last char 0d 0a or just 0a – liron t Feb 21 '15 at 19:57

0 Answers0