Question

I have a shell script script here as below:

#!/bin/bash
CPUSELECTION="1 386SX off \
              2 386DX on \
              3 486SX off \
              4 486DX off "
#dialog --backtitle "Select CPU" --radiolist "Select the cpu" 10 40 4 $CPUSELECTION
echo $CPUSELECTION

now my py file is

import pexpect

child = pexpect.spawn ('sh /tmp/test.sh')
child.expect('386DX')
fp = open('/tmp/test.txt', 'w')

print >> fp, "Before 386DX:", child.before
print >> fp, "After 386DX:", child.after


child.expect('486SX')
print >> fp, "Before 486SX:", child.before
print >> fp, "After 486SX:", child.after

fp.close()

The output of this script is

$ cat /tmp/test.txt
Before 386DX: 1 386SX off 2 
After 386DX: 386DX
Before 486SX:  on 3 
After 486SX: 486SX

From this I will get the text between some text.

But if I use dialog box

#!/bin/bash
CPUSELECTION="1 386SX off \
              2 386DX on \
              3 486SX off \
              4 486DX off "
dialog --backtitle "Select CPU" --radiolist "Select the cpu" 10 40 4 $CPUSELECTION
#echo $CPUSELECTION

I got the junk output.

Is there any way to get the plain text from the dialog box?

Shell Script with dialog box is given by third party so we cant change the script :(.

Please provide the solution without changing shell script.

Was it helpful?

Solution

The junk output that you're seeing is ANSI escape codes. You can strip them out with something like this:

ansi_codes = re.compile('\033\[[0-9;]+m')
def strip_ansi(with_ansi):
    return ansi_codes.sub('', with_ansi)

then, you can use

strip_ansi(child.before)

and you'll just see the text. It will still include the lines from the dialog frames, but that should be easier for you to handle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top