Exec not outputting result to function - bash

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 \"\{}\" \;

\"\{}\" 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 "$#"' _ {} \;
Do not use function in bash. Just codec_scan() {. See https://wiki.bash-hackers.org/scripting/obsolete
You may be interested in -iname.

Related

Bash: How to use functions with parameters with find and ssh

I'm trying to search for files of a specific type on a remote ssh client, and want to call a function with the filename passed as a function parameter:
out=$(ssh operator#$IP << EOF
check_cert_date () {
echo "checking" $1
}
$(typeset -f)
find /opt -iname *.der -o -iname *.pem -exec bash -c 'for arg; do check_cert_date "$arg"; done' - {} \;
EOF
)
Files are found, but the filename itself is not passed to check_cert_date(), i.e. $1 is always empty.
Watch out for quoting with 'Here Documents'. Use << "EOF".
Also, find needs parens for your action to apply to both *.der and *.pem files:
find /opt \( -iname *.der -o -iname *.pem \) -print | while read -r file; do check_cert_date "$file"; done

Trap output in exec part of find

myfunc() {
echo "Hello"
}
export -f myfunc
find / -type f -exec bash -c 'myfunc "$0"' {} \;
var="$(myfunc)"
echo "$var"
This will return Hello, I know. But is there a way I can trap $var in exec part
of find?
I would like to append result of find to global array.
If you want the result of find, you have to put the find command between the $( ).
If you want an array, you have to use extra ( ).
var=($(find / -type f -exec bash -c 'myfunc "$0"' {} \;))

How to put custom function's output into find utility as an option?

I want to send a list of extensions as a parameter. So I wrote a small helper function that parses a string of extensions and formats it in a way that "find" utility expects:
exts="txt log";
track=0;
function ext_parse()
{
for i in $exts; do
if [[ $track -eq 0 ]]
then
varstr="-iname \"*.$i\"";
track=1;
else
varstr="$varstr -o -iname \"*.$i\" ";
fi
done
echo "$varstr";
}
So it returns:
-iname "*.txt" -o -iname "*.log"
If I put this into "find" directly it works well:
find . -type f \( -iname "*.txt" -o -iname "*.log" \) -print
But any attempt to substitute this string with the function above that I've tried fails.
Is there any way to obtain that behavior or it is impossible by design?
I would argue it's cleaner, safer and easier to use arrays:
ext_parse() {
local i
varstr=()
for i; do
((${#varstr[#]}!=0)) && varstr+=( -o )
varstr+=( -iname "*.$i" )
done
}
To use this function, you would first call it with appropriate arguments, e.g., ext_parse txt log; this will set the array varstr; and then you can use it as:
find -type f \( "${varstr[#]}" \)
So your workflow looks like this:
$ ext_parse txt log
$ find -type f \( "${varstr[#]}" \)
RE: your comment: to clarify your worry about find being run once per element in array (which is wrong!), do the following test: save the following script as banana:
#!/bin/bash
for ((i=1;i<=$#;++i)); do
printf 'Argument %d: %s\n' "$i" "${!i}"
done
Then chmod +x banana, and try it:
$ ext_parse txt log
$ ./banana -type f \( "${varstr[#]}" \)
Argument 1: -type
Argument 2: f
Argument 3: (
Argument 4: -iname
Argument 5: *.txt
Argument 6: -o
Argument 7: -iname
Argument 8: *.log
Argument 9: )
So you can see that banana is executed once only, with all the arguments given above: exactly what you want!

xargs how to put result {} into $(cmd {})?

for example:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|xargs -I{} echo "{} =>" $(realpath {})
I would like it to output:
/usr/lib/libblas.so.3gf=>/usr/lib/libblas/libblas.so.3gf.0
/usr/lib/libblas.so=>/usr/lib/libblas/libblas.so.3gf.0
/usr/lib/libblas.a=>/usr/lib/libblas/libblas.a
This will not work because the value in $() is expanded before the script actual running.
Is there any way I can achieve this result? without loop in bash?
Alternatively:
find /usr/lib -maxdepth 1 -type l \
-exec echo -n '{} =>' \; \
-exec realpath '{}' \;
Have xargs call the shell:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|xargs -I{} sh -c 'echo "{} =>" $(realpath {})'
You need the command substitution to happen after the file name is known. So you need xargs to call a shell and do the substitution there.
Since you're running the command on a single file at a time, using xargs is a useless complication (and it also mangles some file names). Use -exec!
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'echo "$0 => $(realpath "$0")' {} \;
You could make this slightly faster and not less clear by not using a command substitution:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'echo -n "$0 => "; realpath "$0"' {} \;
To make things a little faster, don't invoke a new shell process for every file:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*" -exec sh -c 'for x; do echo -n "$x => "; realpath "$x"; done' _ {} +
(You can do the same with xargs, but just drop xargs and stick to the simpler, faster, more robust -exec.)
Try to convert each filename separately using line-by-line "while" loop:
find ... | while read f; do echo "$f" '=>' "$(realpath $f)" ; done
The shortest seems to be using GNU Parallel:
find /usr/lib -maxdepth 1 -type l -iname "*libblas*"|parallel echo {} '=\> $(readlink -f {})'

find -exec with multiple commands

I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.
find accepts multiple -exec portions to the command. For example:
find . -name "*.txt" -exec echo {} \; -exec grep banana {} \;
Note that in this case the second command will only run if the first one returns successfully, as mentioned by #Caleb. If you want both commands to run regardless of their success or failure, you could use this construct:
find . -name "*.txt" \( -exec echo {} \; -o -exec true \; \) -exec grep banana {} \;
find . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" \;
One of the following:
find *.txt -exec awk 'END {print $0 "," FILENAME}' {} \;
find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} \;
find *.txt -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;
Another way is like this:
multiple_cmd() {
tail -n1 $1;
ls $1
};
export -f multiple_cmd;
find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
in one line
multiple_cmd() { tail -1 $1; ls $1 }; export -f multiple_cmd; find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
"multiple_cmd()" - is a function
"export -f multiple_cmd" - will export it so any other subshell can see it
"find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;" - find that will execute the function on your example
In this way multiple_cmd can be as long and as complex, as you need.
Hope this helps.
There's an easier way:
find ... | while read -r file; do
echo "look at my $file, my $file is amazing";
done
Alternatively:
while read -r file; do
echo "look at my $file, my $file is amazing";
done <<< "$(find ...)"
Extending #Tinker's answer,
In my case, I needed to make a command | command | command inside the -exec to print both the filename and the found text in files containing a certain text.
I was able to do it with:
find . -name config -type f \( -exec grep "bitbucket" {} \; -a -exec echo {} \; \)
the result is:
url = git#bitbucket.org:a/a.git
./a/.git/config
url = git#bitbucket.org:b/b.git
./b/.git/config
url = git#bitbucket.org:c/c.git
./c/.git/config
I don't know if you can do this with find, but an alternate solution would be to create a shell script and to run this with find.
lastline.sh:
echo $(tail -1 $1),$1
Make the script executable
chmod +x lastline.sh
Use find:
find . -name "*.txt" -exec ./lastline.sh {} \;
Thanks to Camilo Martin, I was able to answer a related question:
What I wanted to do was
find ... -exec zcat {} | wc -l \;
which didn't work. However,
find ... | while read -r file; do echo "$file: `zcat $file | wc -l`"; done
does work, so thank you!
1st answer of Denis is the answer to resolve the trouble. But in fact it is no more a find with several commands in only one exec like the title suggest. To answer the one exec with several commands thing we will have to look for something else to resolv. Here is a example:
Keep last 10000 lines of .log files which has been modified in the last 7 days using 1 exec command using severals {} references
1) see what the command will do on which files:
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "echo tail -10000 {} \> fictmp; echo cat fictmp \> {} " \;
2) Do it: (note no more "\>" but only ">" this is wanted)
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "tail -10000 {} > fictmp; cat fictmp > {} ; rm fictmp" \;
I usually embed the find in a small for loop one liner, where the find is executed in a subcommand with $().
Your command would look like this then:
for f in $(find *.txt); do echo "$(tail -1 $f), $(ls $f)"; done
The good thing is that instead of {} you just use $f and instead of the -exec … you write all your commands between do and ; done.
Not sure what you actually want to do, but maybe something like this?
for f in $(find *.txt); do echo $f; tail -1 $f; ls -l $f; echo; done
should use xargs :)
find *.txt -type f -exec tail -1 {} \; | xargs -ICONSTANT echo $(pwd),CONSTANT
another one (working on osx)
find *.txt -type f -exec echo ,$(PWD) {} + -exec tail -1 {} + | tr ' ' '/'
A find+xargs answer.
The example below finds all .html files and creates a copy with the .BAK extension appended (e.g. 1.html > 1.html.BAK).
Single command with multiple placeholders
find . -iname "*.html" -print0 | xargs -0 -I {} cp -- "{}" "{}.BAK"
Multiple commands with multiple placeholders
find . -iname "*.html" -print0 | xargs -0 -I {} echo "cp -- {} {}.BAK ; echo {} >> /tmp/log.txt" | sh
# if you need to do anything bash-specific then pipe to bash instead of sh
This command will also work with files that start with a hyphen or contain spaces such as -my file.html thanks to parameter quoting and the -- after cp which signals to cp the end of parameters and the beginning of the actual file names.
-print0 pipes the results with null-byte terminators.
for xargs the -I {} parameter defines {} as the placeholder; you can use whichever placeholder you like; -0 indicates that input items are null-separated.
I found this solution (maybe it is already said in a comment, but I could not find any answer with this)
you can execute MULTIPLE COMMANDS in a row using "bash -c"
find . <SOMETHING> -exec bash -c "EXECUTE 1 && EXECUTE 2 ; EXECUTE 3" \;
in your case
find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
i tested it with a test file:
[gek#tuffoserver tmp]$ ls *.txt
casualfile.txt
[gek#tuffoserver tmp]$ find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
testonline1=some TEXT
./casualfile.txt
Here is my bash script that you can use to find multiple files and then process them all using a command.
Example of usage. This command applies a file linux command to each found file:
./finder.sh file fb2 txt
Finder script:
# Find files and process them using an external command.
# Usage:
# ./finder.sh ./processing_script.sh txt fb2 fb2.zip doc docx
counter=0
find_results=()
for ext in "${#:2}"
do
# #see https://stackoverflow.com/a/54561526/10452175
readarray -d '' ext_results < <(find . -type f -name "*.${ext}" -print0)
for file in "${ext_results[#]}"
do
counter=$((counter+1))
find_results+=("${file}")
echo ${counter}") ${file}"
done
done
countOfResults=$((counter))
echo -e "Found ${countOfResults} files.\n"
echo "Processing..."
counter=0
for file in "${find_results[#]}"
do
counter=$((counter+1))
echo -n ${counter}"/${countOfResults}) "
eval "$1 '${file}'"
done
echo "All files have been processed."

Resources