HandBrakeCLI command break while loop? - bash

In a bash script, result of find is
/path/to/file1.nrg
/path/to/file2.nrg
/path/to/file3.nrg
i have this while loop:
process preset
processpreset ()
{
x=$1
# Replace , by -o -iname for file types.
iname=" -o -iname \*."
# Find specified files. Eval allow var prst1_in with find.
eval "find "$fpath" -type f \( -iname \*."${prst_in[x]//,/$iname}" \) -size ${prst_lim_size[x]}" | sort | while read -r i
do
titles=$(HandBrakeCLI --input "$i" --scan |& grep -Po '(?<=DVD has )([0-9]+)')
if (( $titles > 1 )); then
echo "DVD has $titles title(s)"
fi
done
}
the script only echo 1 time File has 8 title(s) after it stop, when using titles="8" the loop echo for all files in folder. Can anyone point me my error please?
EDIT: what work for me, many thanks Anubhava
processpreset ()
{
x=$1
# Replace , by -o -iname for file types.
iname=" -o -iname \*."
# Find specified files. Eval allow var prst1_in with find.
eval "find "$fpath" -type f \( -iname \*."${prst_in[x]//,/$iname}" \) -size ${prst_lim_size[x]}" | sort | while read -r i
do
titles="$(echo ""|HandBrakeCLI --input "$i" --scan |& grep -Po '(?<=DVD has )([0-9]+)')"
if (( $titles > 1 )); then
echo "DVD has $titles title(s)"
fi
done
}
the echo ""| fix the problem.

ok try this script:
while read -r i
do
echo "i is: $i"
titles="$(echo ""|HandBrakeCLI --input "$i" --scan | grep -Po '(?<=DVD has )([0-9]+)')"
if (( titles > 1 )); then
echo "DVD has $titles title(s)"
fi
done < <(find "$imgpath" -type f \( -iname \*.iso -o -iname \*.nrg -o -iname \*.img \) | sort)

Related

bash find where starting directory has a space

MacOSX Bash terminal:
find . -type f
is straightforward
find A -type f
where A is a directory is equally straightforward
However I have a directory
A\ copy
(on the system it looks like
A copy of course)
I want to search this as the starting directory, ideally using a string variable for convenience. Find cannot deal with the space in the directory name... I have tried
temp="A\ copy" ; find "$temp" -type f
temp="A copy" ; find "$temp" -type f
temp="A copy" ; find \""$temp"\" -type f
and pretty much every other combination in that set you can think of. What am I doing wrong? Grateful for assistance and apologies if its a newbie question but I did try to look everywhere on this one.
This is the full code so you can see what I am trying to do
function find_missing_files_two_input_directories () {
if [[ -z "$1" ]] || [[ -z "$2" ]]; then echo "enter directory" ;
else
firstDirectory=$(absolutePath "$1")
secondDirectory=$(absolutePath "$2")
gfind "$firstDirectory" -type f -printf "%f\n" | sort > /tmp/compare_directory_1
gfind "$secondDirectory" -type f -printf "%f\n" | sort > /tmp/compare_directory_2
diff /tmp/compare_directory_1 /tmp/compare_directory_2 | grep -e ">" -e "<" | tee /tmp/tmp_compare_output
cat /tmp/tmp_compare_output | sed 's/<//' | sed 's/>//' > /tmp/compare_output
mkdir -p /tmp/compare_validation_copies
while IFS= read -r line ; do \
echo "$line"
gfind "$firstDirectory" -type f -name "$line"
#gfind "$firstDirectory" -type f -name "$line" -print -exec \gcp --backup="t" "{}" /tmp/compare_validation_copies
gfind "$secondDirectory" -type f -name "$line"
#gfind "$secondDirectory" -type f -name "$line" -print -exec \gcp --backup="t" "{}" /tmp/compare_validation_copies
done < /tmp/compare_output
\rm /tmp/compare_directory_1 /tmp/compare_directory_2
#open /tmp/compare_validation_copies
#open -a Terminal /tmp/compare_validation_copies
fi
}
function find_missing_files_two_input_directories () {
if [[ -z "$1" ]] || [[ -z "$2" ]]; then echo "enter directory" ;
else
firstDirectory=$(absolutePath "$1")
secondDirectory=$(absolutePath "$2")
gfind "$firstDirectory" -type f -printf "%f\n" | sort > /tmp/compare_directory_1
gfind "$secondDirectory" -type f -printf "%f\n" | sort > /tmp/compare_directory_2
diff /tmp/compare_directory_1 /tmp/compare_directory_2 | grep -e ">" -e "<" | tee /tmp/tmp_compare_output
echo "next bit"
cat /tmp/tmp_compare_output | sed 's/<//' | sed 's/>//' > /tmp/compare_output
mkdir -p /tmp/compare_validation_copies
while IFS= read -r line ; do \
gfind "$firstDirectory" -type f -name $line -exec \gcp --backup="t" "{}" /tmp/compare_validation_copies \;
gfind "$secondDirectory" -type f -name $line -exec \gcp --backup="t" "{}" /tmp/compare_validation_copies \;
done < /tmp/compare_output
\rm /tmp/compare_directory_1 /tmp/compare_directory_2
open /tmp/compare_validation_copies
open -a Terminal /tmp/compare_validation_copies
fi
}

find and grep / zgrep / lzgrep progress bar

I would like to add a progress bar to this command line:
find . \( -iname "*.bz" -o -iname "*.zip" -o -iname "*.gz" -o -iname "*.rar" \) -print0 | while read -d '' file; do echo "$file"; lzgrep -a stringtosearch\.anything "$file"; done
The progress file should be calculated on the total of compressed size files (not on the single file).
Of course, it can be a script too.
I would also like to add other progress bars, if possible:
The total number of files processed (example 3 out of 21)
The percentage of progress of the single file
Can anybody help me please?
Here some example of it should look alike (example from here):
tar cf - /folder-with-big-files -P | pv -s $(du -sb /folder-with-big-files | awk '{print $1}') | gzip > big-files.tar.gz
Multiple progress bars (example from here):
pv -cN orig < foo.tar.bz2 | bzcat | pv -cN bzcat | gzip -9 | pv -cN gzip > foo.tar.gz
Thanks,
This is the first time I've ever heard of pv and it's not on any machine I have access to but assuming it needs to know a total at startup and then a number on each iteration of a command, you could do something like this to get a progress bar per file processed:
IFS= readarray -d '' files < <(find . -whatever -print0)
printf '%s\n' "${files[#]}" | pv -s "${#files[#]}" | command
The first line gives you an array of files so you can then use "${#files[#]}" to provide pv it's initial total value (looks like you use -s value for that?) and then do whatever you normally do to get progress as each file is processed.
I don't see any way to tell pv that the pipe it's reading from is NUL-terminated rather than newline-terminated so if your files can have newlines in their names then you'd have to figure out how to solve that problem.
To additionally get progress on a single file you might need something like:
IFS= readarray -d '' files < <(find . -whatever -print0)
printf '%s\n' "${files[#]}" |
pv -s "${#files[#]}" |
xargs -n 1 -I {} sh -c 'pv {} | command'
I don't have pv so all of the above is untested so check the syntax, especially since I've never heard of pv :-).
Thanks to Max C., I found a solution for the main question:
find ./ -type f -iname *\.gz -o -iname *\.bz | (tot=0;while read fname; do s=$(stat -c%s "$fname"); if [ ! -z "$s" ] ; then echo "$fname"; tot=$(($tot+$s)); fi; done; echo $tot) | tac | (read size; xargs -i{} cat "{}" | pv -s $size | lzgrep -a something -)
But this work only for gz and bz files, now I have to develop to use different tool according to extension.
I'm gonna to try the Ed solution too.
Thanks to ED and Max C., here the verision 0.2
This version work with zgrep, but not with lzgrep. :-\
#!/bin/bash
echo -n "collecting dump... "
IFS= readarray -d '' files < <(find . \( -iname "*.bz" -o -iname "*.gz" \) -print0)
echo done
echo "Calculating archives size..."
tot=0
for line in "${files[#]}"; do
s=$(stat -c\%s "$line")
if [ ! -z "$s" ]
then
tot=$(($tot+$s))
fi
done
(for line in "${files[#]}"; do
s=$(stat -c\%s "$line")
if [ ! -z "$s" ]
then
echo "$line"
fi
done
) | xargs -i{} sh -c 'echo Processing file: "{}" 1>&2 ; cat "{}"' | pv -s $tot | zgrep -a anything -

Passing a date/time value to touch command within bash function

I am trying to write a bash function (Mac OS X) which searches for a specific range of files between two input date/times. Most variations I have tried for evaluating the two inputs $1 and $2 fail. Hardcoding the times works fine (as per usage line below) i.e. the search syntax is fine. Grateful for pointers where I'm going wrong on passing the two inputs to the touch commands.
function ffiles_search1 () {
echo "usage start 201911270000 end 201912102359 "
touch -t $(eval echo "$1") /tmp/lower-date && touch -t $(eval echo "$2") /tmp/upper-date && find . -path "./Library" -prune -o -type f -a -newer /tmp/lower-date -a ! -newer /tmp/upper-date -a -size +32k -a ! -size +1024k -print0 | xargs -0 ls -ld | egrep -iv "|ppt|doc"
}
Corrected code, invoking as
ffiles_search 201911270000 201912102359
on the function
function ffiles_search () {
echo "usage start 201911270000 end 201912102359 "
touch -t "$1" /tmp/lower-date &&
touch -t "$2" /tmp/upper-date &&
find . -path "./Library" -prune -o -type f -a -newer /tmp/lower-date \
-a ! -newer /tmp/upper-date -a -size +32k -a -size -1024k -print0 |
xargs -0 ls -ld |
egrep -iv -f $HOME/Scripts/egrep_exclusions/time_search.txt
}

Bash Script interactive mv issues

I'm working on a bash script to help organize files and I want to use mv -i to make sure I don't write over something important.
The script is working right now except for the -i for the mv.
It shows (y/n [n]) not overwritten part, but then goes and and doesn't allow me to interact with it.
createList()
{
ls *.epub | sed 's/-.*//' |uniq >> list.txt
ls *.mobi | sed 's/-.*//' |uniq >> list2.txt
}
atag()
{
find /Users/j/Desktop/Source -maxdepth 1 -iname "*.epub" -type f -print0 | xargs -0 -I '{}' tag -a Purple {}
find /Users/j/Desktop/Source -maxdepth 1 -iname "*.mobi" -type f -print0 | xargs -0 -I '{}' tag -a Purple {}
}
moveEpub()
{
while read -r line; do
if [ -d "/Users/j/Desktop/Dest/$line" ]; then
if [ -d "/Users/j/Desktop/Dest/$line/EPUB" ]; then
find /Users/j/Desktop/Source/ -maxdepth 1 -iname "*$line*" -and ! -iname ".*$line*" -type f -print0 | xargs -0 -I '{}' mv -i {} /Users/j/Desktop/Dest/"$line"/EPUB/
else
mkdir "/Users/j/Desktop/Dest/$line/EPUB"
find /Users/j/Desktop/Source/ -maxdepth 1 -iname "*$line*" -and ! -iname ".*$line*" -type f -print0 | xargs -0 -I '{}' mv -i {} /Users/j/Desktop/Dest/"$line"/EPUB/
fi
fi
done < "list.txt"
}
moveMobi()
{
while read -r line; do
if [ -d "/Users/j/Desktop/Dest/$line" ]; then
if [ -d "/Users/j/Desktop/Dest/$line/MOBI" ]; then
find /Users/j/Desktop/Source/ -maxdepth 1 -iname "*$line*" -and ! -iname ".*$line*" -type f -print0 | xargs -0 -I '{}' mv -i {} /Users/j/Desktop/Dest/"$line"/MOBI/
else
mkdir "/Users/j/Desktop/Dest/$line/MOBI"
find /Users/j/Desktop/Source/ -maxdepth 1 -iname "*$line*" -and ! -iname ".*$line*" -type f -print0 | xargs -0 -I '{}' mv --interactive {} /Users/j/Desktop/Dest/"$line"/MOBI/
fi
fi
done < "list2.txt"
}
clear
createList
atag
moveEpub
moveMobi
rm list.txt
rm list2.txt
If you want mv -i to interact with the terminal, that means its stdin needs to be attached to that terminal. There are several places, here, where you're overriding stdin.
For instance:
# THIS LOOP OVERRIDES STDIN
while read -r line
...
done <list.txt
...redirects stdin for the entire duration of the loop, so instead of reading from the user, mv reads from list.txt. To change this, use a different file descriptor:
# This loop uses FD 3 for stdin
while read -r line <&3
...
done 3<list.txt
Another place is in calling xargs. Instead of:
# Overrides stdin for xargs and mv to contain output from find
find ... -print0 | xargs -0 -I '{}' mv -i '{}' "$dest"
...use:
# directly executes mv from find, stdin not modified
find ... -exec mv -i '{}' "$dest" ';'
That said, I would suggest ditching list.txt and list2.txt altogether; you simply don't need them; for that matter, you don't need find either.
dest=/Users/j/Desktop/Dest
source=/Users/j/Desktop/Source
moveEpub() {
local -A finished=( ) # WARNING: This requires bash 4.0 or newer.
for name in *.epub; do
prefix=${name%%-*} # remove everything past the first dash
[[ ${finished[$prefix]} ]] && continue # skip if already done with this prefix
finished[$prefix]=1 # set flag to skip other files w/ this prefix
[[ -d $dest/$prefix ]] || continue # skip if no directory exists for this prefix
mkdir -p "$dest/$prefix/EPUB" # create destination if not existing
mv -i "$source"/*"$prefix"* "$dest/$prefix/EPUB"
done
}
You can use built in find action -exec instead of piping to xargs :
find /Users/j/Desktop/Source/ -maxdepth 1 \
-iname "*$line*" -and ! -iname ".*$line*" -type f \
-exec mv -i {} /Users/j/Desktop/Dest/"$line"/EPUB/ \;

Code in bashrc doesn't work

The code below doesn't work in bashrc but works in terminal with other arguments null.
search () {
find $1 -type f | egrep '(.$2|.$3|.$4|.$5|.$6|.$7|.$8|.$9|.$10)'
}
Write this:
search() {
find "$1" -type f \( -true \
-o -name "*$2*" \
-o -name "*$3*" \
-o -name "*$4*" \
-o -name "*$5*" \
-o -name "*$6*" \
-o -name "*$7*" \
-o -name "*$8*" \
-o -name "*$9*" \
-o -name "*$10*" \
\)
}
As #chepner points out, the single quotes prevent the parameters from expanding. Use double quotes.
The egrep will create a line-based match result, which is less precise than the above. It's also slower.
If the above statements are not exactly what you need, keep in mind GNU find has regular expression predicates in addition to -name's pattern matching. There's no need to pipe to grep. You can expand the above function to take an unlimited number of arguments by constructing the arguments to find, such as in this answer.
I didn't know that the egrep get the literal text $2 instead of argument. I solved with this code:
search-type () {
case "$#" in
1) echo "Missing arguments";;
2) find $1 -type f | egrep '(.'$2')';;
3) find $1 -type f | egrep '(.'$2'|.'$3')';;
4) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4')';;
5) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5')';;
6) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5'|.'$6')';;
7) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5'|.'$6'|.'$7')';;
8) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5'|.'$6'|.'$7'|.'$8')';;
9) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5'|.'$6'|.'$7'|.'$8'|.'$9')';;
10) find $1 -type f | egrep '(.'$2'|.'$3'|.'$4'|.'$5'|.'$6'|.'$7'|.'$8'|.'$9'|.'$10')';;
11) echo "Many arguments";;
esac;
}
The #kojiro code doesn't work.
Is it possible to simplify this code with regex?
Thank you guys!
I change the code for the something more simple and clear; and works with any quantity of parameters.
search-type() {
# Flags
flag=0
fld=1
for x in "$#"
do
# The first parameter is the directory; ignored!
if [ $fld = 1 ]; then
fld=0
else
# Verify if have more than one file
if [ $flag = 0 ]; then
cmd='-name '$x
flag=1
else
cmd+=' -o -name '$x
fi
fi
done
find $1 -type f $cmd;
}

Resources