2016-05-23

dev
unknown 2016-05-23 08:32:00 +08:00
parent b3f56ab59f
commit 4b6e1524e6
38 changed files with 1103 additions and 0 deletions

View File

@ -0,0 +1,12 @@
#!/bin/bash
function myprint()
{
printf "%-16s - %s", $1, $4
}
function myrand(limit)
{
return int(limit * rand())
}

View File

@ -0,0 +1,6 @@
#!/bin/bash
BEGIN{FS="\n"; RS=""}
{
myprint()
}

8
gawk进阶/new 100644
View File

@ -0,0 +1,8 @@
#!/bin/bash
for (( i=1; i<=4; i++ ))
do
touch $i.sh
chmod 764 $i.sh
echo "#!/bin/bash" > $i.sh
done

View File

@ -0,0 +1,2 @@
BEGIN{FS=","; print n}
{print $n}

4
gawk进阶/test 100644
View File

@ -0,0 +1,4 @@
10
5
123
50

View File

@ -0,0 +1,42 @@
#!/bin/bash
#使用内建变量
gawk 'BEGIN {testing="This is a test"; print testing; testing=45; print testing}'
#处理数字值
gawk 'BEGIN{x=4; x= x*2+3; printx}'
#处理数组
gawk 'BEGIN{capital["Ill"] = "SprintField"; print capital["Ill"]}'
#遍历数组变量
gawk 'BEGIN{
var["a"] = 1
var["g"] = 2
var["m"] = 3
for( test in var)
{
print "Index:",test,"- Value:",var[test]
}
}'
print "------"
#删除数组变量
gawk 'BEGIN{
var["a"] = 1
var["g"] = 2
for (test in var)
{
print "Index:",test," - Value:", var[test]
}
delete var["g"]
print "----"
for (test in var)
{
print "Index;",test," - Value:", var[test]
}
}'

View File

@ -0,0 +1,57 @@
#!/bin/bash
#正则表达式
gawk 'BEGIN{FS=","}
/11/{print $1}
' test
#if-else语句
gawk '{
if($1 > 20)
{
x=$1*20
print x
}
else
{
x=$1/2
print x
}
}' test
#while 语句
gawk '{
total = 0
i=1
while(i<4)
{
total+=$i
i++
}
avg = total/3
print "Average:".avg
}' test
#do-while语句
gawk '{
total=0
i=1
do
{
total += $i
i++
}while(total < 150)
print total }' test
#for语句
gawk '{
total = 0
for (i=1; i<4; i++)
{
total+=$i
}
avg = total/3
print "Average:".avg
}' test

View File

@ -0,0 +1,12 @@
#!/bin/bash
#gawk 自定义函数
gawk '
function myprint()
{
printf "%-16s - %s\n", $1, $4
}
BEGIN{FS="\n"; RS=""}
{
myprint()
}' test

View File

@ -0,0 +1,5 @@
#!/bin/bash
#使用函数库和gawk脚本
gawk -f gawk函数库 -f gawk脚本 test

View File

@ -0,0 +1,21 @@
#!/bin/bash
# send data to the the table in the MYSQL database
MYSQL=`which mysql`
if [ $# -ne 2 ]
then
echo "Usage:mtest2 emplid lastname firstname salary"
else
#脚本变量一定要用双引号,字符串变量使用单引号
statement=" insert into em_admin values(NULL, '$1', $2)"
$MYSQL emwjs -u test <<EOF
$statement
EOF
if [ $? -eq 0 ]
then
echo Data successfully added
else
echo Problem adding data
fi
fi

View File

@ -0,0 +1,18 @@
#!/bin/bash
#redirecting SQL output to a variable
MYSQL=`which mysql`
dbs=`$MYSQL emwjs -u test -Bse 'show tables;'`
for db in $dbs
do
echo $db
done
#使用xml输出数据
$MYSQL emwjs -u test -X -e 'select * from em_admin'
#使用table标签输出数据
$MYSQL emwjs -u test -H -e 'select * from em_admin'

View File

@ -0,0 +1,13 @@
#!/bin/bash
#连接数据库
mysql=`which mysql
`
#发送单个命令
$mysql emwjs -u test -e "show databases;"
#发送多个命令
$mysql emwjs -u test <<EOF
show tables;
select * from em_admin;
EOF

View File

@ -0,0 +1,16 @@
#!/bin/bash
#h将模式空间保存到保持空间
#H将模式空间附加到保持空间
#g将保持空间复制到模式空间
#G将保持空间保存到模式空间
#x交换模式空间和保持空间的内容
sed -n '/first/{
h
p
n
p
g
p
}' test

