Here is what I am trying to do but I want to use python instead:
test=$(perl -e 'print "test"')
[webalert@localhost scripts]$ echo $test
test
Is there a python equivalent for perl -e ? Thx.
$ test=$(python -c "print 'hello'")
$ echo $test
hello
(as a side note...)
If you want to preserve newlines, use quotes in the echo:
$ test=$(python -c "for i in range(3): print 'hello'")
$ echo $test
hello hello hello
$ echo "$test"
hello
hello
hello
One final tip:
Perl lends itself to one liners a bit better than Python does. I tend to do something like this rather than coerce python into being a language it is not:
$ test=$(python -c "
> import math
> import sys
>
> for x in sys.argv[1:]:
> print '2pi R of {}={}'.format(x,float(x)*2*math.pi)
> " 1 2.4 5 6.6)
$ echo "$test"
2pi R of 1=6.28318530718
2pi R of 2.4=15.0796447372
2pi R of 5=31.4159265359
2pi R of 6.6=41.4690230274