Re: ksh: use find command with quoting (145 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 5 (176 Views)

ksh: use find command with quotings

hello,

 

i want to make "find" variable with different options and store this options in a shell script variable.

 

example ( find a directory named "testdir" ) :

 

mkdir testdir
find . -type d -name "testdir"

 

now variable and it doesn't work:

 

find_opts="-type d -name \"testdir\""
find . ${find_opts} -print     

 

only with eval it works

find . $( eval echo ${find_opts} ) -print

 

Output:


./testdir

- do i need to quote the name of the directory ( -name "testdir"

- does a easier way exist ?

 

regards

Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,736
Registered: ‎06-21-2000
Message 2 of 5 (164 Views)

Re: ksh: use find command with quotings

If you are using a complete directory name in your find command, then no you should not need to quote.  Quotes will come into play if you use a wildcard as part of the directory name.

 

For exampe --

 

No need to quote this:

# find /dir -type d -name some_other_dir

 

Quotes needed:

# find /dir -type d -name "some_other*"

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,888
Registered: ‎03-06-2006
Message 3 of 5 (145 Views)

Re: ksh: use find command with quoting

[ Edited ]

>do I need to quote the name of the directory (-name "testdir")

 

No.

$ find_opts="-type d -name \"testdir\""
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
"testdir"
$ find_opts="-type d -name testdir"
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir

Spaces are a problem:

$ find_opts="-type d -name test dir"
$ printenv $find_opts
the count is 6
printenv
-type
d
-name
test
dir

 

Wildcards may be a problem:

$ find_opts="-type d -name testdir*"
$ mkdir testdir2
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir2

Quoting of var use doesn't help:

$ find_opts='-type d -name testdir*'
$ printenv "$find_opts"
the count is 2
printenv
-type d -name testdir*

 

This doesn't help:

$ find_opts='-type d -name testdir*'
$ printenv $find_opts
the count is 5
printenv
-type
d
-name
testdir2

 

You could use arrays which can maintain quoting:

$ set -A find_opts -- -type d -name "testdir*"

$ printenv "${find_opts[@]}"
the count is 5
printenv
-type
d
-name
testdir*

Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 5 (126 Views)

Re: ksh: use find command with quoting

hello,

 

printenv  only works with following steps ( ksh )

 

$ find_opts="-type d -name test dir"

$ export find_opts
$ printenv $find_opts

 

and the output is only in one line

 

regards

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,888
Registered: ‎03-06-2006
Message 5 of 5 (117 Views)

Re: ksh: use find command with quoting

>printenv  only works with following steps

 

Sorry, printenv is my own program, not printenv(1).

It also allows me to see what is passed on the command line.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation