Long programs using python -c switch

后端 未结 4 1287
执笔经年
执笔经年 2021-01-06 09:46

I would like to use python for things I\'ve been doing using bash. Is it possible to use the -c switch for long programs, e.g. a for loop with two statements? This would let

4条回答
  •  别那么骄傲
    2021-01-06 09:53

    If you are running from a bash script, just use quotes:

    #!/bin/sh
    
    python -c 'import os
    for i in range(3):
        for j in range(3):
            print i*j
    '
    
    echo "done"
    

    Otherwise, if using the cmd line, use ; semicolons to seperate statements, or use single quotes again to wrap around to the next line:

    python -c 'import os
    >    for i in range(3):
    >        for j in range(3):
    >            print i*j
    >    '
    

提交回复
热议问题