Passing wildcard parameters to find via a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing wildcard parameters to find via a variable
# 1  
Old 02-09-2011
Passing wildcard parameters to find via a variable

I have a script to fix permissions which is made up of blocks like:

Code:
FS_ROOT=/home/shared/Photos
FS_EXCLUDE=( \( -path */.webviews -o -path */.thumbnails \) -prune -o )
find $FS_ROOT ${FS_EXCLUDE[@]} -type d -not -perm 2770 -exec chmod 2770 "{}" \;

That fragment works as expected, but no matter what I try I cannot get the following to work:

Code:
FS_ROOT=/home/myhome
FS_EXCLUDE=( \( -path $FS_ROOT/dev -o -path $FS_ROOT/.\* \) -prune -o )
find $FS_ROOT ${FS_EXCLUDE[@]} -type d -not -perm 2770 -exec chmod 2770 {} \;

Without using an array to store the prune parameters it works on the command line and in a script, so there is nothing wrong with the find command itself. But when using the array it is matching hidden files.

I have tried different levels of escaping in case I needed to also escape the slash, but it made no difference.

Also I have tried using escaped single quotes, and running the script in sh and bash modes. The only difference with the latter was a single escaping slash in bash mode causes the wildcard to expand, the same as if no slash was used.

I want to use a variable to hold these parameters though as they are repeated on other find commands for file permissions and ownership, so any help on how to get this to work would be appreciated.

Michael.
# 2  
Old 02-09-2011
Try to quote the variables:

Code:
FS_ROOT=/home/sysadmin/t
FS_EXCLUDE=( \( -path "$FS_ROOT/dev" -o -path "$FS_ROOT/.*" \) -prune -o )
find "$FS_ROOT" "${FS_EXCLUDE[@]}" -type d -not -perm 2770 -exec chmod 2770 {} \;

If that doesn't work, please post an example:

- sample directory tree
- sample command, the actual and the expected output
This User Gave Thanks to radoulov For This Post:
# 3  
Old 02-09-2011
It is always something simple! I probably should quote everything anyway to be safe, I do have files with spaces, but just quoting the ${FS_EXCLUDE[@]} variable was the missing piece to make the script work.

I am struggling to see the logic of why it makes a difference, though, as surely when the array elements are unpacked they would be within the quotes and so passed as a single parameter?

Anyway, it works now, so many thanks.

Michael.
# 4  
Old 02-09-2011
I believe the main problem was the wild card expansion: the * was expanded in the current working directory of the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Parameters to Crontab

Hello Experts, I have a requirement to pass some parameters to Linux cron tab. For ex: My default cron entry looks like this as below: ------------------------------- 55 10 * * --... (7 Replies)
Discussion started by: MaheshChaudhari
7 Replies

2. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

3. Shell Programming and Scripting

passing parameters with spaces

we are using following script to execute stored procedue. The problem is run_pmcmd.ksh script is using $* parameter which is not taking in account 'Men Shirt' parameter which includes spaces. 1. Step 1 run_pmcmd.ksh CONVERT_TEST script for run_pmcmd.ksh /u01/$(whoami)/run_pmcmd.ksh... (11 Replies)
Discussion started by: sandy162
11 Replies

4. Shell Programming and Scripting

passing parameters to the script

how can i make a script to run only when parameters are given, if parameters are not given it should through an error , saying "please enter a parameter" for ex: i want a find command to run only when the parameters are given (4 Replies)
Discussion started by: knip
4 Replies

5. Shell Programming and Scripting

Passing a wildcard in a variable

Hi There I am new to scripting and require some assistance please. I am trying to define a variable with a wildcard in a shell script (.ksh) that will be run on AIX 5300-10. The variable I am trying is: FILES=LLA_*.CSVWhen I run the following section of the script: scp... (2 Replies)
Discussion started by: jimbojames
2 Replies

6. Shell Programming and Scripting

Passing Variable Parameters (C shell)

I am trying to execute a copy command via shell script. However, on occassion, 2 or more files need to copied. How do I code for the multiple arguments? Does it matter how the files are delimited? Example: I have a script to copy files from 1 dir to another called duplicate.csh In most... (1 Reply)
Discussion started by: CKT_newbie88
1 Replies

7. Shell Programming and Scripting

Passing unix variable to oracle parameters

Please help me how to pass some unix vairable to oracle. I have used below , but not displaying passed (inval) value. calling() { sqlplus -s $1/$2@$3 <<EOF begin exec call_sql($4); end; exit EOF } calling user pwd inst value1... (17 Replies)
Discussion started by: Jairaj
17 Replies

8. Shell Programming and Scripting

passing more than 9 parameters

hi, i am passing around 14 parameters for a script a=$1 b=$2 c=$3 d=$4 e=$5 f=$6 g=$7 h=$8 i=\"${9}\" shift j=\"${1}\" still for j it is displaying the 1st parameter value..how to make it take the 10th parameter (2 Replies)
Discussion started by: dnat
2 Replies

9. UNIX for Dummies Questions & Answers

Passing parameters in script

I have 2 scripts: script1 and script2 Script1 passes 4 parameters to script2 as follows #script1 code ... ... script2 $var1 $var2 $var3 $var4 Script2 uses the export command to know to expect these values #script2 export $1 $2 $3 $4 code ... ... The problem that I am having is... (1 Reply)
Discussion started by: eliguy
1 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question