how to improve the below space issue coding - unix

while i ran the below script it returns nothing... expect the prompt as the output..
#!/bin/sh
cd /dla
op=`df -k /dla/ |awk '{print $5}' |grep '%' |cut -d '%' -f1`
if[ $op -ge 80 ]
then
echo ' dla is more tha 80% - Purge started'
find /dla/ -name '20[0-9][0-9]*' -type d -print |sort |cut -d '/' -f7 |grep 20 | sort | uniq -c |head -20 > /tmp/file_list.dat
for i in cat /tmp/file_list.dat |awk '{print $#}'
do
find /dla -name $i -type d -exec rm -rf [] \;
newop='df -k /dla/ |awk '{print $5}' |grep '%' |cut -d '%' -f1'
if [$newop -le 80]
then
echo 'dla is 80% -Purge stopped'
exit 0
fi
done
else
echo 'dla is less than 80% - No Purge Required'
fi

Related

Not able to read file content with sed command

I am trying to read the below file line by line to perform the below operations
Extract the name of the file/directory alone and assign it one variable,
Extract the permission available in the line and add comma between the permission. Then assign it to another variable,
At last applying setfacl logic as shown in the output section.
File
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
Code
input="bkp_23.txt"
while IFS= read -r line;
do
echo $line
file_name=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{print $1}'`
echo $file_name
file_perm=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{$1=""}{print}' | tr ' ' ',' | awk
'{sub(",","")}1'`
echo $file_perm
echo "setfacl -m "$file_perm" "$file_name" executing"
done <"$input"
Output
setfacl -m user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x /disk1/script_1/
setfacl -m user::rw-,group::r--,other::r-- /disk1/script_1//hello.txt
setfacl -m user::rwx,group::r-x,other::r-x /disk1/script_1//bkp_10.txt
Error
sed: can't read # file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x: No such file or directory
$ cat input
# file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file: /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file: /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
$ while read _ _ path perms; do perms="$(echo "$perms" | tr -s ' ' ,)"; echo path="$path", perms="$perms"; done < input
path=/disk1/script_1/, perms=user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x
path=/disk1/script_1//hello.txt, perms=user::rw-,group::r--,other::r--
path=/disk1/script_1//bkp_10.txt, perms=user::rwx,group::r-x,other::r-x
Try to echo the line content along with sed logic like this
file_name=$(echo "$line" | sed 's/# file:\(.*\)/\1/g' | awk '{print $1}')
file_perm=$(echo "$line" | sed -e 's/# file:\(.*\)/\1/g' | awk '{$1=""}{print}' | tr ' ' ',' | awk '{sub(",","")}1')

recovering deleted script running in background

I have script which is running in bg(nohup) but it was accidently deleted,but that is continue running now I need to edit the code which is already deleted.
How can I now get that code.I assume somewhere it should be as it is running.
Try this :
#!/bin/bash
if [[ ! $1 || $1 == -h || $1 == --help ]]; then
echo -e "Usage:\n\n\t$0 '[path/]<file name>'"
exit 1
fi
files=(
$(file 2>/dev/null /proc/*/fd/* |
grep "(deleted)'$" |
sed -r 's#(:.*broken\s+symbolic\s+link\s+to\s+.|\(deleted\).$)# #g' |
grep "$1" |
cut -d' ' -f1
)
)
if [[ ${files[#]} ]]; then
for f in ${files[#]}; do
echo "fd $f match... Try to copy this fd to another place quickly!"
done
else
echo >&2 "No matching fd found..."
exit 2
fi
Not tested on non GNU-Linux

unix script run search in parallel

I have a script that searches files for a phrase in a number of different folders, then shows to output.
The trouble is, it does each search sequentially and takes a long time. I would like to make the searches run without waiting for the previous one to finish.
zipped_folders=("/extlogs/archive/rsyslog/folder1/"
"/extlogs/archive/rsyslog/folder2/")
folders=("/extlogs/rsyslog/Folder1/"
"/extlogs/rsyslog/Folder2/")
portal=0
mobile=0
email=0
if [ "$#" -ne 1 ]; then
echo "Incorrect Argument: logcount 201602"
exit 1
fi
for i in "${zipped_folders[#]}"
do
#echo $i"syslog-"$1*".log.gz"
((portal+=$(nohup gunzip -c $i"syslog-"$1*".log.gz" | grep -i "search1" | grep -v "Search1" | wc -l &)))
((mobile+=$(nohup gunzip -c $i"syslog-"$1*".log.gz" | grep -i "Search2" | wc -l &)))
((email+=$(nohup gunzip -c $i"syslog-"$1*".log.gz" | grep -i "search3" | grep -v "ActiveSync" | wc -l &)))
done
for i in "${folders[#]}"
do
((portal+=$(nohup cat $i"syslog-"$1*".log"| grep -i "search4"| grep -v "exsearch4" | wc -l &)))
((mobile+=$(nohup cat $i"syslog-"$1*".log" | grep -i "search5" | wc -l &)))
((email+=$(nohup cat $i"syslog-"$1*".log" | grep -i "search6" | grep -v "ActiveSync" | wc -l &)))
done
echo "Portal: " $portal
echo "Mobile: " $mobile
echo "Email: " $email
exit 1
You can use xargs.
find ${topdir} -name '*.gz' | xargs -n1 -P${PARALLEL_JOBS} -I {} bash -c "/usr/bin/grep 'criteria' {}"

Passing text to variable in KSH. Not Working

Hi I am struggling to solve this simple program. I am not able to pass the value from the text file to the variable.
I am stuck at this: value=$( sed -n "${line}p" rpt1.txt|awk {$3}
O/P:
1.sh[15]: test: argument expected
CODE:
wc `find /arbor/custom/gur/fold1`|grep -vi "total"| tee rpt1.txt
total1=`wc -l rpt1.txt`
wc `find /arbor/custom/gur/fold2`|grep -vi "total"| tee rpt2.txt
total2=`wc -l rpt2.txt`
line=1
if [ $line -le $total1 ]
then
value=$( sed -n "${line}p" rpt1.txt|awk {$3} )
if [ $value -eq 512 ];
then
sed -n "${line}p" rpt1.txt|awk '{print $4}'| tee direc.txt
fi
line =$line+1
else
echo "loop over"
fi
Shouldn't there be a print in front of $3 in the suspect line?

Error while running command from cron (tcsh)

When I'm running this command from shell(tcsh), it executes perfectly-
cal | tail -6 | sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' | tr -s '[:blank:]' '\n' | head -21 | tail -20 | tr -s '\n' ' ' | grep -w `date "+%e"` ; /usr/bin/bash -lc "if [ "$?" == 0 ] ; then echo xyz ; fi"
But when I put the exact same thing in a crontab, I get this error mail from my machine-
Subject: Output from "cron" command
Content-Length: 244
Your "cron" job on uatserver
cal | tail -6 | sed -e 's/^.\{3\}//' -e 's/.\{3\}$//' | tr -s '[:blank:]' '\n' | head -21 | tail -20 | tr -s '\n' ' ' | grep -w `date "+
produced the following output:
Usage: grep -hblcnsviw pattern file . . .
I'm sure that even my crontab commands are executed using tcsh as it is set to be the default.
p.s- My machine:
SunOS uatserver 5.10 Generic_127112-11 i86pc i386 i86pc
Your problem is that the PATH variable is not the same. Solaris has different flavors of grep
examples:
/usr/bin/grep
/usr/xpg4/bin/grep
You crontab ran /usr/bin/grep instead of /usr/xpg4/bin/grep. The two versions of grep have some different options.

Resources