help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Redirection and subshells and file position pointer


From: Stan Marsh
Subject: Redirection and subshells and file position pointer
Date: Wed, 09 Jul 2025 07:17:50 -0600

See below.  Explanation: This is taken from another, larger script, which runs
unattended (in cron).  The goal is that everything that you would normally see 
on the
screen if run interactively, will get written to a log file instead (and the 
log file
gets appended to from one run to the next).  So, the "exec" line (in the real 
version)
would be: exec >> MyScript.log 2>&1

Anyway, in the line containing the "file" command, the intent is to have the 
output of
"file" show up in the log and also be used as the string to be compared to see 
if the
file is a text file.  So, I figured that piping it to "tee /dev/stderr" would 
do what
I want.  The problem is that doing this truncates the log file (In this test 
version
of the script, the thing to notice is that the "fortune" output is lost/gone).  
So, it
seems like the file position pointer for stderr is not the same as the one for 
stdout
(i.e., it points to the beginning of the file, that is, offset 0).

But note that the "This goes to stderr" line does do the right/expected thing.  
The
problem seems to have something to do with the fact that the "file" command is 
being
run in a subshell.

I originally discovered this using the usual $() form, but then got the idea 
that
maybe the new ${ ...; } form might work better.  Alas, it does not.  The same 
problem
occurs.

Anyway, the workaround is clear: Just break it down into three statements, like:

    f=$(file ...);echo "$f"; [[ $f =~ text ]] && ...

But I am curious if there anything else that can be done to make it work right 
as
originally written.  Or if there is something underlying this that I am missing.

--- Cut Here ---
 $ cat OinkOink
#!/path/to/bash5_3_0_rc2

set -e
echo "BASH_VERSION = $BASH_VERSION"
fortune > fizz.txt
ls -lsa fizz.txt
exec >> fizz.txt 2>&1
echo "This goes to stderr" >&2
pr -Fn fizz.txt > /dev/tty
[[ "${ file fizz.txt | tee /dev/stderr; }" =~ text ]] && echo "It is text!"
ls -lsa fizz.txt > /dev/tty
pr -Fn fizz.txt > /dev/tty

 $ ./OinkOink
BASH_VERSION = 5.3.0(1)-rc2
1 -rw-r--r-- 1 user user 790 Jul  8 08:06 fizz.txt


2025-07-08 08:06                     fizz.txt                     Page 1


    1
    2       "-._    `   \__                  \%\
    3           "-._   .(_()  '  '  '        /%)
    4            `  "-._  __          '     /%/
    5         `         "(_()__ '  ' '  '  /%/
    6             `   `     (_()----------(((--
    7          `    `    A     ,  '                     _
    8        `   `      /^\A ,       '  '             _/ /)
    9           `     A(' `\\  A ,')             ____// // )________,
   10       /"-._  ` /  ) / )\/ \ /             /|  /(@)/ // )     /|
   11       "-._ "-^/ /\ A    \A )^.-"\        / | (@)(@)// //)   / |
   12           "-._( () () /\ )/ __.-"       /  /(@)(@)(@)// /--/  j
   13             .'\,_.A \(  ) /"           /  /()(@)(@)()()/  /  /
   14           ,' ,'"   \ \"\ \             |><---------------Y  /
   15          (),'       \') `O)            |   Shimrod '96   | /
   16                      "                 |_________________|/
   17   This goes to stderr

1 -rw-r--r-- 1 user user 16 Jul  8 08:06 fizz.txt


2025-07-08 08:06                     fizz.txt                     Page 1


    1   fizz.txt: empty

 $
--- Cut Here ---

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

Note that they always end up in my Spam file anyway, so it is annoying to have 
to
periodically clean that out.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]