View File

@ -0,0 +1,21 @@
#!/bin/bash
#多个空格只保留一个
#sed '/./,/^$/!d' test
#删除开头的空白行
#sed '/./,$!d' test
#删除结尾的空白行
sed '{
:start
/^\n*$/{$d; N; b start}
}' test
#删除html标签
#有问题
#s/<.*>//g
#sed 's/<[^>]*>//g' test1
#sed 's/<[^>]*>//g;/^$/d' test1

View File

@ -0,0 +1,8 @@
#!/bin/bash
# shell wrapper for sed editor script to reverse lines
sed -n '{
1!G
h
$p
}' $1

View File

@ -0,0 +1,8 @@
!/bin/bash
#排除命令,使本来起作用的命令不起作用
sed -n '/heade/!p' test
#反转文本文件
sed -n '{1!G ; h; $p}' test

View File

@ -0,0 +1,11 @@
#!/bin/bash
#and符号代表替换命令中的匹配模式不管预定义模式是什么文本都可以用and符号替换and符号会提取匹配替换命令中指定替换模式中的所有字符串
echo "The cat sleeps in his hat" | sed 's/.at/"&"/g'
#替换单独的单词
echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 user/'
#在长数字中插入逗号
echo "1234567" | sed '{:start; s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/; t start}'

View File

@ -0,0 +1,4 @@
#!/bin/bash
#测试如果测试成功如果没有标签sed会跳转到结尾如果有标签就跳转到标签如果测试失败则不会跳转
sed -n '{s/first/matched/; t; s/This is the/No match on/}' test

View File

@ -0,0 +1,3 @@
#!/bin/bash
sed '=' test | sed 'N; s/\n/ /'

View File

@ -0,0 +1,7 @@
#!/bin/bash
#跳转到指定脚本
sed '{/first/b jump1; s/This is the/No jump on/; :jump1; s/This is the/Jump here on/}' test
#跳转到开头,删除每一个逗号,并保证删除最后一个逗号之后,跳出循环
sed -n '{:start; s/,//1p; /,/b start}' test

View File

@ -0,0 +1,10 @@
#!/bin/bash
#输出末尾10行数据
sed '{
:start
$q
N
11,$D
b start
}' /etc/passwd

View File

@ -0,0 +1,21 @@
#!/bin/bash
# add commas to numbers in factorial answer
factorial=1
counter=1
number=$1
while [ $counter -le $number ]
do
factorial=$[ $factorial * $counter ]
counter=$[ $counter + 1 ]
done
result=`echo $factorial | sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t start
}'`
echo "The result is $result"

View File

@ -0,0 +1,24 @@
#!/bin/bash
#
# Capture_Stats - Gather System Performance Statistics
#
#########################################################
#
# Set Script Variables
#
REPORT_FILE=/home/tiandi/Documents/capstats.csv
DATE=`date +%m/%d/%y`
TIME=`date +%k:%M:%S`
#
############################################################
#
USERS=`uptime | sed 's/user.*$//' | gawk '{print $NF}'`
LOAD=`uptime | gawk '{print $NF}'`
#
FREE=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4}'`
IDLE=`vmstat 1 2 | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $15}'`
#
##########################################
#
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $REPORT_FILE
#

View File

@ -0,0 +1,3 @@
#!/bin/bash
#
uptime | sed 's/user.*$//' | gawk '{print $NF}'

View File

@ -0,0 +1,4 @@
#!/bin/bash
#查看僵尸进程
ps -al | gawk '{print $2,$4}' | grep Z

View File

@ -0,0 +1,4 @@
#!/bin/bash
#查看内存使用百分比
free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/'

View File

@ -0,0 +1,4 @@
#!/bin/bash
#查看磁盘实用百分比
df -h /dev/sda1 | sed -n '/% \//p' | gawk '{ print $5 }'

