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')
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 access a python function from the command line, and I would like to write such a command that will print the output in the terminal. The below doesn't work. What could I change?
python -c 'from laser import Laser; laser = Laser();l = laser.embed_sentences("hello", lang = "en").shape == (1, 1024); print(l)'
(base) ~ % python -c 'print("hello, world")'
hello, world
Printing works fine for me when running python through python -c. Are you sure your terminal isn't truncating your output by omitting the last (and in this case, only) line? You could try creating a single line file (no newline at the end) and then running cat [filename] (which is how I sometimes discover that my terminal is doing this)
-c cmd : program passed in as string (terminates option list)
That is the correct flag to be used. This must be a CLI config issue. Or the script is taking longer than you are expecting to run and it appears no output is generated.
Does python -c 'print("hello")' work?
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
I want to pass the name of a file to a python script while I'm running it from the command line. I'm trying this clear command:
cat enwiki-latest-pages-articles.xml | python WikiExtractor.py -b 500K -o extracted
however, it gives an error:
'cat' is not recognized as an internal or external command, operable program or batch file.
Thanks in advance.
It seems like you're running the command in Windows. In windows, there's no cat installed unless you installed.
You can use type command instead:
type enwiki-latest-pages-articles.xml | python WikiExtractor.py -b 500K -o extracted
The correct way would be python WikiExtractor.py -b5 -o extracted -f enwiki-latest-pages-articles.xml.
And use sys.argv array of input arguments of python command from sys command.
This may help:
http://www.tutorialspoint.com/python/python_command_line_arguments.htm
I am trying to use python -c command line option but cannot seem to make it work.
What is the right way to use it?
Sometime it is really useful to store the whole command and one line and make an alias for it then going into the interactive mode.
The following gives no output
-bash-3.2$ python -c "
import hashlib
hashlib.md5('test').hexdigest()"
But of course following works
-bash-3.2$ python
>>> import hashlib
>>> hashlib.md5('test').hexdigest()
'098f6bcd4621d373cade4e832627b4f6'
>>>
you've got to print what you want to see if in non-interactive mode.
python -c "import hashlib
print hashlib.md5('test').hexdigest()"
the interactive mode always prints the return values, but this is just a gimmick of the CLI
python -c "import hashlib; print(hashlib.md5('test').hexdigest())"
>python -c "import hashlib; print hashlib.md5('test').hexdigest()"
098f6bcd4621d373cade4e832627b4f6
You are missing the print, which is why you don't see anything.