Alan
Alan

Reputation: 2086

Bash Script with Prompts from Xargs?

I have a script I am trying to run:

#!/bin/bash
echo "Please enter desired kbps example 1000 for 1000kbps: "
read desired
echo "Please enter minimum kbps: "
read minrate
echo "Please enter maximum kbps: "
read maxrate

for i; do
filename=$(basename "$i")
extension="${filename##*.}"
filename="${filename%.*}"
size=k
    d=$(dirname "$i")
#    b=$(basename "$i")
ffmpeg -i "$i" -c:v hevc_nvenc -preset medium -crf 28 -b:v $desired$size -minrate $minrate$size -maxrate $maxrate$size -bufsize 25M -c:a copy "$d/X265_$filename.mp4"
mv "$i" "$d/zzz_$filename.$extension"
done

Using this find command with xargs:

find ./ -type f -regex ".*\.\(mkv\|mp4\|wmv\|flv\|webm\|mov\|avi\)" -print0 | xargs -0 /root/batch.sh

I want it to prompt me once to ask for the bitrate variables, but it doesn't pause for info when utilizing the find command, but it does if I run the script directly.

Is there a way to combine the find command into the other script to make it a simple command that then prompts me for info? If not, what can I do to get it to prompt me?

Upvotes: 2

Views: 233

Answers (2)

gboffi
gboffi

Reputation: 25053

You can completely omit the xargs part using the -exec ... + option to the find command.

Here I've a script that is similar to yours

$ cat test.sh
#!/bin/sh
read a
for i ; do echo -n $i.$a' ' ; done
echo
$

a directory with a few files (NB the file names have embedded spaces)

$ ls -1 one
1 1
2 1
3 1
4 1
$

and eventually an example of using find with the -exec ... + option

$ find  one -type f -exec sh test.sh '{}' '+'
asdef
one/2 1.asdef one/3 1.asdef one/1 1.asdef one/4 1.asdef 
$

PS I've edited my answer to reflect OP's concern about filenames with spaces etc... fortunately find treats the problem on its own, w/o any problem.

Upvotes: 3

Barmar
Barmar

Reputation: 781726

The problem is that the shell script inherits its standard input from xargs, and its input is the pipe from find. So the script is trying to read from that pipe.

If you can modify the script, change it to read the inputs from /dev/tty instead of standard input. Put this at the beginning of the script:

exec </dev/tty

Upvotes: 3

Related Questions