View File

@ -0,0 +1,45 @@
#!/bin/bash
#
# Report_Stats - Generates Rpt from Captured Perf Stats
#
############################################################
#
# Set Script Variables
#
REPORT_FILE=/home/tiandi/Documents/capstats.csv
TEMP_FILE=/home/tiandi/Documents/capstats.html
#
DATE=`date +%m/%d/%y`
#
MAIL=`which mutt`
MAIL_TO=tiandi
#
###############################################################3
#
# Create Report Header
#
echo "<html><body><h2>Report for $DATE</h2>" > $TEMP_FILE
echo "<table border=\"1\">" >> $TEMP_FILE
echo "<tr><td>Date</td><td>Time</td><td>Users</td>" >> $TEMP_FILE
echo "<td>Load</td><td>Free Memory</td><td>%CPU Idle</td></tr>" >> $TEMP_FILE
#
###############################################################
#
# Place Performance Stats in Report
#
cat $REPORT_FILE | gawk -F, '{
printf "<tr><td>%s</td><td>%s</td><td>%s</td>", $1, $2, $3;
printf "<td>%s</td><td>%s</td><td>%s</td>\n</tr>\n", $4, $5, $6;
}' >> $TEMP_FILE
#
echo "</table></body></html>" >> $TEMP_FILE
#
################################################################
#
# Mail Performance Report & Clean up
#
#$MAIL -a $TEMP_FILE -s "Performance Report $DATE"
#-- $MAIL_TO < /dev/null
#
#rm -r $TEMP_FILE
#

View File

@ -0,0 +1,92 @@
#!/bin/bash
#
# Snapshot_Stats - produces a report for system stats
#
##############################################
#
# Set Script Variables
#
DATE=`date +%m%d%y`
DISKS_TO_MONITOR="/dev/sda1"
MAIL=`which mutt`
MAIL_TO=tiandi
REPORT=/home/tiandi/Documents/Snapshot_Stats_$DATE.rpt
#
####################################################
#
# Create Report File
#
exec 3>&1 # Save file descriptor
#
exec 1> $REPORT # direct output to rpt file
#
###################################################
#
echo
echo -e "\t\tDaily System Report"
echo
#
###################################################
# Date Stamp the Report
#
echo -e "Today is" `date +%m%d%y`
echo
#
##################################################
#
#1) Gather System Uptime Statistics
#
echo -e "System has been \c"
uptime | sed -n '/,/s/,/ /gp' | gawk '{if($4 == "days" || $4 == "day") {print $2,$3,$4,$5} else {print $2,$3}}'
#
#################################################
#
#2) Gather Disk Usage Statistics
#
echo
for DISK in $DISK_TO_MONITOR # loop to check disk space
do
echo -e "$DISK usage: \c"
df -h $DISK | sed -n '/% \//p' | gawk '{ print $5 }'
done
#
##################################################################
#
#3) Gather Memory Usage Statstics
#
echo
echo -e "Memory Usage: \c"
#
free | sed -n '2p' | gawk 'x = int(($3 / $2) * 100) {print x}' | sed 's/$/%/'
#
###############################################################
#
#4) Gather Number of Zombie Processes
#
echo
ZOMBIE_CHECK=`ps -al | gawk '{print $2,$4}' | grep Z`
#
if [ "$ZOMBIE_CHECK" = "" ]
then
echo "No Zombie Process on System at this Time"
else
echo "Current System Zombie Processes"
ps -al | gawk '{print $2,$4}' | grep Z
fi
echo
#
#####################################################################
#
# Restore File Descriptor & Mail Report
#
exec 1>&3 # Restore output to STDOUT
#
#$MAIL -a $REPORT -s "System Sstatistics Report for $DATE"
#-- $MAIL_TO < /dev/null
#
###############################################################
#
# Clean up
#
#rm -f $REPORT
#

View File

