How to use find with variable parameters? (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

How to use find with variable parameters? (Solved)

#1 Post by arivas_2005 »

greetings

i find and select file for extension.
i have the code

Code: Select all

declare -a finales
finales[1]=".docx"
finales[2]=".sxw"
finales[3]=".sxc"
finales[4]=".odt"

for NUM  in {1..4}; do
   find /Z_SECCION/1 -maxdepth 1 -type f -name ${finales[$NUM]} 
   #find /Z_SECCION/1 -maxdepth 1 -type f -name ${finales[NUM]} 
   ## echo ${finales[$NUM]}
   # echo ${finales[NUM]}
done
not work
How to use find with variables as parameters

thanks!
Last edited by arivas_2005 on Wed 05 Jul 2017, 19:11, edited 1 time in total.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#2 Post by arivas_2005 »

Excuse me ... the problem was the asterisko

Code: Select all

finales[1]="*.docx"
finales[2]="*.sxw"
finales[3]="*.sxc"
finales[4]="*.odt"
for NUM  in {1..4}; do
   find /Z_SECCION/1 -maxdepth 1 -type f -name "${finales[$NUM]}"
done
 
Thank you again for your attention.

april

#3 Post by april »

What does the directory structure look like that you are finding files in?
I'm seeing /Z_SECCION/1 is that correct?

I changed to "/TempSave/Scripts"
and got this error

Code: Select all

# bash findvarious.sh
find: paths must precede expression: colour2.sh
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find: `/TempSave/Scripts': No such file or directory
find: `/TempSave/Scripts': No such file or directory
find: `/TempSave/Scripts': No such file or directory
colour2.sh appears to be the first file found.
This was what my script looked like

Code: Select all

#!/usr/bin/findvarious.sh

declare -a finales
finales[1]="*.sh"
finales[2]="*.sxw"
finales[3]="*.sxc"
finales[4]="*.odt"
#finales[1]=".docx"
#finales[2]=".sxw"
#finales[3]=".sxc"
#finales[4]=".odt"

for NUM  in {1..4}; do
   find /TempSave/Scripts/ -maxdepth 1 -type f   -name ${finales[$NUM]} 
   #find /Z_SECCION/1 -maxdepth 1 -type f -name ${finales[NUM]}
   ## echo ${finales[$NUM]}
   # echo ${finales[NUM]}
done

#Excuse me ... the problem was the asterisko
#Code:	
#finales[1]="*.docx"
#finales[2]="*.sxw"
#finales[3]="*.sxc"
#finales[4]="*.odt"


#Thank you again for your attention.          arivas_2005

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

@arivas_2005: Please mark your thread as [solved] when you have found the solution to your problem.
It was not clear to me and probably not for april that you don't need any further help.

You seem to love arrays :lol:

If not using them is OK for you and if not even using find is OK you could simply use bash:

Code: Select all

shopt -s nullglob
printf "%s\n" "/Z_SECCION/1/"{*.docx,*.sxw,*.sxc,*.odt}
Unless you have dozens of extensions to search for you could use find without a loop:

Code: Select all

find "/Z_SECCION/1" -maxdepth 1 -name '*.docx' -o -name '*.sxw' -o -name '*.sxc' -o -name '*.odt' 
...and if your version of find supports it you could use regex and make the code a bit shorter:

Code: Select all

find "/Z_SECCION/1" -maxdepth 1 -regextype gnu-awk -regex ".*\.(docx|sxw|sxc|odt)$"
...and if it has to be a loop:

Code: Select all

for ext in docx sxw sxc odt; do
	find "/Z_SECCION/1" -maxdepth 1 -name *.$ext
done
Buena suerte :wink:

april

#5 Post by april »

I am fully aware he has solved it but the subject interests me including your post which somewhat blows me away. Scripting capabilities are just mindblowing. Much food for thought.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#6 Post by musher0 »

Very interesting discussion.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#7 Post by arivas_2005 »

regards
here again :?
excuseme for opening this thread again :oops:
but I have the same problem
and I can not find you out

the scriptillo is

Code: Select all

#!/bin/bash
 comand0=$(Xdialog --title "Preparando los detalles de limpieza en Z_SECCION" \
		--stdout --separator "~" --inputbox "$MENSAJE""Intro command grep with params values Ej: grep -v -i "tre\|fou"" 12 110 "grep")
  cd /Z_SECCION/1
    OLDIFS=$IFS ; IFS=$'\n'
        # line command work fine  ---------------------------
		#for i in $(find ./ -type d ! -iname '*--*' | grep -i -v 'tre\|fou' | sed 's/..//'); do rm -rf $i ; done
		# but, grep in variable comand0 not work  -----------
		for i in $(find ./ -type d ! -iname '*--*' | $comand0 | sed 's/..//'); do rm -rf --no-preserve-root $i ; done
   IFS=$OLDIFS

I want to mix grep with find but passing grep in variable
ex: grep -v -i "tre\|fou"
Online command works perfect, but
when entering it in variable gives the error:
: line 8: grep -i -v 'tre\|fou': command not found grep -v -i "tre\|fou"
what will be the trick to make it work
again thanks for the support

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#8 Post by arivas_2005 »

eureka! :D

after insistent 7 hours of trial and error.
I found the arrangement.
Thank you for the opportunity
Here I leave my scriptillo that works.

Code: Select all

#!/bin/bash
 cd /Z_SECCION/1
  CONDITION=`Xdialog --stdout --inputbox "parametro para find " 8 125 ""`
#   # funciona con CADENA y eval-------------------------------------------
 TMP=$IFS; IFS=$'\n'
  CADENA="find ./ -type d ! -iname '*--*' | "$CONDITION" | sed 's/..//'"
  for i in $(eval $CADENA); do rm -rf $i ; done
  IFS=$TMP  
# grep -i -v "tre\|cua"  for Xdialog
these lines are the solution
CADENA="find ./ -type d ! -iname '*--*' | "$CONDITION" | sed 's/..//'"
$(eval $CADENA)

I spent my Sunday :oops:
greetings for all!

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#9 Post by MochiMoppel »

arivas_2005 wrote:regards
here again :?
excuseme for opening this thread again :oops:
but I have the same problem
This thread has nothing to do with your problem.

Indeed you already described the same problem, but in a different thread. The answer you received should give you a clue why you receive an error and how to avoid it.

Edit: I somehow didn't notice your last post. Anyway, it seems you could have saved some hours by learning from your own threads :lol: .

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#10 Post by arivas_2005 »

my greeting @MochiMoppel
effectively. the key was in the thread:
http://www.murga-linux.com/puppy/viewtopic.php?t=110667
In a moment of lucidity, From there I re-took the idea.
but, while there is confusion, the path is not found.
thanks for your comments :D
and thank you for the support you offer in this medium
successes!
arivas

Post Reply