Skip to content

Commit

Permalink
2016-05-23
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuhetao committed May 23, 2016
1 parent b3f56ab commit 4b6e152
Show file tree
Hide file tree
Showing 38 changed files with 1,103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gawk进阶/gawk函数库
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

function myprint()
{
printf "%-16s - %s", $1, $4
}

function myrand(limit)
{
return int(limit * rand())
}

6 changes: 6 additions & 0 deletions gawk进阶/gawk脚本
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

BEGIN{FS="\n"; RS=""}
{
myprint()
}
8 changes: 8 additions & 0 deletions gawk进阶/new
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions gawk进阶/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BEGIN{FS=","; print n}
{print $n}
4 changes: 4 additions & 0 deletions gawk进阶/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
10
5
123
50
42 changes: 42 additions & 0 deletions gawk进阶/使用变量.sh
Original file line number Diff line number Diff line change
@@ -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]
}
}'
57 changes: 57 additions & 0 deletions gawk进阶/使用模式,机构化命令.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions gawk进阶/自定义函数.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
#gawk 自定义函数

gawk '
function myprint()
{
printf "%-16s - %s\n", $1, $4
}
BEGIN{FS="\n"; RS=""}
{
myprint()
}' test
5 changes: 5 additions & 0 deletions gawk进阶/调用函数库和脚本.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

#使用函数库和gawk脚本

gawk -f gawk函数库 -f gawk脚本 test
21 changes: 21 additions & 0 deletions mysql数据库/向数据库中插入数据.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions mysql数据库/格式化输出数据.sh
Original file line number Diff line number Diff line change
@@ -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'

13 changes: 13 additions & 0 deletions mysql数据库/连接数据库并发送命令.sh
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions sed进阶/保持空间.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

#h将模式空间保存到保持空间
#H将模式空间附加到保持空间
#g将保持空间复制到模式空间
#G将保持空间保存到模式空间
#x交换模式空间和保持空间的内容

sed -n '/first/{
h
p
n
p
g
p
}' test
21 changes: 21 additions & 0 deletions sed进阶/删除指定的空白行和删除html标签.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions sed进阶/在脚本中使用sed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# shell wrapper for sed editor script to reverse lines

sed -n '{
1!G
h
$p
}' $1
8 changes: 8 additions & 0 deletions sed进阶/排除命令.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!/bin/bash

#排除命令,使本来起作用的命令不起作用

sed -n '/heade/!p' test

#反转文本文件
sed -n '{1!G ; h; $p}' test
11 changes: 11 additions & 0 deletions sed进阶/模式替代.sh
Original file line number Diff line number Diff line change
@@ -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}'

4 changes: 4 additions & 0 deletions sed进阶/测试.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

#测试,如果测试成功,如果没有标签,sed会跳转到结尾,如果有标签,就跳转到标签,如果测试失败,则不会跳转
sed -n '{s/first/matched/; t; s/This is the/No match on/}' test
3 changes: 3 additions & 0 deletions sed进阶/给文件中的行编号.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sed '=' test | sed 'N; s/\n/ /'
7 changes: 7 additions & 0 deletions sed进阶/跳转.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions sed进阶/输出末尾指定行数的数据.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
#输出末尾10行数据

sed '{
:start
$q
N
11,$D
b start
}' /etc/passwd
21 changes: 21 additions & 0 deletions sed进阶/重定向sed输出.sh
Original file line number Diff line number Diff line change
@@ -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"
24 changes: 24 additions & 0 deletions shell脚本编程进阶/创建捕捉脚本.sh
Original file line number Diff line number Diff line change
@@ -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
#
3 changes: 3 additions & 0 deletions shell脚本编程进阶/查看uptime获取在线用户数.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
#
uptime | sed 's/user.*$//' | gawk '{print $NF}'
4 changes: 4 additions & 0 deletions shell脚本编程进阶/查看僵尸进程.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

#查看僵尸进程
ps -al | gawk '{print $2,$4}' | grep Z
4 changes: 4 additions & 0 deletions shell脚本编程进阶/查看内存使用百分比.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

#查看内存使用百分比
free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/'
4 changes: 4 additions & 0 deletions shell脚本编程进阶/查看磁盘使用百分比.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

#查看磁盘实用百分比
df -h /dev/sda1 | sed -n '/% \//p' | gawk '{ print $5 }'
Loading

0 comments on commit 4b6e152

Please sign in to comment.