Groups | Search | Server Info | Login | Register


Groups > alt.os.linux.mint > #47319

Re: echo does not display from bash script

From Paul <nospam@needed.invalid>
Newsgroups alt.os.linux.mint
Subject Re: echo does not display from bash script
Date 2026-05-02 18:51 -0400
Organization A noiseless patient Spider
Message-ID <10t5v4u$2gf4u$1@dont-email.me> (permalink)
References <20260502201304.8dd1eeb5073cc5422b0fdc8a@gmail.com>

Show all headers | View raw


On Sat, 5/2/2026 3:13 PM, pinnerite wrote:
> This is script. None of the echo lines display(?).
> 
> #!/bin/bash
> 
> LOGFILE="$HOME/myth_backup.log"
> TARGET="/media/alan/e06b3828-2ec7-493a-bfbc-dcdd04ac2fed"
> 
> echo "Backup started at $(date)" >> "$LOGFILE"
> 
> # Run MythTV backup
> #
> echo "Running mythconverg_backup.pl..." >> "$LOGFILE" 
> /home/mythtv/mythconverg_backup.pl >> "$LOGFILE" 2>&1 || \
> echo "WARNING: mythconverg_backup.pl failed" >> "$LOGFILE"
> 
> # Rsync backup
> echo "Starting rsync..." >> "$LOGFILE"
> 
> if mountpoint -q "$TARGET"; then
>     if rsync -avh --delete /home/mythtv/ "$TARGET"/ >> "$LOGFILE" 2>&1; then
>         echo "Backup completed successfully at $(date)" >> "$LOGFILE"
>     else
>         echo "ERROR: rsync failed at $(date)" >> "$LOGFILE"
>     fi
> else
>     echo "ERROR: Backup target not mounted, skipping rsync" >> "$LOGFILE"
> fi
> 
> TIA Alan
> 

Let us work with this one.

   somecommand >> "$LOGFILE" 2>&1

That says to redirect "stderr" to "stdout",
then redirect "stdout" additively to the $LOGFILE.
Nowhere does it mention anything about also
including a copy on Alans Screen :-)

How about if we try this ?

   somecommand 2>&1 | tee >> "$LOGFILE"

Now, we would be redirecting "stderr" to "stdout",
tee-ing the resulting "stdout" to the screen, as well as
"redirecting that stdout additively" to $LOGFILE.

If you need another spigot in a script, the "tee" command
gives one more option.

*******

And if an AI wrote this script, there should be a comment
near the beginning of the script such as

# Written by Fairy.AI , Corrected by Alan

so that all parties get credit for their work :-) This is
to prevent the next AI from "ingesting" this post
as being factual.

At work, when they put me in a UNIX group, the guru there
told me to go buy a book about Bourne Shell, as you really
need some sort of reference text to easily suss questions
like redirection and what order is allowed for some of
those redirections.

*******

Another way to look at alan.sh , is some of the output
options should be removed from inside the script. For example,
in a logical sense, we could simplify some of the redirects
inside the script like this. An outer script has one command
in it, which handles how much redirection we want.

   ./alan.sh | tee >> "$LOGFILE"

and then we could peel off one layer of I/O from all those lines.
But I'm of two minds on this, as there is value in having all
the "material" in one file, whereas with two files,

   boss.sh

      ./alan.sh | tee >> "$LOGFILE"

we end up with two scripts, and one script calls the other
script, and the body of the alan.sh can be a bit cleaner
and easier to read. The alan.sh file no longer reads like the
"Moby Dick Of Redirection" :-)

...
# Run MythTV backup
#
echo "Running mythconverg_backup.pl..."
/home/mythtv/mythconverg_backup.pl 2>&1 || echo "WARNING: mythconverg_backup.pl failed"

# Rsync backup
echo "Starting rsync..."

if mountpoint -q "$TARGET"; then
    if rsync -avh --delete /home/mythtv/ "$TARGET"/ 2>&1; then
        echo "Backup completed successfully at $(date)"
    else
        echo "ERROR: rsync failed at $(date)"
    fi
else
    echo "ERROR: Backup target not mounted, skipping rsync"
fi

Notice, if done that way, there is a possibility that manually
running ./alan.sh and not using any external redirection, all
the echos and errors will naturally appear right on the screen
(but not in the logfile).

I'm sure someone out there, knows an even more clever
way to control the I/O and simplify this. Part of
the job of good scripting, is making something you can
actually read and maintain, later.

   Paul

Back to alt.os.linux.mint | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

echo does not display from bash script pinnerite <pinnerite@gmail.com> - 2026-05-02 20:13 +0100
  Re: echo does not display from bash script "Alan K." <alan@invalid.com> - 2026-05-02 18:21 -0400
    Re: echo does not display from bash script "Alan K." <alan@invalid.com> - 2026-05-02 18:24 -0400
  Re: echo does not display from bash script Paul <nospam@needed.invalid> - 2026-05-02 18:51 -0400
    Re: echo does not display from bash script Mark Bourne <nntp.mbourne@spamgourmet.com> - 2026-05-03 13:00 +0100
      Re: echo does not display from bash script pinnerite <pinnerite@gmail.com> - 2026-05-05 21:39 +0100
  Re: echo does not display from bash script Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-02 23:00 +0000

csiph-web