我找到了以下bash脚本以监控cp进度。
#!/bin/sh
cp_p()
{
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size=$(stat -c '%s' "${1}") count=0
}
我不理解strace命令的“-ewrite”选项。我发现最接近的是strace的手册页
-e write = set执行所有的完整十六进制和ASCII转储 写入文件描述符的数据 列在指定的集合中。对于 例如,查看所有输出活动 文件描述符3和5使用-e 写= 3,5。请注意,这是 独立于正常的追踪 write(2)系统调用是哪个 由选项-e控制 跟踪=写。
但是我不明白-ewrite选项的作用。
答案 0 :(得分:4)
-ewrite意味着只跟踪“写入”系统调用。
-e expr一个限定表达式,用于修改哪些事件 跟踪或如何跟踪它们。格式 表达式是:
[qualifier=][!]value1[,value2]... where qualifier is one of trace, abbrev, verbose, raw, signal, read, or write and value is a quali- fier-dependent symbol or number. The default qual- ifier is trace. Using an exclamation mark negates the set of values. For example, -eopen means lit- erally -e trace=open which in turn means trace only the open system call. By contrast, -etrace=!open means to trace every system call except open. In addition, the special values all and none have the obvious meanings. Note that some shells use the exclamation point for history expansion even inside quoted arguments. If so, you must escape the exclamation point with a backslash.