我正在尝试执行以下操作:
printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB) " "$0"
它工作正常,但是,我想通过执行以下操作来显示文件的实际大小:
printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$0" "ls -l $0 | awk {'print $5'}"
然而,我这样做却失败了。我想这不是正确的做法。
答案1
printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB: %lu) " "$0" \
"$(wc -c < "$0")"
解析 的输出ls
是不可靠的(并且您已经忘记了$(...)
它周围的内容)。
答案2
我认为这是某种形式的 shell,如 bash 或 ksh。 X 是文件(我还认为它的名称中有空格)
printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$x" \
$(ls -ld -- "$x" | awk {'print $5'})
应该做。
请注意语法。$( some code )
答案3
如果在 GNU 系统上,stat
更喜欢使用:
printf "Are you sure you want to copy %s (y/n) ? (file bigger than 10 MB : %s) " "$0" \
"$(exec stat -Lc '%s' -- "$0")"
-L
使stat
跟随符号链接。