nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

new scripting problem, cat | foreach

This is a discussion on new scripting problem, cat | foreach within the Shell scripting forums, part of the Development/Scripting category; I've found another way to list my dirs and wants to check each dir with the du -khs command. I ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 03-15-2007, 09:26 PM
Junior Member
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
cybergurra
Default new scripting problem, cat | foreach

I've found another way to list my dirs and wants to check each dir with the du -khs command.
I have all the directories in a file and wants to read the file and the perform the task for each of the dirs in it.

the file consists of rows with dir names followed by /

can I cat the file in my script and the perform a foreach loop on them? any suggestions?
The output should be one logfile for each of the dirs
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-16-2007, 02:46 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Since you have not specified sample input file; I’m going to assume that your file is as follows:

input.txt
Code:
/tmp
/home/vivek
/home/foo
/home/bar
/etc
Shell script
Code:
#!/bin/bash
INPUT="input.txt"
LOG="/tmp/log.txt"
>$LOG #init log file
for f in $(cat $INPUT); do du -ksh $f >>$LOG; done
echo "See log file $LOG"
Let me know if you need any further help..
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 03-16-2007, 02:21 PM
Junior Member
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
cybergurra
Default done but not quite finished.. :)

I've tried some different syntaxes and managed to perform the task like this:

Code:
for i in $(ls -p); do du -khs echo $i; echo $i; done > temp1
cat temp1 | grep ^[0-9] | grep /$ > temp2
tihis little loop gives an output like this:

Code:
5.6M    bin/
6.7M    boot/
168K    dev/
60M     etc/
597M    home/
8.0K    initrd/
89M     lib/
16K     lost+found/
16K     media/
8.0K    misc/
8.0K    mnt/
8.0K    opt/
899M    proc/
143M    root/
16M     sbin/
0       selinux/
8.0K    srv/
0       sys/
576K    tmp/
2.4G    usr/
138M    var/
but it also gives me the error message:

Code:
du: cannot access `echo': No such file or directory
it might not be a problem since the output is good, but it looks bad...
the next problem is that i want to specify a directory to go further down in and perform the same task, lets say i want to check the content of the home/ directories, how can i do this?
My home directory catalogue structure is like this..

Code:
home/
         group1/
                    user/
                    user1/
                    user2/
         group2/
                    user/
                    user1/
                    user2/
I want the logfile to tell me how much each group dir contains, number of directories and size..
can you give me som hints?
Reply With Quote
  #4 (permalink)  
Old 03-16-2007, 04:17 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
I want the logfile to tell me how much each group dir contains, number of directories and size..
can you give me som hints?
Use for command to get list of dirs:
Code:
LIST= $(find /home -type d)
And run command
Code:
for d in $LIST; do du -ksh $d>>/tmp/list.txt; done
View file
Code:
less /tmp/list.txt
Hope this helps!
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 03-16-2007, 04:57 PM
Junior Member
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
cybergurra
Default

Thanks a lot!
I will mix a bit with it and see if i can get it to work smoothly, it seams realy nice!

I will write again when it is finnished or when I get stuck again!
Reply With Quote
  #6 (permalink)  
Old 03-16-2007, 08:00 PM
Junior Member
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
cybergurra
Default still some problems

i just cant get it to work the way i want..

this is what it looks like at the moment

Code:
home/
         group1
                  backup
                             users///
         group2
                   backup
                             users///
I realized that the backup catalogue was forced to be there (it is consistent), the big issue is that I need to check the size of the user-catalogues and this is tricky since I want the script to be dynamic and nog bound to the names of the groups.

I happy that I went to the hairdresser the other day, otherwise I would be tearing my hair of!
Reply With Quote
Reply

Bookmarks


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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
perl scripting pansarevai Coding in General 2 03-17-2008 06:34 PM
new to scripting poone1 Shell scripting 0 01-15-2008 02:19 AM
need help on shell scripting rahul_sayz Shell scripting 1 12-08-2007 11:37 AM
Books on Shell-Scripting shankar100 Shell scripting 3 02-16-2007 02:19 PM
scripting ganes Shell scripting 2 09-06-2005 01:04 PM


All times are GMT +5.5. The time now is 07:28 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36