Re: ksh: use find command with quoting (239 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 5 (270 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

Honored Contributor
Patrick Wallek
Posts: 13,774
Registered: ‎06-21-2000
Message 2 of 5 (258 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*"

Acclaimed Contributor
Dennis Handly
Posts: 25,070
Registered: ‎03-06-2006
Message 3 of 5 (239 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*

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 5 (220 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

Acclaimed Contributor
Dennis Handly
Posts: 25,070
Registered: ‎03-06-2006
Message 5 of 5 (211 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.

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.