@ -0,0 +1,82 @@
#!/bin/bash
#
# Update_Problem - updates problem record in database
#
############################################################
#
# Determine sql location & set variable
#
MYSQL=`which mysql`" Problem_Trek -u root"
#
##############################################################
#
# Obtain Record Id
#
if [ $# -eq 0 ] # Check if id number was passed
then # If not passed ask for it
#
# Check if any unfinished records exist.
RECORDS_EXIST=`$MYSQL -Bse 'SELECT id_number FROM problem_logger where fixed_date="0000-00-00" OR prob_solutions=""'`
#
if [ "$RECORDS_EXIST" != "" ]
then
echo
echo "The following record(s) need updating..."
$MYSQL <<EOF
SELECT id_number, report_date, prob_symptoms FROM problem_logger WHERE fixed_date="0000-00-00" OR prob_solutions=""\G
EOF
fi
#
echo
echo "What is the ID number for the"
echo -e "problem you want to update?: \c"
read ANSWER
ID_NUMBER=$ANSWER
else
ID_NUMBER=$1
fi
#
##########################################################
#
# Obtain Solution (aka Fixed) Date
#
echo
echo -e "Was Problem solved today? (y/n) \c"
read ANSWER
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
#
FIXED_DATE=`date +%Y%m%d`
;;
*)
# if answer is anything but "yes", ask for date
echo
echo -e "What was the date of resolution? [YYYYMMDD] \c"
read ANSWER
#
FIXED_DATE=$ANSWER
;;
esac
#
########################################################
#
# Acquire problem solution
#
echo
echo -e "Birefly describe the problem solution: \c"
#
read ANSWER
PROB_SOLUTIONS=$ANSWER
#
########################################################
#
# Update problem record
#
echo
echo "Problem record updated as follows:"
echo
$MYSQL <<EOF
UPDATE problem_logger SET prob_solutions="$PROB_SOLUTIONS", fixed_date=$FIXED_DATE where id_number=$ID_NUMBER;
SELECT * FROM problem_logger WHERE id_number=$ID_NUMBER\G
EOF

View File

@ -0,0 +1,8 @@
#!/bin/bash
for (( i=1; i<=4; i++ ))
do
touch $i.sh
chmod 764 $i.sh
echo "#!/bin/bash" > $i.sh
done

View File

@ -0,0 +1,41 @@
#!/bin/bash
#
# Find_Problem - finds problem records using keywords
#
###########################################################
#
# Determine sql location & set variable
#
MYSQL=`which mysql`" Problem_Trek -u root"
#
##########################################################
#
# Obtain Keyword(s)
#
if [ -n "$1" ] # Check if a keyword was passed
then # Grab all the passed keywords
#
KEYWORDS=$@ # Grab all the params as separate words, same string
#
else # Keyword(s) not passed, Ask for them
echo
echo "What keywords would you like to search for?"
echo -e "Please separate words by a space: \c"
read ANSWER
KEYWORDS=$ANSWER
fi
#
#######################################################
#
# Find problem record
#
echo
echo "The following was found using keywords: $KEYWORDS"
echo
#
KEYWORDS=`echo $KEYWORDS | sed 's/ /|/g'`
#
$MYSQL <<EOF
SELECT * FROM problem_logger WHERE prob_symptoms REGEXP '($KEYWORDS)' OR prob_solutions REGEXP '($KEYWORDS)'\G
EOF
#

View File

@ -0,0 +1,65 @@
#!/bin/bash
#
# Record_Problem - records system problems in database
#
###########################################################
#
# Determine mysql location & put into variable
#
MYSQL=`which mysql`" Problem_Trek -u root"
#
###########################################################
#
# Create Record Id & Report_Date
#
#ID_NUMBER=`date +%y%m%d%H%M`
#
REPORT_DATE=`date +%y%m%d`
#
############################################################
#
# Acquire information to put into table
#
echo
echo -e "Birefly describe the problem & its symptoms: \c"
#
read ANSWER
PROB_SYMPTOMS=$ANSWER
#
# Set Fixed Date & Problem Solution to null for now
#
FIXED_DATE=0
PROB_SOLUTIONS=""
#
#############################################################
#
# Insert acquired information into table
#
echo
echo "Problem recorded as follows:"
echo
id=$($MYSQL -e "INSERT INTO problem_logger VALUES (null,$REPORT_DATE,$FIXED_DATE,'$PROB_SYMPTOMS','$PROB_SOLUTIONS');SELECT LAST_INSERT_ID() id")
id=`echo $id | gawk '{print $2}'`
$MYSQL <<EOF
SELECT * FROM problem_logger where id_number=$id\G
EOF
#
#############################################################
#
# Check if want to enter a solution now
#
echo
echo -e "Do you have a solution yet?(y/n) \c"
read ANSWER
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
./Update_Problem.sh $id
#
;;
*)
# if answer is anything but yes, just exit script
;;
esac
#
############################################################

