Home > Software engineering >  Exec not outputting result to function
Exec not outputting result to function

Time:03-01

How do I get the result of the find to be used by the function? I keep getting blank results.

#!/bin/bash

#functions
function codec_scan () {

 echo "paremter 1 is this: $1" # >> $fulllog

}

#exporting
export -f codec_scan

#Main Code
find . -type f \( -name "*.avi" -o -name "*.AVI" -o -name "*.m4v" -o -name "*.mkv" -o -name "*.mp4" -o -name "*.MP4" \) -exec bash -c codec_scan \"\{}\" \;

CodePudding user response:

\"\{}\" is literal characters " with {} inside. It's a sole {}. Nothing before it, nothing after it. Unless you want to add a literal " characters.

It's bash -c 'script...'. The arguments that follow are arguments to the script, not to the function. Each function has its own $1 $2 ... positional arguments, they are separate to script. And they arguments to bash -c start from $0, not from $1, see man bash, it is like bash -c 'script..' <$0> <$1> <$2> ...

You want:

find ....  -exec bash -c 'codec_scan "$@"' bash {} \;

Do not use function in bash. Just codec_scan() {. See https://wiki.bash-hackers.org/scripting/obsolete

You may be interested in -iname.

  • Related