forked from fengyuhetao/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3f56ab
commit 4b6e152
Showing
38 changed files
with
1,103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
BEGIN{FS="\n"; RS=""} | ||
{ | ||
myprint() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BEGIN{FS=","; print n} | ||
{print $n} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
10 | ||
5 | ||
123 | ||
50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
#使用函数库和gawk脚本 | ||
|
||
gawk -f gawk函数库 -f gawk脚本 test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
sed '=' test | sed 'N; s/\n/ /' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
# | ||
uptime | sed 's/user.*$//' | gawk '{print $NF}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
#查看僵尸进程 | ||
ps -al | gawk '{print $2,$4}' | grep Z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/$/%/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }' |
Oops, something went wrong.