Shell script to loop and store in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to loop and store in array
# 1  
Old 08-15-2017
Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck.

Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification.

Code:
du -sh * | sort -rh

139G     Dir_1
84G     Dir_2
15G     Dir_3

ls -l Dir_1

drwx------ 2 User1 Group1  4096 Aug 15 16:38 Dir_1

The script should resolve for Dir_1 and Dir_2 as the size is > 50GB and send a notification to User1 and User2 indicating that the directores have exceed the limit.

Apprecaiet any help.

---------- Post updated at 05:54 PM ---------- Previous update was at 04:48 PM ----------

I tried this.
Code:
#!/bin/ksh
dir_name=/apps/directory_name

cd $dir_name

du_cmd=`du -sh * | sort -rh`

for f in $du_cmd
do
echo $f
done

Output:
Code:
138G
Dir_1
76G
Dir_2
15G
Dir_3
1.7G

--------
I added bit more functionality

Code:
#!/bin/ksh
dir_name=/apps/directory_name

cd $dir_name

du_cmd=`du -sh * | sort -rh`

for f in $du_cmd
do
echo $f

echo `ls -ld $dir_name/$f | awk '{print$3}'`

done

Output:
Code:
156G
ls: cannot access /apps/directory_name/156G: No such file or directory

Dir_1
User_1
76G
ls: cannot access /apps/directory_name/76G: No such file or directory

Dir_2
User_2
15G
ls: cannot access /apps/directory_name/15G: No such file or directory

Dir_3
User_3
1.7G
ls: cannot access /apps/directory_name/1.7G: No such file or directory


Please help.



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-16-2017 at 04:35 AM.. Reason: Added & changed ICODE to CODE tags.
# 2  
Old 08-16-2017
Not tested..

this script will check the directory size is in GB and if its more than 50GB then it get the corresponding userid in for loop. implement your notification method in for loop

Code:
#!/bin/bash

dir_name=/apps/directory_name
cd ${dir_name}

du -sh * | awk '$1~/G/ && $1+0>50' | while read SIZE DIR_NAME
do
	USER_ID=$(ls -ld ${DIR_NAME} | awk '{print $3}')
	echo "Send Notification to ${USER_ID}"
	# Put your mail command here
done

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-16-2017
Quote:
Originally Posted by itkamaraj
Not tested..

this script will check the directory size is in GB and if its more than 50GB then it get the corresponding userid in for loop. implement your notification method in for loop

Code:
#!/bin/bash

dir_name=/apps/directory_name
cd ${dir_name}

du -sh * | awk '$1~/G/ && $1+0>50' | while read SIZE DIR_NAME
do
	USER_ID=$(ls -ld ${DIR_NAME} | awk '{print $3}')
	echo "Send Notification to ${USER_ID}"
	# Put your mail command here
done

That might work, but it will only find directories that contain 51 through 999 Gb. It would not report on directories that have crossed over the 1 Tb threshold.

Using human readable numbers (i.e. du -h) when looking for numbers is frequently more trouble that it is worth. You might want to use:
Code:
du -sk * | awk '$1+0>50*1024*1024' | ...

instead.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-16-2017
Thanks for the inpurts - I really appreciaet it. I will try and keep you posted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to print python array in shell script loop.

I am unable to loop print a python string array in my unix shell script: ~/readarr.sh '{{ myarr }}' more readarr.sh echo "Parameter 1:"$1 MYARRAY= $1 IFS= MYARRAY=`python <<< "print ' '.join($MYARRAY)"` for a in "$MYARRAY"; do echo "Printing Array: $a" done Can you... (10 Replies)
Discussion started by: mohtashims
10 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

4. Shell Programming and Scripting

Store values from a file into an array variable in Shell

Dear All, I have been trying to do a simple task of extracting 2 fields from the file (3 rows) and store it in an array variable. I tried with: #! /bin/bash ch=`cut -f10 tmp.txt` counter=0 for p in $pid do c=${ch} echo "$c ..$counter" counter=$((counter+1))... (2 Replies)
Discussion started by: ezhil01
2 Replies

5. Shell Programming and Scripting

Script to store the variable in a table or array.

Hi, I have few variable say 10 ex:- l_pc_291334_01_0_01_00.cmp l_pc_441133_50_0_02_00.cmp l_pc_441133_55_0_02_00.cmp Each variable value is coming via loop on a table. I want to create a script that stores these value to a table or array ( But one by one not all at one time as... (4 Replies)
Discussion started by: amitkumar.b2
4 Replies

6. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

7. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

8. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

9. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies

10. UNIX for Advanced & Expert Users

how do I store the values in array using shell

Hi, Is is possible to get the value using shell script? x=1 y1 = 10 y2 = 15 y3 = 7 echo $y$x is giving y1 (variable name) but I need the value of y1 (i.e. 10 dynamically) Is there any solution? if so, please mail me at kkodava@maxis.com.my ... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question