小编Jam*_*ite的帖子

如何检查 rsync 是否在 bash 中进行了任何更改?

我有一个脚本,它使用 rsync 在远程 -> 本地场景中同步数据。运行 rsync 命令后,立即检查错误代码是否为零。如果为零,则执行进一步的命令。然而,这并没有考虑到 rsync 可能已成功运行但实际上并未进行任何更改的事实。因此,无论如何都会运行等零条件,这有点多余。

rsync -aEivm --delete /path/to/remote/ /path/to/local/

if [ $? -eq 0 ]; then
    # Success do some more work!
else
    # Something went wrong!
    exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

什么是最好的方法来扩展它以检查是否真的有任何基于运行的 rsync 命令的更改。我读过 -i 标志可以向 stdout 提供输出,但是如何将其放置在条件块中?

rsync shell-script files

16
推荐指数
1
解决办法
1万
查看次数

使用 ash shell (BusyBox) 进行 RegExp 测试

我需要对某些用户输入进行 RegExp 模式测试。这是我需要测试该值的模式。

^([a-z]{2,3})\-([a-z][a-z]*[0-9]*)\-(\d+)$
Run Code Online (Sandbox Code Playgroud)

一个示例匹配是: na-examplename-01

我可用的 shell 是 BusyBox aka ash,所以我没有完整的 bash 功能。

使用 BusyBox 时,RegExp 模式测试有哪些选项?

注意:我不能使用 expr,因为它在我的安装中不可用。

我有以下功能可用:

arp, ash, awk, basename, bash, bunzip2, bzcat, bzip2, cat, chmod,
chown, chvt, clear, cp, crond, crontab, cryptpw, cut, date, dd,
deallocvt, df, dirname, dmesg, dnsdomainname, dos2unix, du, egrep,
eject, env, fbset, fgconsole, fgrep, find, findfs, flock, free, fstrim,
ftpget, ftpput, fuser, getopt, grep, groups, gunzip, gzip, head,
hostname, httpd, hwclock, id, ifconfig, ifdown, ifplugd, ifup, install,
ionice, iostat, …
Run Code Online (Sandbox Code Playgroud)

busybox regular-expression ash

7
推荐指数
2
解决办法
8817
查看次数

通过命令行运行时,将脚本中的 bash 函数作为参数的最佳方法是什么?

我对 bash 脚本还很陌生,想知道在通过命令行运行时将脚本中的 bash 脚本函数作为参数的最简单方法是什么?

用法示例:

./myscript function1
./myscript function2
Run Code Online (Sandbox Code Playgroud)

myscript 的示例内容:

echo "Example myscript"

function1() { 
    echo "I am function number 1"
}

function2() { 
    echo "I am function number 2"
}

if [ $# -eq 0 ]; then
    echo "Specify a function. E.g. function1"
    exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

该脚本只会在命令行上调用时执行特定的函数,否则会显示一些示例用法。它需要可供 cron 和此类进程使用以及由用户执行。

bash shell-script function

2
推荐指数
1
解决办法
477
查看次数

标签 统计

shell-script ×2

ash ×1

bash ×1

busybox ×1

files ×1

function ×1

regular-expression ×1

rsync ×1