View File

@ -0,0 +1,66 @@
#!/bin/bash
#
# Daily_Archive - Archive designated files & directories
######################################################
#
# Gather Current Date
#
DATE=`date +%y%m%d`
#
# Set Archive File Name
#
FILE=archive$DATE.tar.gz
#
# Set Configuration and Destination File
#
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
DESTINATION=/home/tiandi/archive/$FILE
#
##################### Main Script ###############
#
# Check Backup Config file exists
#
if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then
echo
else
echo
echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file"
echo
exit
fi
#
# Build the names of all the files to backup
#
FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File
#
read FILE_NAME # Read 1st record
#
while [ $? -eq 0 ]
do
# Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ]
then
# If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME"
else
# If file doesn't exist, issue warning
echo
echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file."
echo
fi
#
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record.
done
###########################################################
#
# Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#

View File

@ -0,0 +1,73 @@
#!/bin/bash
#
# Hourly_Archive - Every hour create an archive
######################################################
#
# Set Configuration and Destination File
#
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
#
# Gather Current Date,Month & Time
#
DAY=`date +%d`
MONTH=`date +%m`
TIME=`date +%k%M`
#
# Set Base Archive Destination Location
#
BASEDEST=/home/tiandi/archive/hourly
#
# Create Archive Destination Directory
mkdir -p $BASEDEST/$MONTH/$DAY
#
# Build Archive Destination File Name
DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz
#
##################### Main Script ###############
#
# Check Backup Config file exists
#
if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then
echo
else
echo
echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file"
echo
exit
fi
#
# Build the names of all the files to backup
#
FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File
#
read FILE_NAME # Read 1st record
#
while [ $? -eq 0 ]
do
# Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ]
then
# If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME"
else
# If file doesn't exist, issue warning
echo
echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file."
echo
fi
#
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record.
done
###########################################################
#
# Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#

View File

