Register
It is currently Sat Oct 25, 2014 8:30 am

howto simplify this


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Apr 15, 2007 3:21 am   

Joined: Sun Apr 15, 2007 3:04 am
Posts: 1
this is a nice way to do something with files in directory...

Code:
for i in $( ls  /root/Desktop );
     do

     done

but the problem is when files have spaces in their names, then variable "i" becomes, first the first part of the name of the file(before space) and the second part(after space), i wrote few more lines to avoid that.
Code:
for i in $( ls  /root/Desktop );
     do
direktorij1=/root/Desktop/$i
echo $direktorij1
echo $direktorij2
if [ -d "$direktorij1" ];
then pitanje $direktorij1
elif [ -e "$direktorij1" ];
then echo aa
else
   if [ -e "$direktorij2" ];
   then direktorij2=$direktorij1;

   else   direktorij2="$direktorij2 $i"
echo $direktorij2
      if [ -d "$direktorij2" ];
      then pitanje "$direktorij2"
      fi
   fi
fi

     done


Note that it works only for folders because i need to call function "pitanje" for every folder in a folder /root/Desktop (/root/Desktop is just an example)
So the question is can all this be avoided by editing "for loop" or simplified the other way.


Top
 Profile  
 PostPosted: Mon Apr 16, 2007 1:49 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
well... first of all... what are you looking to do?... get a list of items in the directory?
second... if you quote the variable, spaces won't be split up.

i would use the following to give an example...
Code:
for i in /root/Desktop/*
do
    echo "$i"
done


notice i didn't use ls
it's not really needed because bash is so smart.

if you are wanting to do something recursively (go down the directory tree), i would suggest find, especially if you are just targeting folders.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP