Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -c switch

I currently have

".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'\1.', "00112233")).split('.')])
'xx.xx.xx.xx'

which works but when i try to use it via the python -c switch it fails ?

[root@monty ~]# python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?!$)', r'\1.', "00112233")).split('.')])"
python -c "import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?"import re ; ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r'(.{2})(?python)', r'\1.', "00112233")).split('.')])")', r'\1.', "00112233")).split('.')])"
-bash: syntax error near unexpected token `str'

Any ideas ?

like image 457
felix001 Avatar asked Apr 05 '13 13:04

felix001


People also ask

Is there a switch in Python?

Well, the answer to this question is NO. Unlike any other programming language, python language does not have switch statement functionality.

What does switch mean in Python?

Switch-case statement is a powerful programming feature that allows you control the flow of your program based on the value of a variable or an expression. You can use it to execute different blocks of code, depending on the variable value during runtime.

Does Python 3.10 have switch statements?

From version 3.10 upwards, Python has implemented a switch case feature called “structural pattern matching”. You can implement this feature with the match and case keywords.

What is the alternative for switch in Python?

getattr() in Python is used to invoke a function call. The lambda keyword in Python is used to define an anonymous function. In case the user gives an invalid input, lambda will invoke the default function.


1 Answers

Looks like a quoting issue on the command line.

Try wrapping the Python string in single quotes instead, and not using single quotes inside it.

You can also escape the quotes that collide with the shell's interpretation, using \".

$ python -c 'import re;print ".".join(str(z) for z in [int(x, 16) for x in (re.sub(r"(.{2})(?!$)", r"\1.", "00112233")).split(".")])'
0.17.34.51

Note: as your not running the python interpretor any more you need to explicitly print the results.

like image 83
unwind Avatar answered Sep 18 '22 18:09

unwind