8 篇文章带有标签 “scripting”

PowerShell 脚本示例

# 核心参数配置(无需修改,已按你的需求设定)
$targetMinDate = Get-Date "2024-06-01"  # 目标日期区间:开始
$targetMaxDate = Get-Date "2024-12-31"  # 目标日期区间:结束
$hourMin = 8                            # 限制最小小时(8点)
$hourMax = 21                           # 限制最大小时(21点,因22点不包含,实际最晚21:59:59)
$folderPath = "D:\test"                 # 要遍历的目录
$skipExtension = ".eml"                 # 需跳过的文件后缀
$logFilePath = "D:\log.txt"  # 日志文件路径(与脚本同目录)

# 生成目标区间内随机时间(限制8:00-22:00)的函数
function Get-RandomTargetDateTime {
    param(
        [datetime]$DateMin = $targetMinDate,
        [datetime]$DateMax = $targetMaxDate,
        [int]$HourMin = $hourMin,
// ...

该PowerShell脚本的核心功能是 批量筛选并修改指定目录下文件的修改时间,同时跳过特定类型文件、记录操作日

远程执行Shell命令

安装 Fabric

pip3 install fabric

远程执行 Shell 命令脚本(remote_execute_shell_command.py) #!/usr/bin/python import argparse from fabric import Connection, Config # 您要远程操作的计算机,username@ip HOSTS = ['root@192.168.0.1', 'root@192.168.0.2'] PASSWORDS = ['admin', 'admin'] if name == 'main': parser = argparse.ArgumentParser() parser.add_argument('-c', '--command', type=str, help='execute shell command.') args = parser.parse_args() if not args.command: args.command = 'uname -a' print('➜ Execute shell command: ', args.

Docker SDK for Python Examples

安装 Docker SDK for Python

pip install docker

例子

将本地文件或目录添加到容器,生成新的镜像。

  • put_archive
  • commit
import docker
import tarfile
import tempfile
import os

def simple_tar(path):
    f = tempfile.NamedTemporaryFile()
    t = tarfile.open(mode='w', fileobj=f)
    abs_path = os.path.abspath(path)
    t.add(abs_path, arcname=os.path.basename(path))
    t.close()
    f.seek(0)
    return f

client = docker.from_env()
// ...

参考资料

Linux Shell 实践

快捷键

  • Ctrl+r 快速查找历史命令
  • Ctrl+l 清理控制台屏幕

移动光标

  • Ctrl+a 移动光标到行首
  • Ctrl+e 移动光标到行尾
  • Alt+Left Arrow 移动光标到上一个单词
  • Alt+Right Arrow 移动光标到下一个单词

删除字符

  • Ctrl+u 删除光标之前的内容
  • Ctrl+k 删除光标之后的内容
  • Ctrl+w 删除光标前面的一个单词

进程

  • Ctrl+d 退出。等同于exit命令
  • Ctrl+z 当前运行的程序后台运行。如果一步到位,可以在命令后面加 &

重定向

  • 执行时的错误信息输出到文件(2>)
hello 2> log.error
$ cat log.error 
-bash: hello: 未找到命令
  • 执行时的所有信息都输出到文件(&>)
echo hello &> log.info
$ cat log.info 
hello
  • 创建一个文件并写入内容(> filename <<EOF)
cat > hello.sh << EOF
#!/bin/bash
echo hello
EOF

变量

变量赋值

  • 执行结果保存到变量($() ``)
var1=$(pwd)
var2=`pwd`
  • 整数四则运算(let)
let n=10-3+4/2
echo $n
9

变量引用 ${var} 大部分情况可省略为

Linux Shell 执行方式

在当前shell下一行执行多条命令(;)

cd /etc/ssh ; cat ssh_config ; pwd ; du -sh /etc/ssh/ssh_config

创建shell脚本

vim ssh_config-info.sh
#!/bin/bash
cd /etc/ssh
cat ssh_config
pwd
du -sh /etc/ssh/ssh_config

shell脚本执行方式

bash ssh_config-info.sh

  • 创建子进程执行脚本
bash ssh_config-info.sh
$ pwd
/root

./ssh_config-info.sh

  • 需要执行权限
chmod u+x ssh_config-info.sh
  • 使用脚本文件中第一行#!指定的shell创建子进程执行脚本
./ssh_config-info.sh
$ pwd
/root

source ssh_config-info.sh

  • 在当前shell进程中执行,会对当前shell产生影响
source ssh_config-info.sh
$ pwd
/etc/ssh

. ssh_config-info.sh

  • 在当前shell进程中执行,会对当前shell产生影响(.相当于source)
. ssh_config-info.sh
$ pwd
/etc/ssh

命令cp

拷贝一批文本文件(10000)到目录

cp

  • time: 0.470s
  • 当文件更新了或者缺少时才拷贝。(-u)
  • 速度最快
cp -u labels/*/*.txt datasets/yolo/sign/labels/

xargs

  • time: 30.003s
ls labels/*/*.txt | xargs -I {} cp {} datasets/yolo/sign/labels/

find -exec

  • time: 32.521s
find labels/ -type f -iname '*.txt' -exec cp {} datasets/yolo/sign/labels/ \;

for

  • time: 41.259s
for i in `ls labels/*/*.txt`; do cp $i datasets/yolo/sign/labels/; done

参考资料

VLC Extension Example

开发

编写脚本 hello.lua

function descriptor()
    return {
        title = "Hello World";
        version = "1.0";
        author = "WangJunjian";
        url = 'www.wangjunjian.com';
        shortdesc = "Hello World";
        description = "Hello World!";
        capabilities = {"menu"}
    }
end

-- activate() : called when the extension is activated from within VLC
function activate()
    create_dialog()
end

-- deactivate() : called when the extension is deactivated from within VLC
function deactivate()
    close()
    vlc.deactivate()
end

-- create_dialog() : creates the dialog containing the initial widgets
function create_dialog()
    dlg = vlc.dialog(descriptor().title)
    dlg:add_label("<b>Hello World: VLC Lua scripts and extensions</b>")
end