Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2012, 9:58 AM   #1
Ali3nSVK
Newbie
 
Join Date: Mar 2012
Posts: 2
Rep Power: 0 Ali3nSVK is on a distinguished road
Searching Directory Tree

Hello. I've written a script that searches a directory tree and performs certain tests on all files and folders of any subfolder. The script itself works, but it always creates an empty file with name "0" at the root of the directory tree. What could be the cause of that ? Since I'm fairly new to bash, I'd appreciate any help. Thank you in advance. Here is the code:

cd $TEST_DIR
for tdir in $(find -type d | sort); do
  cd "$tdir"
  isdir=0; isfile=0;
  for cdir in $(ls -1); do
    if [ -d "$cdir" ]; then ((isdir++)); fi
    if [ -f "$cdir" ]; then ((isfile++)); fi
  done
  for i in $(seq $(echo "$tdir" | grep -o "/" | wc -l | sed s/\ //g)); do
    cd ..
  done
  if [[ "$isdir" -gt 0 && "$isfile" -gt 0 ]]; then 
    printf "%s: directories and other files mixed in: %s\n" "$0" "$tdir"
  fi
  done

TEST_DIR is the directory tree supplied in script arguments.
Ali3nSVK is offline   Reply With Quote
Old Mar 15th, 2012, 1:39 PM   #2
watael
Hobbyist Programmer
 
Join Date: Nov 2011
Posts: 155
Rep Power: 3 watael is on a distinguished road
Re: Searching Directory Tree

hi,

don't use the form `for i in $(command); do', it'll loop on each word of command output.
don't use `for i in $(find); do', instead use pathname expansion; and `find' has a very useful feature called `-exec'.
find $test_dir -type d -exec /bin/bash -c '
   for f in "$1"/*
   do [ -d "$f" ] && ((isdir++))
      [ -f "$f" ] && ((isfile++))
   done
   (( isdir && isfile )) && printf "%s: directories and other files mixed in: %s\n" "$0" "$1"
' na {} \; | sort
watael is offline   Reply With Quote
Old Mar 16th, 2012, 10:40 AM   #3
Ali3nSVK
Newbie
 
Join Date: Mar 2012
Posts: 2
Rep Power: 0 Ali3nSVK is on a distinguished road
Re: Searching Directory Tree

Thank you. That seems to have solved the problem and looks cleaner. This time I got stuck on a different problem. I need to check contents of several files and verify they only contain whole decadic number with multiple digits terminated with newline character 0x0A.

For example if I execute the following:
printf "1234\n" >test-file

The condition will be met. But it won't be in these cases.

printf "1234" >test-file
printf "1234\n\n" >test-file
printf "\n" >test-file

I tried to store the contents of something like this into variable.
var=$(cat test-file | grep -E "[^0-9]^\n")
And then check if the variable is empty, but that doesn't work as intended and I don't know how to put the right regular expression together. How would I go about accomplishig that ?

Thanks again in advance.
Ali3nSVK is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help fixing Binary Search Tree java code fistpunch Java 3 Dec 6th, 2010 9:16 AM
Tree in C Apprentice123 C++ 9 May 9th, 2010 7:45 PM
Tree, Infix notation, Mathematical Expressions S.I C 11 May 7th, 2010 4:05 PM
display and searching through the contents of a windows directory cwl157 C++ 2 May 28th, 2007 9:59 PM
BinaryTree , I need help on inserting expression to the Tree Master C# 3 Oct 14th, 2005 3:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC