Skip to content

Commit 4b6e152

Browse files
committed
2016-05-23
1 parent b3f56ab commit 4b6e152

38 files changed

+1103
-0
lines changed

gawk进阶/gawk函数库

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
function myprint()
4+
{
5+
printf "%-16s - %s", $1, $4
6+
}
7+
8+
function myrand(limit)
9+
{
10+
return int(limit * rand())
11+
}
12+

gawk进阶/gawk脚本

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
BEGIN{FS="\n"; RS=""}
4+
{
5+
myprint()
6+
}

gawk进阶/new

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
for (( i=1; i<=4; i++ ))
4+
do
5+
touch $i.sh
6+
chmod 764 $i.sh
7+
echo "#!/bin/bash" > $i.sh
8+
done

gawk进阶/script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BEGIN{FS=","; print n}
2+
{print $n}

gawk进阶/test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
10
2+
5
3+
123
4+
50

gawk进阶/使用变量.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
#使用内建变量
3+
4+
gawk 'BEGIN {testing="This is a test"; print testing; testing=45; print testing}'
5+
6+
#处理数字值
7+
8+
gawk 'BEGIN{x=4; x= x*2+3; printx}'
9+
10+
#处理数组
11+
gawk 'BEGIN{capital["Ill"] = "SprintField"; print capital["Ill"]}'
12+
13+
#遍历数组变量
14+
gawk 'BEGIN{
15+
var["a"] = 1
16+
var["g"] = 2
17+
var["m"] = 3
18+
for( test in var)
19+
{
20+
print "Index:",test,"- Value:",var[test]
21+
}
22+
}'
23+
24+
print "------"
25+
26+
#删除数组变量
27+
gawk 'BEGIN{
28+
var["a"] = 1
29+
var["g"] = 2
30+
for (test in var)
31+
{
32+
print "Index:",test," - Value:", var[test]
33+
}
34+
delete var["g"]
35+
36+
print "----"
37+
38+
for (test in var)
39+
{
40+
print "Index;",test," - Value:", var[test]
41+
}
42+
}'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
#正则表达式
3+
4+
gawk 'BEGIN{FS=","}
5+
/11/{print $1}
6+
' test
7+
8+
#if-else语句
9+
gawk '{
10+
if($1 > 20)
11+
{
12+
x=$1*20
13+
print x
14+
}
15+
else
16+
{
17+
x=$1/2
18+
print x
19+
}
20+
}' test
21+
22+
#while 语句
23+
gawk '{
24+
total = 0
25+
i=1
26+
while(i<4)
27+
{
28+
total+=$i
29+
i++
30+
}
31+
avg = total/3
32+
print "Average:".avg
33+
}' test
34+
35+
36+
#do-while语句
37+
gawk '{
38+
total=0
39+
i=1
40+
do
41+
{
42+
total += $i
43+
i++
44+
}while(total < 150)
45+
print total }' test
46+
47+
48+
#for语句
49+
gawk '{
50+
total = 0
51+
for (i=1; i<4; i++)
52+
{
53+
total+=$i
54+
}
55+
avg = total/3
56+
print "Average:".avg
57+
}' test

gawk进阶/自定义函数.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
#gawk 自定义函数
3+
4+
gawk '
5+
function myprint()
6+
{
7+
printf "%-16s - %s\n", $1, $4
8+
}
9+
BEGIN{FS="\n"; RS=""}
10+
{
11+
myprint()
12+
}' test
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
#使用函数库和gawk脚本
4+
5+
gawk -f gawk函数库 -f gawk脚本 test
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# send data to the the table in the MYSQL database
3+
4+
MYSQL=`which mysql`
5+
6+
if [ $# -ne 2 ]
7+
then
8+
echo "Usage:mtest2 emplid lastname firstname salary"
9+
else
10+
#脚本变量一定要用双引号,字符串变量使用单引号
11+
statement=" insert into em_admin values(NULL, '$1', $2)"
12+
$MYSQL emwjs -u test <<EOF
13+
$statement
14+
EOF
15+
if [ $? -eq 0 ]
16+
then
17+
echo Data successfully added
18+
else
19+
echo Problem adding data
20+
fi
21+
fi

0 commit comments

Comments
 (0)