无法将整行回显到文件

fix*_*it7 1 command-line bash

我不知道如何将以下行回显到文件中。它只有一部分进入文件。

echo perl -e "printf(\"%.1lf%%\n\", ($reserved_block_count * 100.0 ) / $block_count);">>Show_Percent_Reserved_Blocks.sh
Run Code Online (Sandbox Code Playgroud)

当我运行下面的脚本时,我得到

andyk_~/Downloads$ Show_Percent_Reserved_Blocks.sh
/home/andy/bin/Show_Percent_Reserved_Blocks.sh: line 2: Block: command not found
/home/andy/bin/Show_Percent_Reserved_Blocks.sh: line 3: Reserved: command not found
syntax error at -e line 1, near "/ )"
Execution of -e aborted due to compilation errors.

#!/bin/bash
Block count:              421958912
Reserved block count:     4219589
perl -e "printf(\"%.1lf%%\n\", ($reserved_block_count * 100.0 ) / $block_count);"
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 5

问题是,当您使用双引号时,变量引用 ( $reserved_block_count, $block_count) 在当前(调用)shell 环境中被扩展;你需要用单引号引用整个事情:

echo 'perl -e "printf(\"%.1lf%%\n\", ($reserved_block_count * 100.0 ) / $block_count);"' >>Show_Percent_Reserved_Blocks.sh
Run Code Online (Sandbox Code Playgroud)