embedding python in tcsh - python

So as an example, I know that embedding python in bash as follows work:
python -c "import os
dict_name[\"var1\"]=1
dict_name[\"var2\"]=2
"
However, when I do the same exact thing in tcsh, I get the following error:
ERROR = Unmatched ".
Would anybody happen to have any insight on this? Would I have to python - c "" every line?
Thank you so much for your time.

Try this
python -c 'import os\
dict_name = {}\
dict_name["var1"]=1\
dict_name["var2"]=2\
'

Related

linux command pipe with python "-c" flag

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

How to use python -c "code here" with newlines?

The command
python -c "print('hello')"
runs the code inside the quotes successfully, both in Linux (bash) and Windows (cmd.exe).
But how to pass code with newlines, with python -c?
Example: both
python -c "for i in range(10): if i % 2 == 0: print('hello')"
python -c "for i in range(10):\n if i % 2 == 0:\n print('hello')"
fail.
Example use case: I need to send a SSH command (with paramiko) to execute a short Python code on a remote server, so I need to pass one command like
ssh.exec_command('python -c "..."').
You can use bash's $'foo' string syntax to get newlines:
python -c $'for i in range(10):\n if i % 2 == 0:\n print("hello")'
(I'm using single space indents here)
For windows, you really should be using powershell, which has `n as a newline:
python -c "for i in range(10):`n if i % 2 == 0:`n print('hello')"
In cmd.exe, it seems that you can use ^ to escape a newline, however I'm unable to test this currently so you should refer to this question's answers.
You can use a bash "heredoc" (also available ksh, and POSIX shells):
python <<EOF
import numpy as np
print(dir(np))
EOF
While not using -c it is worth mentioning that, if using Bash, you can pipe echoed code directly to python.
echo -e 'for i in range(10):\n if i % 2 == 0:\n print("hello")' | python
Following Aplet123's lead and using using single space indents.
One way could be with exec(), what make strings executable. It looks bad, but it works.
python -c "exec(\"for i in range(10):\n if i % 2 == 0:\n print('hello')\")"

Execute shell command and use it's output in Jupyter

I know this is a recurrent question, but I can't find a useful answer.
In Python, for running a shell command one can use this.
If I do the same inside Jupyter I got no output. How can I see the results
of executing the command? Doing
print subprocess.call(["ping", "-c 2", "www.cyberciti.biz"])
returns zero.
Use the ! shell magic:
!ping -c 2 www.cyberciti.biz
If you want to assign it to a variable:
output = !ping -c 2 www.cyberciti.biz
print(output)

Python --command command line option

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.

Python file testing line in BASH program failing with syntax error

Hey guys I'm trying to integrate some PY into my shell script and running across
the following error, I though quote should have quoted my variable but it looks
like it's not doing what I expected, can someone help me troubleshoot this?
#!/bin/bash
host='user#localhost'
path='/home/user/file'
python -c "return subprocess.call(['ssh', '$host', 'test -e ' + pipes.quote($path)]) == 0"
File "<string>", line 1
return subprocess.call(['ssh', "user#localhost", 'test -e ' + pipes.quote(/home/jdaniel/sent)]) == 0
^
SyntaxError: invalid syntax
python -c "return subprocess.call(['ssh', '$host', 'test -e ' + pipes.quote(\"$path\")]) == 0"
I would assume
as an aside .. why are you not just calling ssh from the bash? what benefit are you getting by using python here in this fashion? and do you not need to use import subprocess when you use the -c flag?
I would choose to do this whole program either in python or bash ... but mixing them like this feels slightly silly (especially given what your python code does)
You'll need to change this
pipes.quote($path)
to
pipes.quote('$path')
as pipes.quote() is expecting a string
I'd say Its better to use shell instead of python
#!/bin/bash
host='user#localhost'
path='/home/user/file'
ssh -q $host "test -e $path"

Categories