Execute shell command in Python
The previous execution of the shell command is mostly done by Os.system (Shell command), which is more cumbersome.
Understand that SH is a better library than subprocess, capable of executing shell commands
1. View IP:
[Email protected] myblog]# ifconfig
Eth0:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255
Ether 00:16:3e:01:72:ac Txqueuelen (Ethernet)
RX packets 235322 Bytes 231762359 (221.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 78402 Bytes 15382388 (14.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Complete with SH:
in [+]: from SH import ifconfig
in [+]: Print ifconfig ("eth0")
Eth0:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255
Ether 00:16:3e:01:72:ac Txqueuelen (Ethernet)
RX packets 236463 Bytes 231849592 (221.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 79234 Bytes 15505613 (14.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2. View the files in the/app directory:
In []: Print Sh.ls ("/app")
git-2.14.1 MyBlog pyinit.py v2.14.1.tar.gz
in [+]: Sh.ls ("-L", "/app")
OUT[27]:
Drwxrwxr-x root root 24576 9?. 20:52 git-2.14.1
Drwxr-xr-x 6 root root 4096 9?. 21:53 MyBlog
-rw-r--r--1 root root 273 9?. 20:43 pyinit.py
-rw-r--r--1 root root 6983655 9?. 20:48 v2.14.1.tar.gz
See how many files are under/app:
in [+]: SH.WC (Sh.ls ("-L", "/app"), "-L")
OUT[28]: 5
3. Save the file under/app to the list:
In [All]: List=sh.glob ('/app/* ')
in [+]: List
OUT[43]: ['/app/git-2.14.1 ', '/app/v2.14.1.tar.gz ', '/app/pyinit.py ', '/app/myblog ']
4. Piping:
In [Sh.sort]: Print (Sh.du (Sh.glob ('/app/* '), '-SHC '), '-rn ')
397M?.?
378m/app/git-2.14.1
13m/app/myblog
6.7m/app/v2.14.1.tar.gz
4.0k/app/pyinit.py
Using the SH library to execute shell commands