将 awk 输出传递给对话框清单
Pass awk output to dialog checklist
我想使用 dialog checklist
显示数据库及其大小的列表,但有些混乱。删除 size 和单词 "MB" 之间的 space 有帮助,但我不知道如何保留它并进行正常输出。
我的代码:
#!/bin/bash
db_list_command="
SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1)
FROM information_schema.tables
GROUP BY table_schema;
"
db_list=$(echo $db_list_command | mysql -N)
dialog_options=$(echo "$db_list" | awk '{print " \"" " MB\" off"}')
dialog --checklist "Select databases" 40 60 20 $dialog_options 2>/tmp/dout.txt
db_selected=$(</tmp/dout.txt)
echo $db_selected
以上代码的输出:
输出 w/o spaces 在大小和 MB 之间,但现在有不需要的额外引号:
我想获得大小格式如 5.8 MB
的普通清单
怎么做?
p.s。如果我将 awk 命令回显到终端,然后手动复制并粘贴到对话框 - 一切正常!
这是一种保留空格的方法:
#!/bin/bash
db_list_command="
SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1)
FROM information_schema.tables
GROUP BY table_schema;
"
declare -a dialog_options
while read -r db size; do
dialog_options+=("$db" "$size MB" off)
done < <(echo $db_list_command | mysql -N)
dialog --checklist "Select databases" 40 60 20 "$dialog_options[@]}" 2>/tmp/dout.txt
db_selected=$(</tmp/dout.txt)
echo $db_selected
我想使用 dialog checklist
显示数据库及其大小的列表,但有些混乱。删除 size 和单词 "MB" 之间的 space 有帮助,但我不知道如何保留它并进行正常输出。
我的代码:
#!/bin/bash
db_list_command="
SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1)
FROM information_schema.tables
GROUP BY table_schema;
"
db_list=$(echo $db_list_command | mysql -N)
dialog_options=$(echo "$db_list" | awk '{print " \"" " MB\" off"}')
dialog --checklist "Select databases" 40 60 20 $dialog_options 2>/tmp/dout.txt
db_selected=$(</tmp/dout.txt)
echo $db_selected
以上代码的输出:
输出 w/o spaces 在大小和 MB 之间,但现在有不需要的额外引号:
我想获得大小格式如 5.8 MB
的普通清单
怎么做?
p.s。如果我将 awk 命令回显到终端,然后手动复制并粘贴到对话框 - 一切正常!
这是一种保留空格的方法:
#!/bin/bash
db_list_command="
SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1)
FROM information_schema.tables
GROUP BY table_schema;
"
declare -a dialog_options
while read -r db size; do
dialog_options+=("$db" "$size MB" off)
done < <(echo $db_list_command | mysql -N)
dialog --checklist "Select databases" 40 60 20 "$dialog_options[@]}" 2>/tmp/dout.txt
db_selected=$(</tmp/dout.txt)
echo $db_selected