Shell 与脚本初探

下方课件区域方向键控制翻页,f 键全屏。

脚本实例

功能描述

下面将带着大家编写一个定时自动截屏并发送到指定邮箱的脚本。

关键函数

在 Powershell 脚本中定义一个函数,实现了屏幕截图的功能,如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function screenshot([Drawing.Rectangle]$bounds, $path) {
    $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
    $graphics = [Drawing.Graphics]::FromImage($bmp)

    $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
    $bmp.Save($path)

    $graphics.Dispose()
    $bmp.Dispose()
}

$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1920, 1080)
screenshot $bounds "C:\screenshot.png"

来自Jeremy@stackoverflow

然后实现一个发邮件功能,能够发送邮件到指定邮箱。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$username = "MyUserName";
$password = "MyPassword";
$path = "C:\screenshot.png";

function sendemail([String]$email, [String]$attachmentpath) {
    $message = New-Object Net.Mail.MailMessage
    $message.From = $email
    $message.To.Add($email)
    $message.Subject = "截图"
    $message.Body = "屏幕截图"
    $attachment = New-Object Net.Mail.Attachment($attachmentpath)
    $message.Attachments.Add($attachment)

    $smtp = New-Object Net.Mail.SmtpClient("smtp.qq.com", "587")
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($username, $password)
    $smtp.send($message)
    echo "邮件已发送"
    $attachment.Dispose()
 }
sendemail -email "demo@qq.com" -attachmentpath $path

参考IgrDi@stackoverflow

程序循环

Powershell 中也有循环语句,这里我们可以使用 while 来实现循环。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
while ($true) {
    $bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1920, 1080)

    screenshot $bounds "C:\screenshot.png"

    sendmail -email "demo@qq.com" -attachmentpath "C:\screenshot.png"

    # 等待一秒
    Start-Sleep -s 1
}

脚本的参数

在上面的函数示例中,发送邮件时我们直接将以及邮箱写在了脚本中,这样会导致信息泄露,所有我们改造一下脚本,通过参数将用户名、密码、邮箱地址传给脚本,这样脚本内就不会保存我们的个人信息了。在脚本最开始加入以下代码:

1
param ($UserName, $Password, $Mail)

这里我们定义了三个有名字的参数,执行脚本时,通过指定参数名加上参数值的形式来使用:

1
script.ps1 -UserName anjing -Password akldjfx8kq3jk24 -Mail admin@qq.com

作业

请尝试参考以上代码写出完整的脚本。


Git 与 Gitee

配置与使用

  1. 软件配置
  1. 编辑提交部分

软件链接:

附加资料

Git 官方视频中字 https://www.nowcoder.com/courses/2


Markdown Cheet Sheet

鉴于部分同学对 Markdown 不太熟悉,这里给大家推荐 Markdown Guide 。这个网站是一个 Markdown 学习站点,大家可以上去学习一下语法。下面一小节就是参考他们的 Markdown Cheet Sheet 翻译的。当然还有很多中文教程,大家可以自行补习。

基本语法

元素 语法
标题 # H1
## H2
### H3
粗体 **bold text**
斜体 _italicized text_
引用 > blockquote
有序列表 1. First item
2. Second item
3. Third item
无序列表 - First item
- Second item
- Third item
代码 code
横向分割线 ---
超链接 [title](https://www.example.com)
图片 ![alt text](image.jpg)

扩展语法

元素 语法
表格 | Syntax | Description |
| ----------- | ----------- |
| Header | Title |
|Paragraph | Text |
Fenced Code Block ```
{
" firstName": "John",
"lastName": "Smith",
"age": 25
}
```
脚注 Here's a sentence with a footnote. [^1]
[^1]: This is the footnote.
标题 id ### My Great Heading {#custom-id}
定义列表 term
: definition
划掉 ~~The world is flat.~~
任务列表 - [x] Write the press release
- [ ] Update the website
- [ ] Contact the media