Problem with recursion in subdirectories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with recursion in subdirectories
# 1  
Old 03-11-2007
Problem with recursion in subdirectories

Hello !
I need some help with my simple bash script.
This script removes all files ( with name given in $1 ) in current dir and subdirectories .
The problem is with first loop in the script ( for file in * ; do ) .
When I run the sript in my home directory this script display sometimes(
probably special directories ) strange massage like :
" [: 31 : (d.) .png : undexpected operator " .
But When I write this loop like this : " for file in $( ls $dir ) " the sript
working perfectly ( but slow) .
How to remove the strange message ?

Paweł Łepko

Last edited by scotty_123; 03-11-2007 at 06:53 AM.. Reason: to hide code
# 2  
Old 03-11-2007
Welcome to the forum! Smilie Smilie Smilie

To remove strange errors as you had mentioned
the script could be run as,
Code:
./script.sh 2>/dev/null

Well, that is not advisable,
could you please run the script with debug mode
Code:
set -x

to see why these errors do really occur !!!
( probably seems like missing parameters to test )

If the errors are fixed, then there is no need to run it as 2>/dev/null

Smilie
# 3  
Old 03-11-2007
Debuging

I run the script in debug mode ( set -x ) and i found the problem - the problem is
probably with "=" operator .

When I try to remove file photo.png

In debug moge I get message like this:
...
+ [ Expirience ubuntu.ogg = photo.png ]
[ : 1 : ubuntu.ogg : Undexpected operator ]
..
# 4  
Old 03-11-2007
I cant remove the problem

I don't know how to remove this problem ?
# 5  
Old 03-11-2007
Code:
[ Expirience ubuntu.ogg = photo.png ]

Expirience ubuntu.ogg is a single filename with space in between. Use quotes to avoid this problem
Code:
if [ "$file" = "$2" ] && [ -f "$file" ] ; then #

# 6  
Old 03-11-2007
Yes Yes Yes

Thanks for help!

Paweł Łepko
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in concatenating multiple files from subdirectories

I want to concatenate multiple files recursively from sub-directories intoone file in Linux. I saved the following script as script.sh in $HOME/testing1 where I have several subdirectories and .txt files into them. I ran script.sh from the command prompt of $HOME/testing1 as ./script.sh. But it... (3 Replies)
Discussion started by: raj284
3 Replies

2. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

3. Shell Programming and Scripting

recursion script problem

Hi Guys,, I tried to create a recursive function in unix. The following is the code. #/bin/sh function(){ n=$1; if ; then out=1; echo "inside if for 0"; else out = `$n * function "$n-1"`; echo "inside if for $n-1; fi (3 Replies)
Discussion started by: mac4rfree
3 Replies

4. UNIX for Dummies Questions & Answers

Function Recursion Shift Problem

Hi, I'm trying to create a script that will display the contents of the users directories, but i'm confused about how to incorporate the shift properly. The problem I'm getting with my script is that it goes throught the first couple of directories but then returns an error as it loses the... (10 Replies)
Discussion started by: nuvpal
10 Replies

5. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

6. Shell Programming and Scripting

KSH: recursion into subdirectories?

Hello, I'm scripting a newbie. I'm using KSH on HP-UX. I'm trying to write a script that will change a whole directory of file names into UPPER CASE. I have the "convert to upper case" part of it working fine: ls | while read filename; do typeset -u uppercase uppercase=${filename} ... (2 Replies)
Discussion started by: Wotan31
2 Replies

7. Shell Programming and Scripting

Problem with changing directory and subdirectories to read only

I have a directory with its subdirectories and files. I want to change them all to read only. Say it is ~/test chmod -R 444 ~/test chmod: `/home/myname/test': permission denied I do not understand. Do I have to have executable mode for a diirectory to access. How can I change ~/test to... (5 Replies)
Discussion started by: lalelle
5 Replies

8. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

9. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies
Login or Register to Ask a Question