@ -0,0 +1,247 @@
#!/bin/bash
#
# Delete_User - Automates the 4 steps to remove an account
#
#################################################################
#
# Define Functions
#
#################################################################
function get_answer {
#
unset ANSWER
ASK_COUNT=0
#
while [ -z "$ANSWER" ] # while no answer is given, keep asking
do
ASK_COUNT=$[ $ASK_COUNT + 1 ]
#
case $ASK_COUNT in # If user gives no answer in time allowed
2)
echo
echo "Please answer the question."
echo
;;
3)
echo
echo "One last try... please answer the question."
echo
;;
4)
echo
echo "Since you refuse to answer the question..."
echo "exiting program."
echo
#
exit
;;
esac
#
echo
#
if [ -n "$LINE2" ]
then
echo $LINE1 # Print 2 lines
echo -e $LINE2" \c"
else # Print 1 line
echo -e $LINE1" \c"
fi
#
# Allow 60 seconds to answer before time-out
read -t 60 ANSWER
done
#
# Do a little variable clean-up
#
unset LINE1
unset LINE2
#
} #end of get_answer function
#
#################################################################
function process_answer {
#
case $ANSWER in
y|Y|YES|yes|yEs|yeS|YEs|yES)
# If user answers "yes".do nothing.
;;
*)
# If user answers anything but "yes", exit script
echo
echo $EXIT_LINE1
echo $EXIT_LINE2
echo
exit
;;
esac
#
# Do a little variable clean-up
unset EXIT_LINE1
unset EXIT_LINE2
#
} #End of process_answer function
#
################################################################
#
# End of Function Definitions
#
############### Main Script #################################
#
# Get name of User Account to check
#
echo "Step #1 - Determine User Account name to delete "
echo
LINE1="Please enter the username of the user"
LINE2="account you wish to delete from system:"
get_answer
USER_ACCOUNT=$ANSWER
#
# Double check with script user that this is the correct User Account
#
LINE1="Is $USER_ACCOUNT the user account"
LINE2="you wish to delete from the system?[ y/n ]:"
get_answer
#
############################################################
#
# Check that USER_ACCOUNT is really an account on the system
#
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
#
if [ $? -eq 1 ] # If the account is not found, exit script
then
echo
echo "Account, $USER_ACCOUNT, not found."
echo "Leaving the script..."
echo
exit
fi
#
echo
echo "I found this record:"
echo $USER_ACCOUNT_RECORD
echo
#
LINE1="Is this the correct User Account?[y/n]:"
get_answer
#
#
# Call process_answer function:
# if user answers anything but "yes", exit script
#
EXIT_LINE1="Because the account, $USER_ACCOUNT, is not "
EXIT_LINE2="the one you wish to delete, we are leaving the script..."
process_anser
#
##############################################################
#
# Search for any running processes that belong to the User Account
#
echo
echo "Step #2 - Find process on system belonging to user account"
echo
echo "$USER_ACCOUNT has the following processes running: "
echo
#
ps -u $USER_ACCOUNT #List the processes running
#
case $? in
1) # No processes running for this User Account
#
echo "There are no processes for this account currently running."
echo
;;
0) # Processes running for this User Account.
# Ask Script User if wants us to kill the processes.
#
unset ANSWER # I think this line is not needed
LINE1="Would you like me to kill the process(es)? [y/n]:"
get_answer
#
case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES) # if user answer "yes",
#kill User Account processes
#
echo
#
# Clean-up temp file upon signals
#
trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
#
# List user processes running
ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
#
exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input
#
read USER_PROCESS_REC # First record will be blank
read USER_PROCESS_REC
#
while [ $? -eq 0 ]
do
# obtain PID
USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1 )
kill -9 $USER_PID
echo "Killed process $USER_PID"
read USER_PROCESS_REC
done
#
echo
#
rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report
;;
*) # If user answers anything but "yes", do not kill.
echo
echo "Will not kill the process(es)."
echo
;;
esac
;;
esac
###################################################################################
#
# Create a report of all files owned by User Account
#
echo
echo "Step #3 - Find files on system belonging to user account"
echo
echo "Creating a report of all files owned by $USER_ACCOUNT."
echo
echo "It is recommended that you backup/archive these files."
echo "and then do one of two things:"
echo " 1) Delete the files"
echo " 2) Change the files' ownership to a current user account."
echo
echo "Please wait. This may take a while..."
#
REPORT_DATE=`date +%y%m%d`
REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
#
find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null
#
echo
echo "Report is complete."
echo "Name of report: $REPORT_FILE"
echo "Location of report: `pwd`"
echo
################################################################
#
# Remove User Account
echo
echo "Step #4 - Remove user account"
echo
#
LINE1="Do you wish to remove $USER_ACCOUNT's account from system? [y/n]:"
get_answer
#
# Cass process_answer function:
# if user answers anything but "yes", exit script
#
EXIT_LINE1="Since you do not wish to remove the user account."
EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..."
process_answer
#
userdel $USER_ACCOUNT # delete user account
echo
echo "User account, $USER_ACCOUNT, has been removed"
echo
#

View File

@ -0,0 +1,31 @@
#!/bin/bash
#
# Big_Users - find big disk space users in various directories
#############################################################
#Parameters for script
#
CHECK_DIRECTORIES="/var/log /home" #directories to check
#
######################### Main Script #######################
#
DATE=$(date '+%m%d%y') #Date for report file
#
exec > disk_space_$DATE.rpt #Make report file Std Output
#
echo "Top Ten Disk Space Usage" #Report header for while report
echo "for $CHECK_DIRECTORIES Directories"
#
for DIR_CHECK in $CHECK_DIRECTORIES #loop to du directories
do
echo ""
echo "The $DIR_CHECK Directory:" #Title header for each directory
#
# Creating a listing of top ten disk space users
du -S $DIR_CHECK 2>/dev/null |
sort -rn |
sed '{11,$D; =}' |
sed 'N; s/\n/ /' |
gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
#
done #End of for loop for du directories
#

View File

@ -0,0 +1,5 @@
#!/bin/bash
#download latest cURL file automatically
curl -s -o /home/tiandi/curl
http://curl.haxx.se/download/curl