2

I want make a script that runs in background (with &) and checks if a certain file, the name of which is read from the keyboard, exists in multiple directories, given as parameter.

So the script will run in a while true loop, until the file with the given name is created.

The problem is, when I run the script, what I type from read is taken as a normal terminal cmd.

Here's the script:

#!/bin/bash

echo Type the file name
read fileName


while true
do
    for file in $@
    do
        if find $file -name $fileName | grep -q "$fileName"
        then
            echo The file with name $fileName has been created!!!
            break
        fi
    done
done

If I don't run the script with &, it works fine.

3
  • 2
    Load the filename as parameter of the script instead of "loading" it via read. scriptname.sh filename -> in your script the filename will be accesible via $1 variable ($1 is the first parameter $2 second ... ). how-to.wikia.com/wiki/… Commented Apr 11, 2017 at 19:29
  • 2
    Since you're in Linux, consider using inotifywait instead of hammering your disk (or cache) with a while loop. Commented Apr 11, 2017 at 20:16
  • 1
    But to answer your question ... the reason the filename gets taken as shell input is that by backgrounding the script, you detach it from the terminal so that it can't receive input. If you really need to provide input which will be processed by a background script, use something like Fred's suggestion. Commented Apr 11, 2017 at 20:20

3 Answers 3

3

I think what you want to do is send only the part of the script that executes without user input to the background. You can do that if you use & inside the script instead of on the command line.

#!/bin/bash

echo Type the file name
read fileName

while true
do
    for file in "$@"
    do
        if find "$file" -name "$fileName" | grep -q "$fileName"
        then
            echo "The file with name $fileName has been created!!!"
            break
        fi
    done
done &

Please note I have added additional quoting to prevent problems in case of files with names containing special characters.

You could make backgrounding the while loop an option too if needed, so that you could select the behavior you prefer when calling the script.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah that's what I was looking for! Ty!
0

I will replace your script with these commands:

  sleep 2; read -p "x=" x; echo "x=$x"

You can get input without changing the "script" with

  echo Hello | sleep 2; read -p "x=" x; echo "x=$x"

If you want the script put together, use

  echo Hello | (sleep 2; read -p "x=" x; echo "x=$x")

Putting the command in the ackground seems to work nice:

  echo Hello | (sleep 2; read -p "x=" x; echo "x=$x")&

When you want to have echo Hello in the background to, use

  (echo Hello | (sleep 2; read -p "x=" x; echo "x=$x"))&

Comments

0

You can simplify this by supplying the filename in the command line, if it's acceptable, see the following:

#!/bin/bash

fileName="$1"
shift

while true
do
    for file in "$@"
    do
        if find "${file}" -name "${fileName}" | grep -q "${fileName}"
        then
            echo The file with name ${fileName} has been created!!!
            break
        fi
    done
done

You can just launch by:

$ ./script.sh FILE DIR1 DIR2 &

7 Comments

This will also search for $fileName inside $1 (which is a member of $@). Probably not what you intended.
Not clear what you mean... the two scripts are the same, the one I suggest only take the filename as argument (which is $1)
You should really, really use "$@" with proper quoting and similarly quote "$file" and "$fileName" throughout.
What @ghoti is saying is you want to shift before looping over "$@" because otherwise you will search for $1 in $1 which is kind of silly.
I think it works like this. Used $1 and shift (otherwise it would get $1 at some stage, cycling over $@). Thanks both of you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.