0

I'm attempting to write a shell script for BusyBox (v1.27.1) wherein the script, in part, needs to extract the last item from a list/multiline variable, which contains a list of absolute file paths. I've been working on this most of the day, and still no joy. What am I doing wrong?

TARGET_DIRECTORY="/usr/bin"                                          # can be any valid location
ALL_LOG_FILES=$(find $TARGET_DIRECTORY -maxdepth 1 -regex '.*\.log')
LAST_LINE=$($(`echo $ALL_LOG_FILES`) | tail -1)                      # broken here
echo "last line=$LAST_LINE"
4
  • 1
    Likely the issue is that the unquoted expansion $ALL_LOG_FILES is subject to word splitting, so there is no "last line" - see related Echo newline with quotes Nov 3, 2020 at 22:05
  • @steeldriver Thanks, but how does this apply to my situation? The file paths in my variable have no spaces, only line ends (presumably).
    – Jim Fell
    Nov 3, 2020 at 22:11
  • It should become clear if you compare the output of echo $ALL_LOG_FILES vs echo "$ALL_LOG_FILES". BTW you seem to have more layers of command substitution than you need there - LAST_LINE=$(echo "$ALL_LOG_FILES" | tail -1) should be sufficient. Nov 3, 2020 at 22:18
  • @steeldriver That appears to have been the problem. If you post your answer, I'll accept.
    – Jim Fell
    Nov 3, 2020 at 22:32

0

You must log in to answer this question.

Browse other questions tagged .