0

I'm very confused about what is wrong with the bash variable ${fwhm} in this code. The script CRNitschke.py returns 3 floating point numbers which I need to have for use in this bash script:

  fwhm_dt_ft=(`/u/ki/awright/thiswork/eyes/CRNitschke/CRNitschke.py ${file}`)
  fwhm=${fwhm_dt_ft[0]}
  dt=${fwhm_dt_ft[1]}
  ft=${fwhm_dt_ft[2]}

after I run this, everything in bash looks normal:

~$ echo "fwhm= " ${fwhm}
fwhm=  0.52
~$ echo "dt= " ${dt}
dt=  139.960346726
~$ echo "ft= " ${ft}
ft=  15.2307051015

except when I have these as inputs elsewhere there are problems. For example for the python script print_args.py given by:

#! /usr/bin/env python
import sys
print "sys.argv=",sys.argv

I get wierd looking things like this:

~$ ./print_argv.py ${fwhm} ${dt} ${ft}
sys.argv= ['./print_argv.py', '\x1b[?1034h0.52', '139.960346726', '15.2307051015']

does anyone know what could be up with the fwhm variable or how I could fix it?

It's strange because when I just type things in it's fine:

~$ abc=(0.52 139.960346726 15.2307051015)
~$ ./print_argv.py ${abc[0]} ${abc[1]} ${abc[2]}
sys.argv= ['./print_argv.py', '0.52', '139.960346726', '15.2307051015']

-Adam

NEXT DAY UPDATE: ... OK, here is a little added info. I ran this command in a script rather than at the command line and the output showed this:

+ echo 'fwhm= ' '0.58'
fwhm=  0.58
+ echo 'dt= ' 114.988365258
dt=  114.988365258
+ echo 'ft= ' 12.190067533
ft=  12.190067533

you can see that in the echo line, the numerical value of fwhm is in quotes and that is not the case for dt and ft. I have no idea why this would be the case, since python prints everything out the same. Any help would be appreciated!

arwright3
  • 381
  • 2
  • 3
  • 13
  • The `\x1b[?1034h` is an escape sequence for the terminal in some shape or form; the `\x1b` is the escape. Maybe it is coloration; maybe it is something else. A lot depends on what the `?` actually represents. – Jonathan Leffler Apr 29 '14 at 01:48
  • @JonathanLeffler Is there any kind of command that I can use to find out more of what exactly is going on with `${fwhm}` and what exactly this escape sequence is representing? – arwright3 Apr 29 '14 at 19:26
  • You can use, for example, `echo "{fhwm}" | od -c` (or any similar character analysis program) to identify what's in the variable. For the meaning of the sequence, you can investigate [ANSI escape code](http://en.wikipedia.org/wiki/ANSI_escape_code) at Wikipedia. – Jonathan Leffler Apr 29 '14 at 20:01

2 Answers2

0

Try this code:

import re
escape = re.compile(r'\x1b\[\?\d+h')
sys.argv = list([escape.sub('', x) for x in sys.argv])

This is the similar problem of How can I remove the ANSI escape sequences from a string in python

Community
  • 1
  • 1
emesday
  • 6,078
  • 3
  • 29
  • 46
  • this would work fine if I'm only using this env variable in python code, but I'm actually using it in multiple different places, so I need `${fwhm}` to not have that escape sequence in the shell – arwright3 Apr 29 '14 at 19:24
0

I figured it out...kinda. Turns out it's just that the first value, for some reason, has that escape sequence and is going to give you troubles everytime. I added in a 4th value to return called out that's just a dummy that I don't use. That way fwhm isn't in the first position and everything is fine, see:

+ out_fwhm_dt_ft=(`/u/ki/awright/thiswork/eyes/CRNitschke/CRNitschke.py ${file}`)
+ out='0'
+ fwhm=0.54
+ dt=134.840822967
+ ft=12.3659326799
+ echo 'out= ' '0'
out=  0
+ echo 'fwhm= ' 0.54
fwhm=  0.54
+ echo 'dt= ' 134.840822967
dt=  134.840822967
+ echo 'ft= ' 12.36

Thanks, -Adam

P.S. I would still be curious if someone could tell my why bash interprets python output in this way.

arwright3
  • 381
  • 2
  • 3
  • 13