1

I have an application which outputs a string with a number, like this:

output number is 20.

I have a code which parses output and cutting out only the number:

result=$(./my_script | awk 'print $4')
echo $result

the result output will be "20" as expected, but now, if I would try to use it as an integer, for example:

result=$((result+1))

then I will get an error:

13915: syntax error: operand expected (error token is "20")

Using it as a seq argument will also give an error

$(seq 0 $result) 
seq: invalid floating point argument: ‘\033[?25h\033[?0c20’

trying to print it with %q will give the same result:

printf '%q' $result
'\E[?25h\E[?0c20'

So, it looks like there are some unexpected characters in the string, but I'm not sure how to trim them?

Thank you!

7
  • 2
    The application has written ANSI escapes like "Make cursor visible" even though stdout is not a terminal. This is not canonical Unix behavior, and the application shouldn't be doing it. It's not supposed to be your responsibility to trim it. Which application is this? Commented May 10, 2018 at 3:36
  • 1
    Specifically, well-behaved programs will look at whether output is a TTY, and only put escape sequences in place if that check returns true (if the program is implemented in shell, this is accomplished with [ -t 1 ] for stdout, [ -t 2 ] for stderr; in C, the call is isatty()). This program is buggy, and you should report those bugs to its author. Commented May 10, 2018 at 4:38
  • @thatotherguy Actually, i run that script on other machine using:ssh user@host <<'ENDSSH'; result=$(./my_script | awk 'print $4'); echo $result; ENDSSH; Unfortunately, I can't make new lines in comment. Commented May 10, 2018 at 5:59
  • @CharlesDuffy As I mention in my previous comment - I run it in nested ssh session and try to capture all output, so, probably, I'm getting some garbage too. Commented May 10, 2018 at 6:04
  • 1
    Consider ssh -T to explicitly disable TTY emulation on SSH's part. BTW, echo $result introduces some bugs in and of itself, like replacing a * in the output with a list of files. Better to use echo "$result", or -- to remove everything but numbers -- echo "${result//[^[:digit:]]/}"... or just to avoid the command substitution altogether and let the pipeline write direct to stdout. Commented May 10, 2018 at 16:55

2 Answers 2

3

You can try to get the number by using Regex.

It worked for me:

#!/bin/bash

result=$(bash output.sh | sed 's/[^0-9]//g')
r=$((result+1))
echo $r

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

When dealing with a single line, sed (or any other external tool) is much higher overhead than bash's built-in string manipulation; for example, result=${result//[^[:digit:]]/}
0

Or, simply, change the field separator

result=$(./my_script | awk -F '[. ]' '{print $4}')
echo $result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.