0

I have the following bash script, run at system startup to view files dropped into the /srv/mutt directory. It works perfectly for a while but then simply stops 'seeing' files arriving in /srv/mutt.

I added the echo statements to try and diagnose what's wrong. This shows that the inotifywait sees the files and they're reported in /tmp/waitAndView.log but the viewing program doesn't get called.

What's going wrong? Does anyone have any ideas on the cause or on how to diagnose the problem? It seems''

#!/bin/bash
#
#
# script to detect new files in /srv/mutt and display them, run from 'Session & Startup'
#
cd /srv/mutt                    # where the files appear
shopt -s nocasematch            # ignore case in case (erk!)
#
#
# inotifywait will output the name of any file that is rsynced into /srv/mutt
#
inotifywait -m -q -e moved_to --format %f /srv/mutt/ | \
#
#
# Handle file as appropriate
#

while read -r file; do
    echo $file >>/tmp/waitAndView.log
    case $file in
        dbapost*)
            #
            #
            # run dbapost on the received message file
            #
            /home/chris/.cfg/bin/dbapost $file &
            ;;

        *.pdf)
            #
            #
            # View PDF file with atril
            #
            atril $file &
            ;;

        *.jpg|*.png|*.jpeg)
            #
            #
            # View other image formats with nomacs
            #
            nomacs $file &
            ;;

        *.html)
            #
            #
            # HTML file is an E-Mail to view with web browser
            #
            $HOME/bin/browser --new-tab file:///srv/mutt/$file &
            ;;

        *)
            ;;
    esac
    echo finished with $file >>/tmp/waitAndView.log
done
6
  • Hoe the ***&&&""" do you get this stupid system to accept code? I say 'code' and paste the script and it results in the above total rubbish! Jan 27, 2023 at 22:25
  • Paste it as normal, select the code, then hit the code button.
    – Bib
    Jan 27, 2023 at 22:28
  • Try giving the full path to atril andnomacs etc. On my system that would be /usr/bin/atril... Jan 28, 2023 at 2:45
  • The one I caught after adding the diagnostics was an html file so the 'viewer' has a full path. So, for example, I have in the log file:- mutt-esprimo-1000-793619-13564711348920342824.html finished with mutt-esprimo-1000-793619-13564711348920342824.html ''' Jan 28, 2023 at 9:37
  • Re the 'code' formatting, or not, it appears OK to me now. Has someone fixed it or was it just a display problem for me? Jan 28, 2023 at 9:40

1 Answer 1

0

One of the programs being run within the case statement is stealing the input. Consider this example:

seq 9 | 
while read f
do case $f in 
   4) cat -n & ;;
   *) echo got $f ;;
   esac
done

This can be the output:

got 1
got 2
got 3
     1  6
     2  7
     3  8
     4  9
got 5

Once the cat is started, it can read the pipe before the while read manages to. You need to close off standard input, typically by adding </dev/null to the "browser" command and so on.

1
  • Aha, thank you, I'll try dding a </dev/null to each 'viewer' Jan 28, 2023 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .