Using a loop with cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using a loop with cut
# 1  
Old 12-01-2010
Using a loop with cut

I don't know if I described this right, but I am new to scripting and this is giving me a little bit of trouble, but I will explain what I am trying to do.

Each time this is run, I want it to grab and save ls -l /home to data.txt.
ls -l /home > data.txt

Now the part I am getting confused with is, I need to check each users total disk usage, so I am using du -skh. Now I don't understand how to use this in loop, so it goes through the text file, reads one user at a time, and outputs it to either a text file, or just to the shell. I have tried a few different loops and I just can't get it to work, and so far all i'v gotten for it is:

ls -l /home > data.txt
cut -f3 -d" " data.txt

Scripting is hard ^^. So any help is appreciated.
# 2  
Old 12-01-2010
I think your looking for something like this.

Code:
ls -ld /home/*|while read a;do du -sk $a;done

# 3  
Old 12-01-2010
or simply
Code:
du -skh /home/*

Smilie
# 4  
Old 12-01-2010
ok so du -skh /home/* works better than what I was using, but the part of it i'm not understanding is how to implement it into a loop. The two lines I posted were to copy the user names for the system into a text file. Now I need to figure out how to use a loop to go through that text file, cutting out each user name, and displaying the storage use for each user.

If it helps, I can copy and paste the question I am trying to do. I printed a bunch of tutorial things offline to try to learn from.

Code:
Take the following scenario when you are the system administrator to purchase a larger disk drive for your Linux server. You receive a phone call from an angry user, who claims that they are unable to save the files. To solve this problem quickly, you must check which user had taken most of the disk space and then make a decision what to do.  

Display the Device name, used space for each disk in kilo bytes and also the total available disk space in GB with meaningful headings sorted by Capacity. 

Display the usage in kilo bytes sorted in descending order (highest as the first line) for the first 10 users. (Test in Ubantu only as opentech does not allow you to access the other users. Create 3 or 4 users in Ubantu and 4 to 5 files in each user’s directory). 

Ask the user to enter the size of the used space in Kilobytes. List all users whose used space is  more than the size entered by the user as:
Login Name    User Full name		Used space

Login Name is the directory name itself. User Full name can be obtained from /etc/passwd command.

I'v spent a couple days on this already, and it's making me wannna go crazy

Last edited by ninjafish; 12-01-2010 at 12:44 PM..
# 5  
Old 12-16-2010
Hope its not too late :

Code:
du -skh /home/* > /tmp/usr_usage.tmp
echo " Login name  User Full Name  Used Space" 
while read line 
do 
Usage=`echo $line | awk '{print $1}'`
User=`echo $line | awk '{print $2}'| cut -d\/ -f3`
rlname=`grep $User /etc/passwd | cut -d: -f5`
echo "$User     $rlname     $Usage"
done </tmp/usr_usage.tmp
rm /tmp/usr_usage.tmp

But it looks like an assignment..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

3. Shell Programming and Scripting

Cut command not working in for loop

grep -Fxvf testdata.xls file_GTDA1.xls >file_GTDA.xls SLS_COUNT=`grep 'GTDA_Dly_Sls' file_GTDA.xls |wc -l` PMIX_COUNT=`grep 'GTDA_Dly_Pmix' file_GTDA.xls |wc -l` if ; then var1=`cat file_GTDA.xls|grep 'GTDA_Dly_Sls_'` var4="|" for i in $var1... (7 Replies)
Discussion started by: renuk
7 Replies

4. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

5. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

6. Shell Programming and Scripting

cut -f1 -d"," to a array and loop

############## SOLVE ############# Hi I would like to do something quite simple but i could not figure it out Example file: BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CAD MASTER SWAP BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast Zero,CHF SWAP GOTTEX BAS,Fast ZeroSPEC,Fast ZeroSPEC,Fast... (10 Replies)
Discussion started by: kykyboss023
10 Replies

7. Shell Programming and Scripting

Problem in getting data from a loop using grep and cut

The script is following : for each_rec in <file_name> do count=`cut -c -2 ${each_rec} | grep "45"` echo ${count} if ] then amount=`cut -c 24-35 ${each_rec}` echo ${amount} else echo "failed" fi done And the file looks like below : ... (4 Replies)
Discussion started by: mady135
4 Replies

8. UNIX for Dummies Questions & Answers

Cut not working in a loop

I have a function "MyPrint" that runs great on a file (BaseData.txt) that has one line of data. If i add rows to the text file it's reading the tFile variable becomes a list of every field 2 in the file. To correct this, i tried to call the function from a loop where i read one line at a time and... (4 Replies)
Discussion started by: KME
4 Replies

9. Shell Programming and Scripting

Use loop var i within Cut Command

Hi, In the following bash code rather than cutting at a predefined character I would like to cut at position i (i var from loop). Is this possible? I have tried eval, but either it's not possible or my syntax is wrong. thanks Nick for i in {1..9} do theChar=$(echo... (3 Replies)
Discussion started by: de_la_espada
3 Replies
Login or Register to Ask a Question