Skip to content

Latest commit

 

History

History
279 lines (200 loc) · 7.02 KB

157-328398-执行shell命令.sy.md

File metadata and controls

279 lines (200 loc) · 7.02 KB
show version enable_checker
step
1.0
true

循环起来

回忆

  • 上次深入了循环
  • 明确了循环的核心
    • 循环条件
    • 只要满足循环条件就会不断循环
    • 甚至死循环
  • 循环中可以执行外部命令吗?🤔

输出时间

#!/usr/bin/python3
import time
localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
print(localtime,end="")

图片描述

  • 这个是很久以前的深度整合那一节的代码
  • 当时是用shell循环来把输出结果粘合的
  • 现在运行也没有问题

图片描述

  • 套上循环

循环

图片描述

  • 套上循环之后
  • 确实能没完没了地运行
  • 但是效果并不好

图片描述

  • ctrl+c可以结束shell退回到vim
  • 需要加上延迟
  • 还有重头开始的 \r

修改代码

#!/usr/bin/python3
import time
while True:
    localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
    print("\r"+localtime,end="")
    time.sleep(1)

图片描述

  • 这次可以按秒来刷新时间了

图片描述

  • 不过当时调用了figlet、cowsay、lolcat那些shell的命令
  • 我可以在python里面调用shell的命令么?
  • 搜索一下

python执行shell

图片描述

图片描述

  • 看起来是可以的
  • 我们先来试试

尝试

图片描述

图片描述

  • 执行成功!!!🙌
    • 前面的是ls -la的结果
    • 后面的0 是 print(val)的结果
  • 我们来试试cowsay之类的东西

整合shell

图片描述

  • 这个 os 以前见过
    • os 的意思是 Operating System
  • 这个和命令行参数的 sys 不一样
    • os 负责程序与操作系统的交互,提供了访问操作系统底层的接口
      • This module provides a portable way of using operating system dependent functionality.
      • mac
      • win
      • linux
    • sys 模块负责程序与python解释器的交互
      • This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
      • 提供了一系列的函数和变量
      • 用于操控python的运行时环境
      • 比如命令行参数

命令行参数

#!/usr/bin/python3
import sys
print("Current python File:",sys.argv[0])
print("Current File:",sys.argv[1])
s_file = sys.argv[1]
f = open(s_file)
s_name = f.readline().replace("\n","")
print("Hello ",s_name,"!Welcome to file io")
f.close()
  • 还需要准备一个o.txt
    • 里面写上oeasy

图片描述

  • 尝试运行文件

运行结果

  • 确实可以使用参数了
    • 这样就可以在shell中
      • 动态设置t.py 读取的目标了

图片描述

  • 可以执行其他shell命令吗?

执行前提

  • 执行的前提是这些命令都安装了
  • 如果没有安装
    • sudo apt install cowsay 进行安装

图片描述

  • 然后试着整合其他的程序
    • sudo apt install lolcat 安装变色应用

继续整合其他命令

图片描述

图片描述

  • 看起来可以运行
  • 下面需要和python整合

整合python

图片描述

#!/usr/bin/python3
import time
import os
while True:
    localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
    val = os.system("echo " + localtime + "|cowsay -f moose|lolcat")
    time.sleep(1)

图片描述

  • 这个运行是没有问题的
  • 但是需要清屏

清屏

图片描述

#!/usr/bin/python3
import time
import os
while True:
    localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
    os.system("clear")
    os.system("echo " + localtime + "|cowsay -f moose|lolcat")
    time.sleep(1)
  • 清屏是没有问题的
  • 如果加上figlet呢?

figlet

#!/usr/bin/python3
import time
import os
while True:
    localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
    os.system("clear")
    os.system("figlet " + localtime + "|cowsay -f moose|lolcat")
    time.sleep(1)

图片描述

图片描述

  • figlet却出现了问题
  • 怎么办?

分析

  • 日期送figlet是没问题的
  • 但是figlet再送cowsay是有问题的

图片描述

  • 有什么解决办法么?
  • 原来是如何解决的?

解决办法

  • 问题出现在cowsay上面
  • 试着寻找cowsay的参数
  • 找到-n这个参数
  • 可以解决问题

图片描述

完整代码

#!/usr/bin/python3
import time
import os

while True:
    localtime = time.strftime(' %H:%M:%S',time.localtime(time.time()))
    os.system("clear")
    os.system("echo" + localtime + "|figlet|cowsay -f turtle -n|lolcat")
    time.sleep(1)

图片描述

图片描述

  • 这样我们就用python这个脚本语言
  • 把shell执行的结果粘合到了一起
  • 使用python做粘合剂

总结

  • 这次继续使用循环
    • 尝试持续输出时间
  • 然后想起以前的命令
    • cowsay
    • lolcat
    • figlet
  • 这次使用python做主循环
    • 并且把命令执行的结果整合了起来
  • 我们不但可以整合shell下命令输出的结果
    • 还可以整合其他语言比如c、java程序运行后输出的结果
    • 这也就是python被叫做胶水语言的原因
    • 可以把各种语言运行的结果粘合起来
    • 然后用循环不断地执行
  • 但是循环不应该总是死循环
    • 是否应该有个跳出循环的条件呢?
    • 这个帮助手册是怎么说循环的呢?🤔
  • 下次再说 👋