【BASH】bash shell的使用实例
Ø 读取文件的时间
#!/bin/bash
for file in `ls /root`
do
stat $file>1.txt
sed -n "7p" 1.txt>2.txt
usetime= awk -F ":" '{print $2}' 2.txt
echo "time="$file $usetime
done
Ø 读取文件的每行while语句。
cat afile | while read oneline
do
echo $oneline
done
Ø 读取文件的每行for语句。
for i in `cat afile`
do
echo $i
done
Ø 自己主动加入用户
useradd test
passwd test<<eof
111111
111111
eof
Ø 自己主动杀死进程
ps -a | grep '' | awk '{print $1}' | xargs -i kill 9 {}
Ø 保留七天的文件
#! /bin/bash
for file in `ls /orabak`
do
if [[ ${file:0:2} == "db" ]];then
fdate=${file:10:8}
mydate=`date +%Y%m%d -d "-7 day"`
if [[ $fdate < $mydate ]];then
rm -rf $file;
else
echo $file;
fi
else
fdate=${file:12:8}
mydate=`date +%Y%m%d -d "-7 day"`
if [[ $fdate < $mydate ]];then
rm -rf $file;
else
echo $file;
fi
fi
done
Ø 查看表的字段
su - oracle << BO
sqlplus / as sysdba<<EOF
desc user_tables;
exit;
EOF