Help using variable in find rule


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help using variable in find rule
# 1  
Old 10-02-2012
Help using variable in find rule

I'm not able to use a variable in my find rule. It's essentially being ignored.

I'm trying to store a list of file types to ignore in a variable.

This is the relevant code.

Code:
#!/bin/ksh

EXCEPTIONS='-not -name "*.xom" -a -not -name "*.sh" -a -not -name "*.pl"'

/usr/local/bin/find path/to/files $EXCEPTIONS > FileList.txt

When I use the contents of the EXCEPTIONS variable directly, the find rule works. However the variable doesn't work at all? Is this even possible?
# 2  
Old 10-02-2012
Try:
Code:
eval /usr/local/bin/find path/to/files $EXCEPTIONS > FileList.txt

--
Bye
# 3  
Old 10-02-2012
Thank you so much. I was putting my eval in the wrong place.
# 4  
Old 10-02-2012
The short answer is, you cannot put quotes in quotes that way directly, the shell won't do that kind of doublethink. You asked for literal quotes, so it gave you literal quotes; making them go away takes an eval, which means big trouble. If someone manages to cram `rm -RF ~/ into your string, eval will execute that!

I'd do this instead:

Code:
set -- "-not" "-name" "*.xom" "-a" "-not" "-name" "*.sh" "-a" "-not" "-name" "*.pl"

find ... "$@"

This lets you do splitting on arguments instead of spaces, preserving your strings literally without having to deal with quotes inside quotes. This avoids the eval, and makes your code much safer.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-02-2012
Quote:
Originally Posted by Corona688
The short answer is, you cannot put quotes in quotes that way directly, the shell won't do that kind of doublethink.
Very true! The shell maintains literally a switch, which flips between "inside quotes" and "outside quotes". Once it encounters a quotation mark, the switch flips into the other position. Per default it is on "outside a quotation". If the shell encounters the first quotation mark the switch flips into "inside quotes" and everything is interpreted accordingly, once it encounters the second quote the switch flips back to "outside quotes" again.

This is why quotes cannot be nested in any way.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 6  
Old 10-02-2012
Quote:
Originally Posted by Corona688
If someone manages to cram `rm -RF ~/ into your string, eval will execute that!
Uhm... How can it be done without having write access to the script?

Because, if someone has write access to the script, she can modify it as she likes (regardless of the presence of eval).
--
Bye
# 7  
Old 10-02-2012
Quote:
Originally Posted by Lem
Uhm... How can it be done without having write access to the script?
Because the command is being run from a variable. I'm guessing he didn't do that on a lark, but because he wanted the contents to vary depending on how the script is used.

That leaves room for injection of malicious strings unless done very, very carefully.

It's also completely avoidable by just not using eval at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find no of underscores in a variable?

Hi i have a variable var=a_b_c i want command to find no. of underscores in a variable Thank you (7 Replies)
Discussion started by: pracheth
7 Replies

2. Shell Programming and Scripting

Find string in variable

korn shell, testing if a pattern exist in a string, x is constant, y could be none,all or any combination of the elements of x, x="aa bb cc dd ee ff gg hh ii jj kk ll" y="ff kk" for z in `echo ${x}` do if <any pattern in $y matches any pattern in $x> ] then do something fi... (3 Replies)
Discussion started by: squrcles
3 Replies

3. Solaris

Jumpstart -- Warning: Could not find matching rule in rules.ok

I just setup a new jumpstart server, and I'm having problems with rules.ok errors. I'm coming up blank after many Google searches, forum searches, etc..... This is the error I receive: Skipped interface e1000g1 Attempting to configure interface e1000g0... Configured interface e1000g0... (0 Replies)
Discussion started by: christr
0 Replies

4. IP Networking

iptables - most easy way to find rule and remove it?

I have situation where I have rules in iptables with comments. Now... I can for example enter rule like "iptables -A FORWARD -s xxx -j ACCEPT" and delete it with "iptables -D FORWARD -s xxx -j ACCEPT".. but if that rule contain some random comment (-m comment) then ... ? I can find with scripting... (2 Replies)
Discussion started by: darkman_hr
2 Replies

5. Shell Programming and Scripting

how to find the value of a variable in zsh?

I have a zsh script written by someone else, I am trying to modify it to work on slightly different data -problem is I know nothing about shell scripting. I am trying to muddle through this myself since I need to learn but can someone tell me how to debug a script? ie. I want to display the value... (6 Replies)
Discussion started by: cmp260
6 Replies

6. Shell Programming and Scripting

How can we use a variable value in find command

Hi All: I want to write a shell script to store OS current date - 1 day value in a variable. Current Date is: 22042010 and new value is 21042010 I have multiple files having date value in name in ddmmyyyy format for multiple day, so I want to find all the files based on the date... (3 Replies)
Discussion started by: lodhi1978
3 Replies

7. Shell Programming and Scripting

Find whether the variable holds value or not

when i run a select query in database, it will return the objid based on the condtion. If there is no objid tht meets the condition, I will not get any value returned and the variable will hold nothing... so now how to chk tht the variable is blank or not. I tried the below piece of code: if ;... (6 Replies)
Discussion started by: harish409
6 Replies

8. UNIX for Advanced & Expert Users

Find a environment variable Value !

There is shell script which contains some variables . These variables are used , but they have declared in the script. I think its a environment variable, so I want to know where these variables are set and given values In .profile or .login..or where to c Please give the full path and file... (3 Replies)
Discussion started by: tkbharani
3 Replies

9. UNIX for Advanced & Expert Users

Find a environment variable Value !

There is shell script which contains some variables . These variables are used , but they have not declared in the script. I think its a environment variable, so I want to know where these variables are set and given values In .profile or .login..or where to c Please give the full path and... (1 Reply)
Discussion started by: tkbharani
1 Replies

10. Shell Programming and Scripting

Find and replace the value in a variable

I have a variable whose value is I="user1:x:1100:1200:ID for user1:/home/user1:/bin/ssh-dummy-shell" I want to replace the last part '/bin/ssh-dummy-shell' with '/bin/true' I tried using sed but it garbled sed 's/\/bin\/ssh-dummy-shell/\/bin\/true' $I Thanks for the help (5 Replies)
Discussion started by: aajmani
5 Replies
Login or Register to Ask a Question