戏耍脚本中~奇技淫巧
参数判空
字符串拼接判定
有时候我们想判断一个变量是不是空的,会变得有些棘手
1
2
3if [ ${abc} != "" ]; then
echo ${abc}
fi我们的想法是判断是否存在 abc 这个变量,实际上这样写并不行,没有这个变量的话会变成下面的样子:
1
2
3if [ != "" ]; then
echo ${abc}
fi这样肯定会报错的
改一下,比如添加冗余校验字符
1
2
3
4
5abc = "abc";
if [ a${abc} != a ]; then
echo ${abc}
fibat 中引号也会视为变量一部分
,所以:1
if "%DIRNAME%" == "" set DIRNAME=.
或者:
1
2
3
4
5set abc=""
if %abc%=="" then (
echo "abc"
)
语法判定
shell 的语法:
1
2
3if [ -n "${abc}" ]; then
echo ${abc}
fibat 语法:
1
2
3if not defined abc (
echo 'abc' not defined.
)
程序占用
如果你用过 aria2 的话肯定会遇到一个头疼的事:
如何开机后台自动启动它
它的程序本身不带有开机启动,需要另外写启动脚本
它无法后台启动,必然会占据一个控制台程序的前台
网上找到了比较好用的方法:
vbs 脚本
1
CreateObject("WScript.Shell").Run "aria2c --conf-path=D:\Game\Scoop\persist\aria2\conf",0
由于我平时用 bat 管理电脑,单独编写执行一个 vbs 脚本略显多余,所以直接结合进 bat 脚本里,用完就删针刺激:
1
2
3echo CreateObject("WScript.Shell").Run "aria2c --conf-path=D:\Game\Scoop\persist\aria2\conf",0 > aria2.vbs
cscript //Nologo aria2.vbs
del aria2.vbs
echo
看到一个脚本中这么写,乍一看蒙了:
1
2CLS&ECHO.&ECHO 完成, 以下选项自选:
ECHO.&ECHO 1、启用视频播放程序(会驻留进程)实际上只是把几个命令压缩到了一行
1
2cls
echo.
Scoop
portable
我们可以通过创建虚拟链接把 C 盘中的 runtime 数据 -> scoop/persist 内
此片段可自动判断
remix-ide / .remix-ipfsnode / .cache_remix_ide
所在目录是USERPROFILE / APPDATA
并将数据移动到 persist 内,创建虚拟链接
需要注意只能是文件夹,不能是文件!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19"foreach ($folder in @('remix-ide', '.remix-ipfsnode', '.cache_remix_ide')) {",
" if (Test-Path \"$env:USERPROFILE\\$folder\") {",
" $runtimeCache = \"$env:USERPROFILE\\$folder\"",
" $runtimeCachePersist = \"$persist_dir\\USERPROFILE\\$folder\"",
" } else {",
" if (Test-Path \"$env:APPDATA\\$folder\") {",
" $runtimeCache = \"$env:APPDATA\\$folder\"",
" $runtimeCachePersist = \"$persist_dir\\APPDATA\\$folder\"",
" } else {continue}",
" }",
" if (Test-Path $runtimeCachePersist) {",
" Remove-Item $runtimeCache -Force -Recurse -ErrorAction SilentlyContinue",
" New-Item -Type Junction -Path $runtimeCache -Target $runtimeCachePersist | Out-Null",
" } else {",
" mkdir $runtimeCache",
" Move-Item $runtimeCache $runtimeCachePersist -Force",
" New-Item -Type Junction -Path $runtimeCache -Target $runtimeCachePersist | Out-Null",
" }",
"}"以及配对的 uninstaller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17"uninstaller": {
"script": [
"foreach ($folder in @('remix-ide', '.remix-ipfsnode', '.cache_remix_ide')) {",
" if (Test-Path \"$env:USERPROFILE\\$folder\") {",
" $runtimeCache = \"$env:USERPROFILE\\$folder\"",
" $runtimeCachePersist = \"$persist_dir\\USERPROFILE\\$folder\"",
" } else {",
" if (Test-Path \"$env:APPDATA\\$folder\") {",
" $runtimeCache = \"$env:APPDATA\\$folder\"",
" $runtimeCachePersist = \"$persist_dir\\APPDATA\\$folder\"",
" } else {continue}",
" }",
" if (!(Test-Path $runtimeCachePersist)) { Move-Item $runtimeCache $runtimeCachePersist -Force -ErrorAction SilentlyContinue }",
" else { Remove-Item $runtimeCache -Force -Recurse -ErrorAction SilentlyContinue }",
"}"
]
},
解剖-exe
此片段可以把常见的
.exe -> .exe#/dl.7z
中的文件提取出来1
2"Expand-7zipArchive \"$dir\\`$PLUGINSDIR\\app-64.7z\" \"$dir\"",
"Remove-Item -Recurse -Force \"$dir\\`$PLUGINSDIR\"",
判断是否含有某字符串
如下,可以判断.gitignore 中是否含有 ‘backup’
1
2
3cat %BACKUP_DIR%\.gitignore | findstr backup >nul || (
echo backup>> %BACKUP_DIR%\.gitignore
)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ⭐️齐下无贰⭐️!
评论