运行 对单个文件执行多个 shell 命令并将输出重定向到同一文件

Run multiple shell commands on a single file and redirect the output to the same file

这是一个相当笨拙的问题,因为我知道如何让它工作但现在无法弄清楚语法。

我有一个 shell 脚本,主要检查文件大小并将其重定向到临时文件

#!/bin/bash
while true
do
    echo "Press CTRL+C to stop the script execution"

echo "*********************" >> /tmp/size.txt
echo "current date is" >> /tmp/size.txt
date >> /tmp/size.txt
echo "size of file r4" >> /tmp/size.txt
du -h /tmp/r4
echo "size of file h5" >> /tmp/size.txt
du -h /var/h5
echo "size of file h6" >> /tmp/size.txt
du -h /opt/h6
echo "size of file h8" >> /tmp/size.txt
du -h /data/h8
echo "*********************" >> /tmp/size.txt
end

以上脚本完美运行并连续记录所有数据。但是,我需要多次编写重定向 (/tmp/size.txt),使脚本看起来很笨拙。 我能够更早地做到这一点,但由于语法看起来不正确,它无法正常工作。

我可以在这里得到一些帮助吗?这样我就不需要重复重定向,并且可以在一行中处理。

谢谢

>> /tmp/size.txt移到done之后并从里面移除:

while true
do
   echo "Press CTRL+C to stop the script execution"

   echo "*********************"
   echo "current date is"
   date >> /tmp/size.txt
   echo "size of file r4"
   du -h /tmp/r4
   echo "size of file h5"
   du -h /var/h5
   echo "size of file h6"
   du -h /opt/h6
   echo "size of file h8"
   du -h /data/h8
   echo "*********************"
done >> /tmp/size.txt