-1
cat <<EOF > /etc/rotatescript/truncate.sh
FILENAME=/mnt/logs/filename
MAXSIZE=10000000000
FILESIZE=$(stat -c%s "$FILENAME")
if (( FILESIZE > MAXSIZE )); then
        truncate -s 0 /mnt/logs/filename
else
        echo “nope”
fi

As you can see, I am creating a shell script file using terminal. The problem here is that

FILESIZE=$(stat -c%s "$FILENAME")

The above value is saved inside FILESIZE variable when I execute the above code. So the value of FILESIZE is set as a constant.

Whereas, I want that whenever I actually run this shell script using the below command, linux dynamically picks the value for FILESIZE each time.

/etc/rotatescript/truncate.sh

1 Answers1

0

You need to escape the subshell:

cat <<EOF > /etc/rotatescript/truncate.sh
FILENAME=/mnt/logs/filename
MAXSIZE=10000000000
FILESIZE=\$(stat -c%s "$FILENAME")
if (( FILESIZE > MAXSIZE )); then
    truncate -s 0 /mnt/logs/filename
else
    echo “nope”
fi

Note the \$, since the shell is actually performing substitutions inside the heredoc, you need to escape it to prevent that. You want a literal $.

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34