python -c command多行问题
python允许直接使用命令行执行命令,例如:
- snippet.bash
[yz@localhost ~]$ python -c "print('xyz')" xyz
也可以多行,使用分号分割:
- snippet.bash
[yz@localhost ~]$ python -c "xyz='xyz'; print(xyz)" xyz
但是,如果需要(多行)缩减,写法有所不同。下面的写法会报错:
- snippet.bash
[yz@localhost ~]$ python -c "xyz='xyz'; print(xyz); for i in range(3): print(i)" File "<string>", line 1 xyz='xyz'; print(xyz); for i in range(3): print(i) ^ SyntaxError: invalid syntax
这里,for循环因为需要使用缩减,单行无法表达缩进,因此,需要改换写法:
- snippet.bash
[yz@localhost ~]$ python -c "xyz='xyz' > print(xyz) > for i in range(3): > print(i) > "
运行结果:
xyz 0 1 2
打赏作者以资鼓励:
![]() | ![]() |