I am trying below commands
python -c 'import sample; sample.Functionname()'
python -c 'import sample; sample.printFxn("helloWorld")'
Both of these work well but when I pass a variable as an argument, I get the following error.
File "<string>", line 1 import sample; sample.printFxn($filename) SyntaxError: invalid syntax
What is the proper way to pass a variable as an argument to a python function from bash?
Don't interpolate string variables into the command; pass the value as an argument to the Python script.
python -c 'import sys, sample; sample.printFxn(sys.argv[1])' "$fileName"
Single quotes (') prevent the shell from parsing a string an evaluating variables. You can use double quotes instead so the shell will evaluate your variable:
python -c "import sample; sample.printFxn('$fileName')"
Related
I am trying to do a string printing with python -c flag, e.g.
python3 -c "print('Hello World')"
So now I wanna substitute an argument with pipe, e.g. echo "Hello World" | python3 -c "print($1)"
the pipe is to take output from previous command and take it as input to next command, if I am not wrong, this is possible? But I think I got syntax error which I cannot find any source of this
I also bumped into question previously asked, but the solution required python imports and .py file depends on how we run this, I understand but I just wanna get it in a line of command in linux shell
If your input is always single line then you should be able to harness input function for example
echo "Hello World" | python3 -c "print(input().upper())"
would output
HELLO WORLD
I am trying to write this small python program to execute with python through the -c option:
python -c "import sys;if 2==sys.version_info.major: raise RuntimeError('Must use python3')"
However, this is raising a syntax error:
File "<string>", line 1
import sys;if 2==sys.version_info.major: raise RunTimeError('Must use python3')
^
SyntaxError: invalid syntax
is there a way to write this such that it does work in the above? And if it's invalid, is there a canonical reference to what syntaxes are allowed in -c executed code?
I am doing this in a Makefile.
Literal newlines are perfectly valid inside single-quoted strings in POSIX shells:
python -c '
import sys
if 2 == sys.version_info.major:
raise RuntimeError("Must use python3")
'
This means you aren't dependent on having bash, ksh93 or zsh with the $'' extension.
If this is in a Makefile:
define python_script
import sys
if 2 == sys.version_info.major:
raise RuntimeError("Must use python3")
endef
test:
python -c "$$python_script"
You can use \n.
python -c "import sys"$'\n'"if 2 == sys.version_info.major:"$'\n'" raise RuntimeError('Must use python3')"
...this assumes you're using bash or some closely related shell. But otherwise obviously you can still just have newlines in the string, especially if you're calling python -c from a program using exec or something.
how can i run an easy python script and save it in a file but directly in linux command line:
fox#fox:/opt/gera# python -c print "aaaaa" > myfileName
but it is just print nothing instead of "aaaaa".
You have to quote the whole command:
python -c 'print "aaaaa"' > myfileName
Otherwise you execute print in Python (which, in Python 2 prints a linebreak and in Python 3 does nothing since you'd just evaluate the function print without calling it) and pass aaaaa as an argument to the script.
You need to put quotes around the code.
python -c 'print "aaaaa"' > myfileName
python -c 'print "aaaaa"' > myfileName
in your example python is running python -c print and giving "aaaaa" as an argument.
man python:
-c command
Specify the command to execute. This terminates the option list (following options are passed as arguments to the command).
passed as arguments to the command -- means everything after the command is available as sys.argv:
$ python -c 'import sys; print sys.argv' -a -b -c -d
['-c', '-a', '-b', '-c', '-d']
To make a single argument (command in terms of the man excerpt above) out of a list of arguments you just take these in quotes:
$ python -c 'print "aaaaa"'
aaaaa
A single command passed to Python interpreter via '-c' option works perfectly:
$ python3 -c "print('Hi')"
Hi
$
However, I couldn't figure out how to send multiple lines (from the Windows command prompt), since the statements are grouped by indentation. Passing multiple lines in a single line will not work.
A Linux terminal supports multiple lines with newline character as argument:
$ python3 -c "
>import sys
>print(sys.argv[0])"
$ -c
But in Windows it is not possible because the command get terminated with a newline
$ python3 -c "
$
How do I make this work in the Windows command prompt?
I am just checking out the options of Python interpreter, so I am not looking for any workaround solution!
You could use the ^ operator here. Something like
C:\>python3 -c "print('Hai')"
Hai
C:\>python3 -c "import sys; print(sys.argv)"
['-c']
C:\>python3 -c ^
More? "import sys; ^
More? print(sys.argv)"
['-c']
And,
C:\>python3 -c ^
More? "if 2*2 == 4: ^
More? print('Testing')"
Testing
And,
C:\>python3 -c ^
More? "if True: ^
More? print('First Line'); ^
More? print('Second Line')"
First Line
Second Line
I'm trying to have python generate the input parameter to my command line program (Linux), and simply cannot get it to work.
I know it is something to the effect of
./heap0 (python -c 'print "A"*72)
but that does not work....
Try $(). It takes the output of a command and includes it as a value.
./heap0 $(python -c 'print "A"*72')