linux - Bash:将 redis-benchmark 结果存储到 var 生成奇怪的字符串

标签 linux bash shell command

我尝试在 shell 脚本中解析 redis-benchmark 结果,我编写了脚本但无法执行。

环境

$ bash --version
GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu)
$ cat /etc/issue
Ubuntu 12.04 LTS \n \l
$ dpkg -l |grep redis
2:2.8.19-rwky1~precise
$ cat demo.sh
OUTPUT=`redis-benchmark -n 1000 -r 100000 -d 32 -c 30 -t GET -p 6379 -q |grep 'per second'`
R=$(echo "$OUTPUT" | cut -f 1 -d'.')
S=$(echo $R | awk '{print $2}')
echo $S

Shell 调试显示一些混淆信息。

$ bash -x demo.sh
++ redis-benchmark -n 1000 -r 100000 -d 32 -c 30 -t GET -p 6379 -q
++ grep 'per second'
GET: 166666.67 requests per second'
GET: 166666.67 requests per second'
++ cut -f 1 -d.
GET: 166666'an
++ echo GET: $'-nan\rGET:' 166666
++ awk '{print $2}'
+ S=$'-nan\rGET:'
+ echo $'-nan\rGET:'
GET:

我错过了什么吗?

评论

由于 redis-benchmark 结果看起来有些奇怪,不知道为什么

$ redis-benchmark -n 1000 -r 100000 -d 32 -c 30 -t GET -p 6379 -q |grep per > todo
$ vim todo 
GET: -nan^MGET: 166666.67 requests per second

最佳答案

如果您无法修复 redis-benchmark 输出,这将解析正确和奇怪的格式:

redis-benchmark -n 1000 -r 100000 -d 32 -c 30 -t GET -p 6379 -q | grep 'per second' | sed 's/.*GET: \(.*\) requests .*/\1/'

但是你应该修正输入 :D

关于linux - Bash:将 redis-benchmark 结果存储到 var 生成奇怪的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36471790/

相关文章:

linux - 无法在 Docker 容器中使用 sudo 编辑环境文件

linux - 获取以太网设备的永久地址

bash - 如何在不关闭 xterm 的情况下捕获 SIGINT?

linux - 如何在 Unix 中创建附加到文件的 Shell 脚本?

shell - $$在shell中是什么意思?

linux - 主厨独奏失败,返回 'lpadmin: Unable to connect to server: Transport endpoint is not connected'

linux - 如何使用 Linux 命令或 Shell 脚本读取文件的特定行

linux - Bash 中意外标记 'elif' 附近的语法错误

linux - 获取命令的输出并将其插入到 bash 脚本中

linux - Bash 在单独的文件中复制多行