Re: Help Understanding While Loop
in message [EMAIL PROTECTED], wrote Drew Tomlinson thusly... Thus I set the following variables: remote_pictures_dir=/multimedia/Pictures local_pictures_dir=/tv/pictures find_args=-iname '*.jpg' -or -iname '*.gif' Then I called the 'find' command as follows: for original in $(/usr/bin/find $remote_pictures_dir $find_args -print) But when I run my script, I get /usr/bin/find: invalid predicate `-iname '*.jpg' -or -iname '*.gif''. I don't get the invalid predicate; i get nothing printed at all (bash3 sh). However if I don't try and use $find_args and type the arguments in specifically, the script runs fine. Are you really sure about the runs fine part? Here, when the -iname options were not surrounded by '\(' '\)', find searched only for the last option, in this case -iname '*.gif', ignoring all the '*.jpg' files. I tried various combinations of quoting and escaping those quotes but can't come up with a combination that works. Add eval before find so that find recognizes $find_args as separate options not one long string, and group $find_args to make it work what you actually wanted ... for f in $( eval find $dir \( $find_args \) -print ) do echo $f done - Parv -- ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help Understanding While Loop
On 10/15/2005 11:33 PM Parv wrote: in message [EMAIL PROTECTED], wrote Drew Tomlinson thusly... Thus I set the following variables: remote_pictures_dir=/multimedia/Pictures local_pictures_dir=/tv/pictures find_args=-iname '*.jpg' -or -iname '*.gif' Then I called the 'find' command as follows: for original in $(/usr/bin/find $remote_pictures_dir $find_args -print) But when I run my script, I get /usr/bin/find: invalid predicate `-iname '*.jpg' -or -iname '*.gif''. I don't get the invalid predicate; i get nothing printed at all (bash3 sh). However if I don't try and use $find_args and type the arguments in specifically, the script runs fine. Are you really sure about the runs fine part? Here, when the -iname options were not surrounded by '\(' '\)', find searched only for the last option, in this case -iname '*.gif', ignoring all the '*.jpg' files. I thought I was but then after continuing to play around with it I noticed the phenomena you describe above. That's when I added the '\(' as you mention. Good catch! I tried various combinations of quoting and escaping those quotes but can't come up with a combination that works. Add eval before find so that find recognizes $find_args as separate options not one long string, and group $find_args to make it work what you actually wanted ... for f in $( eval find $dir \( $find_args \) -print ) do echo $f done Thank you very much!!! This works great. Drew -- Visit The Alchemist's Warehouse Magic Tricks, DVDs, Videos, Books, More! http://www.alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help Understanding While Loop
On Oct 15, 2005, at 5:59 PM, Drew Tomlinson wrote: On 10/14/2005 3:24 PM David Kirchner wrote: On 10/14/05, Drew Tomlinson [EMAIL PROTECTED] wrote: OK, I've been working on an sh script and I'm almost there. In the script, I created a 'while read' loop that is doing what I want. Now I want to keep track of how many times the loop executes. Thus I included this line between the 'while read' and 'done' statements: count = $(( count + 1 )) I've tested this by adding an 'echo $count' statement in the loop and it increments by one each time the loop runs. However when I attempt to call $count in an 'echo' statement after the 'done', the variable is null. Thus I assume that $count is only local to the loop and I have to export it to make it available outside the loop? What must I do? Oh yeah, that's another side effect of using the while read method. Because it's | while read it's starting a subshell, so any variables are only going to exist there. You'd need to have some sort of 'echo' within the while read, and then | wc -l at the end of the while loop, or something along those lines. The IFS method someone else mentioned, in regards to 'for' loops, would probably be better all around. So you'd want: OLDIFS=$IFS # Note this is a single quote, return, single quote, no spaces IFS=' ' for i in `find etc` do done IFS=$OLDIFS OK, I've tried this and it does fix the count problem. However it messes up another part of the script and I'm trying understand why. I tried to make this script dynamic in that all I would need to do is edit variables set at the top and then not have to worry about all occurrences in the script. Thus I set the following variables: remote_pictures_dir=/multimedia/Pictures local_pictures_dir=/tv/pictures find_args=-iname '*.jpg' -or -iname '*.gif' Then I called the 'find' command as follows: for original in $(/usr/bin/find $remote_pictures_dir $find_args - print) But when I run my script, I get /usr/bin/find: invalid predicate `- iname '*.jpg' -or -iname '*.gif''. However if I don't try and use $find_args and type the arguments in specifically, the script runs fine. I tried various combinations of quoting and escaping those quotes but can't come up with a combination that works. What is going on? And is there some way to set verbosity so I can see how the shell is expanding the variables? Thanks much, Drew IIRC, you can do that be appending a '-x' after #!/bin/sh. Your first line would look like this: #!/bin/sh -x This will result in the script echoing all of the commands as they're executed. As far as the count problem, try declaring the variable before the while loop. For example: doit = 0 count = 0 while [ $doit -lt 4 ] do count=$[$count+1] doit=$[$doit+1] done echo $count HTH ___ Eric F Crist I am so smart, S.M.R.T! Secure Computing Networks -Homer J Simpson ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help Understanding While Loop
On 10/14/2005 3:24 PM David Kirchner wrote: On 10/14/05, Drew Tomlinson [EMAIL PROTECTED] wrote: OK, I've been working on an sh script and I'm almost there. In the script, I created a 'while read' loop that is doing what I want. Now I want to keep track of how many times the loop executes. Thus I included this line between the 'while read' and 'done' statements: count = $(( count + 1 )) I've tested this by adding an 'echo $count' statement in the loop and it increments by one each time the loop runs. However when I attempt to call $count in an 'echo' statement after the 'done', the variable is null. Thus I assume that $count is only local to the loop and I have to export it to make it available outside the loop? What must I do? Oh yeah, that's another side effect of using the while read method. Because it's | while read it's starting a subshell, so any variables are only going to exist there. You'd need to have some sort of 'echo' within the while read, and then | wc -l at the end of the while loop, or something along those lines. The IFS method someone else mentioned, in regards to 'for' loops, would probably be better all around. So you'd want: OLDIFS=$IFS # Note this is a single quote, return, single quote, no spaces IFS=' ' for i in `find etc` do done IFS=$OLDIFS OK, I've tried this and it does fix the count problem. However it messes up another part of the script and I'm trying understand why. I tried to make this script dynamic in that all I would need to do is edit variables set at the top and then not have to worry about all occurrences in the script. Thus I set the following variables: remote_pictures_dir=/multimedia/Pictures local_pictures_dir=/tv/pictures find_args=-iname '*.jpg' -or -iname '*.gif' Then I called the 'find' command as follows: for original in $(/usr/bin/find $remote_pictures_dir $find_args -print) But when I run my script, I get /usr/bin/find: invalid predicate `-iname '*.jpg' -or -iname '*.gif''. However if I don't try and use $find_args and type the arguments in specifically, the script runs fine. I tried various combinations of quoting and escaping those quotes but can't come up with a combination that works. What is going on? And is there some way to set verbosity so I can see how the shell is expanding the variables? Thanks much, Drew -- Visit The Alchemist's Warehouse Magic Tricks, DVDs, Videos, Books, More! http://www.alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Help Understanding While Loop
OK, I've been working on an sh script and I'm almost there. In the script, I created a 'while read' loop that is doing what I want. Now I want to keep track of how many times the loop executes. Thus I included this line between the 'while read' and 'done' statements: count = $(( count + 1 )) I've tested this by adding an 'echo $count' statement in the loop and it increments by one each time the loop runs. However when I attempt to call $count in an 'echo' statement after the 'done', the variable is null. Thus I assume that $count is only local to the loop and I have to export it to make it available outside the loop? What must I do? Thanks for your help, Drew -- Visit The Alchemist's Warehouse Magic Tricks, DVDs, Videos, Books, More! http://www.alchemistswarehouse.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help Understanding While Loop
On Fri, Oct 14, 2005 at 02:48:19PM -0700, Drew Tomlinson wrote: OK, I've been working on an sh script and I'm almost there. In the script, I created a 'while read' loop that is doing what I want. Now I want to keep track of how many times the loop executes. Thus I included this line between the 'while read' and 'done' statements: count = $(( count + 1 )) ^^^ You're missing something here ;) $ count=1 $ echo $count 1 $ count = $(( $count +1 )) # note: 'count =' count: not found $ ^[[A^C $ count=$(( $count + 1 )) # note: 'count=' $ echo $count 2 -- o--{ Will Maier }--o | jabber:[EMAIL PROTECTED] | email:[EMAIL PROTECTED] | | [EMAIL PROTECTED] | [EMAIL PROTECTED] | *--[ BSD Unix: Live Free or Die ]--* ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: Help Understanding While Loop
On 10/14/05, Drew Tomlinson [EMAIL PROTECTED] wrote: OK, I've been working on an sh script and I'm almost there. In the script, I created a 'while read' loop that is doing what I want. Now I want to keep track of how many times the loop executes. Thus I included this line between the 'while read' and 'done' statements: count = $(( count + 1 )) I've tested this by adding an 'echo $count' statement in the loop and it increments by one each time the loop runs. However when I attempt to call $count in an 'echo' statement after the 'done', the variable is null. Thus I assume that $count is only local to the loop and I have to export it to make it available outside the loop? What must I do? Oh yeah, that's another side effect of using the while read method. Because it's | while read it's starting a subshell, so any variables are only going to exist there. You'd need to have some sort of 'echo' within the while read, and then | wc -l at the end of the while loop, or something along those lines. The IFS method someone else mentioned, in regards to 'for' loops, would probably be better all around. So you'd want: OLDIFS=$IFS # Note this is a single quote, return, single quote, no spaces IFS=' ' for i in `find etc` do done IFS=$OLDIFS ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]