#!/bin/sh
grep HISTTIMEFORMAT /etc/bashrc || echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "' >>/etc/bashrc
for U in `grep -v shutdown /etc/passwd|awk -F: '$NF~/sh/&&$NF!~/no/{print $1}'`
do
UHOME=`cat /etc/passwd|grep "^$U"|cut -d: -f6`
[ ! -f $UHOME/.bash_history ] && touch $UHOME/.bash_history
chattr +a $UHOME/.bash_history
done
#! /bin/bash
STORE_NAME=`date +%m%d`
STORE_DIR='/home/backup/mysql/'
exec 1>/var/log/mysqlbak.log 2>&1
echo mysqlbak begin at `date`
[ ! -d $STORE_DIR ] && mkdir -p $STORE_DIR
/usr/local/mysql/bin/mysqldump -uroot -p'password' databasename > $STORE_DIR/mysql-$STORE_NAME.sql
gzip $STORE_DIR/mysql-$STORE_NAME.sql
find $STORE_DIR -mtime +7 -name '*.sql.gz' |xargs rm -f
echo mysqlbak finish at `date`
#! /bin/bash
dir=/home/mysql
for file in `ls $dir`; do
for file2 in `ls $dir/$file` ; do
own=`ls -ld $dir/$file/$file2 |awk '{print $3}'`
if [ "$own" = "root" ] ; then
chown -R mysql:mysql $dir/$file/$file2
echo "$dir/$file/$file2" >>/tmp/checkown.log
fi
done
done
#! /bin/bash
exec >>/tmp/addcnip.log 2>> /tmp/addcnip.log
echo " "
echo begin at `date`
d=`date +%y%m%d --date "-1 hour"`
d1=`date +%d --date "-1 hour"`
d2=`date +y%m%d%H --date "-1 hour"`
d3=`date +%H --date "-1 hour"`
svnpath=http://lishiming.5d6d.com/svn/cnip
logfile=/home/logs/access-$d2\_log
count1=`wc -l /usr/local/sbin/newcnip.txt|awk '{print $1}'`
accipf=/usr/local/sbin/nocnip.txt
newcnipf=/usr/local/sbin/newcnip.txt
dip=192.168.1.102
ipt=/sbin/iptables
awk '{print $1}' $logfile |sort -n|uniq |grep -v '^$'|grep -v '_'>/tmp/initip.txt
rm -f /tmp/cnip.txt
rm -f /tmp/initip2.txt
rm -f /tmp/nocnip.txt
for ip in `cat /tmp/initip.txt`; do
n=`grep $ip $accipf|wc -l`
if [ "$n" = "0" ] ; then
echo $ip >>/tmp/initip2.txt
fi
done
killall whois
for ip in `cat /tmp/initip2.txt|grep -v '^$'`; do
[ -d /tmp/whois ] || mkdir /tmp/whois
whois $ip >/tmp/whois/$ip
coun=`cat /tmp/whois/$ip |grep 'ountry'|awk '{print $2}'|head -n1`
if [ "$coun" = "CN" ] || [ "$coun" = "cn" ]; then
ippool=`cat /tmp/whois/$ip |grep 'netnum' |awk '{print $2"-"$4}'`
echo $ippool >>/tmp/cnip.txt
else
echo $ip >>/tmp/nocnip.txt
fi
rm -f /tmp/whois/$ip
done
cat $accipf >>/tmp/nocnip.txt
grep -v '^$' /tmp/nocnip.txt|sort -n |uniq >$accipf
cat $newcnipf |grep -v '^$'>>/tmp/cnip.txt
count2=`grep -v '^$' /tmp/cnip.txt|sort -n |uniq |wc -l |awk '{print $1}'`
if [ "$count1" -lt "$count2" ]; then
sort -n /tmp/cnip.txt |uniq |grep -v '^$'>$newcnipf
/bin/cp -f $newcnipf /tmp/cnip/$d1-cnip.txt
$ipt -F
for ip in `cat $newcnipf|grep -v '^$'`; do
$ipt -I INPUT -m iprange --src-range $ip -d $dip -j DROP
done
for ip2 in `cat /usr/local/sbin/goodip.txt`; do
$ipt -I INPUT -s $ip2 -d $dip -j ACCEPT
done
if [ "$d3" != "00" ] ; then
/usr/bin/svn remove $svnpath\/$d.txt -m 'if exist'
fi
/usr/bin/svn import /tmp/cnip/$d1-cnip.txt $svnpath\/$d.txt -m 'add cnip'
fi
echo end `date`
#! /bin/bash
exec 1>/var/log/mysqlbak.log 2>&1
data_source=/home/mysql/
backup_dir=/mnt/mysqlbak
remote_dir=1.2.3.4:/data/mysqlbak
n=`date +%d`
w=`date +%w`
echo backup of mysql begins at `date`
/etc/init.d/mysqld stop
p=/var/run/mysqld/mysqld.pid
while ps -p $p >/dev/null;
do
sleep 1 && echo -n .
done
sync;sync;sync;sleep 2;
rsync -aru --delete $data_source/* $backup_dir/mysql_$w/
/etc/init.d/mysqld start
echo backup of mysql finished at `date`
rsync -aru --delete $backup_dir/mysql_$w/* $remote_dir/mysql_$n/
echo remote backup of mysql finished at `date`
df -h|awk '$5~/[8 9][0-9]%|100%/ ' >/tmp/checkdisk.log
sed 's/\(^a.*\)/\1 12/' test
#cat test
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj
#sed 's/\(^a.*\)/\1 12/' test
askdj 12
aslkd aslkdjf3e 12
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj 12
#! /bin/bash
chd='www.5dforum.com/bbs.php'
mailer='[email protected]'
maxrp=3
while :; do
n1=0
c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
if [ $c == "0" ] ; then
for i in `seq 1 $maxrp`; do
c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
if [ $c == "0" ] ; then
n1=$i
fi
sleep 10
done
if [ $n1 == $maxrp ]; then
echo "The 5dforum\'s network is down!"|mail -s 5dforum-down $mailer
exit
fi
else
if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
sleep 30
else
n2=0
for i in `seq 1 $maxrp`; do
if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
sleep 30
else
n2=$i
fi
done
if [ $n2 == $maxrp ] ; then
echo "The 5dforum is something wrong, please cheack!"|mail -s 5dforum-down $mailer
exit
fi
fi
fi
sleep 30
done
grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行
egrep '123|abc' filename //用egrep同样可以实现
awk '/123|abc/' filename // awk 的实现方式
其他awk相关文章
前提是,文件中只有一列数字。计算脚本为:
let sum=0 ; for i in `cat file `; do let j=$i; let sum=$j+$sum; done; echo $sum
sort -t : -k 5b,5 -k 3,3n /etc/passwd
sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
sort -t : -b -k 5,5 -k 3,3n /etc/passwd
通过上边三个例子,我只想说明sort中的 -t 和 -k 选项的意义。
其中-t选项用来定义分割符,在上边三个例子中,使用:作为分隔符
-k 后边跟域号,-k 5 代表按第五列进行排序, -k 5,5 代表从第5列开始到第5列结束
其中-b 选项代表忽略头行出现的空行, -n选项代表按数值大小排序
另外还有没有用到的一些选项
-c 测试文件是否已经分类。
-m 合并两个分类文件。
-u 删除所有复制行。
-o 存储sort 结果的输出文件名。
-r 对分类次序或比较求逆。
今天突然发现grep -q 用于if 逻辑判断很好用。
-q 参数,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. 中文意思为,安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。
小应用
# cat a.txt
nihao
nihaooo
hello
# if grep -q hello a.txt ; then echo yes;else echo no; fi
yes
# if grep -q word a.txt; then echo yes; else echo no; fi
no
其他postfix 相关文章 http://mylinux.5d6d.com/search.php?action=tag&name=postfix
1、return命令会使得一个函数返回.我们在前面的一些函数的用法中已经注意到了这一点.return命令会返回一个单一的数字参数, 而这个数字参数在调用这个函数的脚本中是可见的.如果没有指定返回参数,return在默认情况下会返回上一次命令的返回代码。
2、了解一個概念:return value ﹗
我們在 shell 下跑的每一個 command 或 function ,在結束的時候都會傳回父行程一個值,稱為 return value 。
在 shell command line 中可用 $? 這個變量得到最"新"的一個 return value ,也就是剛結束的那個行程傳回的值。
Return Value(RV) 的取值為 0-255 之間,由程式(或 script)的作者自行定議:
* 若在 script 裡,用 exit RV 來指定其值,若沒指定,在結束時以最後一道命令之 RV 為值。
* 若在 function 裡,則用 return RV 來代替 exit RV 即可。
Return Value 的作用,是用來判斷行程的退出狀態(exit status),只有兩種:
* 0 的話為"真"( true )
* 非 0 的話為"假"( false )
舉個例子來說明好了:
假設當前目錄內有一份 my.file 的文件,而 no.file 是不存在的:
代码:
$ touch my.file
$ ls my.file
$ echo $? # first echo
0
$ ls no.file
ls: no.file: No such file or directory
$ echo $? # second echo
1
$ echo $? # third echo
0
上例的第一個 echo 是關於 ls my.file 的 RV ,可得到 0 的值,因此為 true ﹔
第二個 echo 是關於 ls no.file 的 RV ,則得到非 0 的值,因此為 false ﹔
第三個 echo 是關於第二個 echo $? 的 RV ,為 0 的值,因此也為 true 。
請記住:每一個 command 在結束時都會送回 return value 的﹗不管你跑甚麼樣的命令...
awk 'gsub(/www/,"abc")' /etc/passwd // passwd文件中把所有www替换为abc
awk -F ':' 'gsub(/www/,"abc",$1) {print $0}' /etc/passwd // 替换$1中的www为abc
其他awk相关文章
- «
- 1
- ...
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- ...
- 66
- »