0

I am using the following script to watch a folder, and if the size changes and a file doesn't exist, it runs a script.

#!/usr/bin/sh
SCRIPT="dim_import/xdir"

while true;
do
    size=$(du -s dim_import | cut -b 1-2)
    if [ $size -gt 32 ]; then
        if [ ! -f files ]; then
          $SCRIPT
        fi
    fi
    sleep 2
done

I run the script like so

>watcher &

The problem is that the script that gets run within the watcher program requires input. It echos out the prompt, but then the input is not sent to that script, but the the current shell. Thus if I input 1234 I get this:

sh: 1234:  not found.

How can I make sure to send the input the correct place?

I have tried many different methods and none seem to work because of the unique way I am doing this. I am also open to better methods/work around. The script also has the ability to handle the input I want to send as an argument.

EDIT: I am trying to send a number like 62918

EDIT: I could also do something like this, but I get the same error:

    SCRIPT="dim_import/xdir"

    while true;
    do
        size=$(du -s dim_import | cut -b 1-2)
        if [ $size -gt 32 ]; then
            if [ ! -f files ]; then
              echo "SR Number: "; read srNum
              $SCRIPT $srNum
            fi
        fi
        sleep 2
    done

Note: I am not using Bash. I am using Sh.

0

2 Answers 2

3

Well, the real question is what input you actually want to send to your SCRIPT. If you want it to have empty input, then run:

$SCRIPT < /dev/null

If you want to give it some particular input, like the contents of $size, then run:

echo "$size" | $SCRIPT

If you want to type the input, then you will have to put it back in the forground to type your input, then put it into the background again. The ability to do that depends on whether your shell has job control (most do, but the original Bourne shell did not). Do do that:

  • Run fg %watcher or fg %1 (using whatever job number you see after running jobs -l) to put the process into the foreground.
  • Type the input you want followed by return.
  • Type control-Z to put it back into the background (or use stty -a and check for susp to see what your stop character is).
  • After the program is stopped by control-Z, put it back in the background with bg %1 (or whatever the number was).

Yet another answer would be to have a named pipe. Here is an example (using tr in place of your script just to show):

$ mkfifo my_fifo
$ tr a-z A-Z < my_fifo &
$ exec 6> my_fifo
$ echo here is some input for the fifo >&6
$ HERE IS SOME INPUT FOR THE FIFO

First you run the script taking it's input from the fifo (important to run the script first). Then exec 6> ... makes file descriptor 6 of the shell write to the fifo. Then, whenever you want to send output, write it to file descriptor 6 (via >&6). Notice the final "HERE IS..." was not typed by me, it just was the output of tr.

10
  • jobs -l out puts nothing.
    – James
    Jul 17, 2015 at 16:13
  • It outputs nothing even when you have jobs in the background? Like if you run sleep 60 & jobs -l, you see nothing? Jul 17, 2015 at 16:45
  • It doesn't have the script there, which is what I would need to output to
    – James
    Jul 17, 2015 at 17:46
  • jobs -l only works if you are in the same shell. That is why it was showing nothing.
    – James
    Jul 17, 2015 at 18:03
  • 1
    If you want to be able to run a program and later reconnect to it in a different shell, use screen or tmux to run it in a virtual terminal.
    – Barmar
    Jul 17, 2015 at 18:26
1

Through a lot of research, I have determined it is not possible with the restrictions. I found a less than desirable work around however.

Basically I created a named pipe (called ".in" ) and send it information. I created an function so that all I have to do its type "i [input]"

SCRIPT=".dim_import/xdir"

while true;
do
    size=$(du -s .dim_import | cut -b 1-2)
    if [ $size -gt 32 ]; then
        if [ ! -f ~/files ]; then
          echo "SR Number (i [number]): "; 
          read srNum < ~/.in
          $SCRIPT $srNum
          sleep 10
          ~/scripts/listSR
        fi
    fi
    if [ ~/ ]
    sleep 5
done

Profile function:

function i
{
  set +u
  echo $1 > ~/.in
  set -u
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.