2016-05-18

dev
unknown 2016-05-18 15:54:22 +08:00
parent 994371b765
commit b3f56ab59f
16 changed files with 326 additions and 0 deletions

1
sed进阶/1.sh 100644
View File

@ -0,0 +1 @@
#!/bin/bash

1
sed进阶/2.sh 100644
View File

@ -0,0 +1 @@
#!/bin/bash

1
sed进阶/3.sh 100644
View File

@ -0,0 +1 @@
#!/bin/bash

1
sed进阶/4.sh 100644
View File

@ -0,0 +1 @@
#!/bin/bash

8
sed进阶/new 100644
View File

@ -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

5
sed进阶/test 100644
View File

@ -0,0 +1,5 @@
This is the header line
This is the first data line
This is the data line
This is the second data line
line This is the last line

View File

@ -0,0 +1,19 @@
#!/bin/bash
BEGIN {
print "The latest list of users and shells"
print "Userid Shell"
print "------ -----"
FS=":"
}
{
print $1 " " $7
}
END {
print "This concludes the listing"
}
#执行gawk命令截取/etc/passwd输出
#gawk -f gawk.sh /etc/passwd

View File

@ -0,0 +1,2 @@
{ print $1 "'s home directory is " $6 }

View File

@ -0,0 +1,24 @@
#!/bin/bash
#向文件写入
sed '1,2w test1' test1
echo -e "next\n"
#从文件读取
sed '3r ./test' ./test
echo -e "next\n"
#从文件读取,并插入字符流
sed '/lazy/r test' test
#向数据流末尾添加数据
sed '$r test' test
echo -e "next1\n"
sed '/lazy/ {
r test
d
}' test

View File

@ -0,0 +1,104 @@
#!/bin/bash
#sed编辑器基础
#替换标记
sed 's/lazy/ht/' ./test
echo -e "next\n"
#可用的替换标记
#1.数字 表明新闻本将替换第几处模式匹配的地方
sed 's/lazy/ht/2' ./test
#2.g 表明新文件将会替换所有已有文本出现的地方
sed 's/lazy/ht/g' ./test
#3.p 表明原来行的内容要打印出来,替换后的
sed 's/lazy/ht/p' ./test
#4.w file 将替换的结果写到文件中
sed 's/lazy/ht/w test1' ./test
echo -e "next\n"
#替换字符
sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
#或者
sed 's!/bin/bash!/bin/csh!' /etc/passwd
echo -e "next\n"
#使用地址
#1.数字方式的行寻址
sed '2s/lazy/cat/' ./test
sed '2,3s/lazy/cat/' ./test
sed '2,$s/lazy/cat/' ./test
#2.使用文本模式过滤器
sed '/tiandi/s/bash/csh/' /etc/passwd
echo -e "next\n"
#组合命令
sed '2{
s/fox/elephant/
s/dog/cat/
}' test
sed '2,${
s/fox/elephant/
s/dog/cat/
}' test
echo -e "next\n"
#删除行
sed '3d' ./test
sed '2,$d' ./test
sed '/number 1/d' ./test
#删除两个文本模式来删除某个范围的行,第一个开启删除功能,第二个关闭删除功能
sed '/1/,/3/d' ./test
echo -e "next\n"
#插入和附加文本
sed '3i\
This is an appended line.' ./test
sed '$a\
This is a new line of text.' ./test
#修改行
sed '3c\
This a changed line of text.' ./test
sed '/number 1/c\
This a changed line of text.' ./test
#替换两行文本
#sed '2,3c\
#This a changed line of text.' ./test
#转换命令,处理单个字符
#sed 'y/123/789/' ./test
echo -e "next\n"
#回顾打印
# p 打印文本行
# -n 禁止其他行,只打印包含匹配文本模式的行
sed -n '/number 3/p' ./test
#查看修改之前的行和修改之后的行
#sed -n '/3/{
#p
#s/line/test/p
#}' ./test
echo -e "next\n"
# 打印行号
sed '=' ./test
#打印指定的行和行号
#sed -n '/lazy/{
#=
#p
#}' ./test
#列出行 打印数据流中的文本和不可打印的ASCII字符任何不可打印的字符都用它们的八进制值前加一个反斜线或标准C风格的命名法比如用\t来代表制表符
sed -n 'l' ./test

View File

@ -0,0 +1,3 @@
#!/bin/bash
dialog --title text --msgbox "This is a test" 10 20

View File

@ -0,0 +1,36 @@
#!/bin/bash
# using select in the menu
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
PS3="Enter option:"
select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program"
do
case $option in
"Exit program")
break;;
"Display disk space")
diskspace;;
"Display logged on users")
whoseon;;
"Display memory usage")
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
done
clear

View File

@ -0,0 +1,49 @@
#!/bin/bash
function menu {
clear
echo
echo -e "\t\tSys Admin Menu\n"
echo -e "\t1. Display disk space"
echo -e "\t2. Display logged on users"
echo -e "\t3. Display memory usage"
echo -e "\t0. Exit program\n\n"
echo -en "\t\tEnter option:"
read -n 1 option
}
function diskspace {
clear
df -k
}
function whoseon {
clear
who
}
function menusage {
clear
cat /proc/meminfo
}
while [ 1 ]
do
menu
case $option in
0)
break;;
1)
diskspace;;
2)
whoseon;;
3)
menusage;;
*)
clear
echo "Sorry, wrong selection";;
esac
echo -en "\n\n\t\tHit any key to continue"
read -n 1 line
done
clear

View File

@ -0,0 +1,47 @@
#!/bin/bash
# using dialog to create a menu
temp=`mktemp -t test.XXXXXX`
temp2=`mktemp -t test2.XXXXXX`
function diskspace {
df -k > $temp
dialog --textbox $temp 20 60
}
function whoseon {
who > $temp
dialog --textbox $temp 20 50
}
function menusage {
cat /proc/meminfo > $temp
dialog --textbox $temp 20 50
}
while [ 1 ]
do
dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2> $temp2
if [ $? -eq 1 ]
then
break
fi
selection=`cat $temp2`
case $selection in
1)
diskspace;;
2)
whoseon;;
3)
menusage;;
0)
break;;
*)
dialog --msgbox "Sorry,invalid selection" 10 30
esac
done
rm -f $temp 2> /dev/null
rm -f $temp2 2> /dev/null

View File

@ -0,0 +1,18 @@
#!/bin/bash
# count number of files in your PATH
mypath=`echo $PATH | sed 's/:/ /g'`
count=0
for directory in $mypath
do
check=`ls $directory`
echo $check
for item in $check
do
count=$[ $count + 1 ]
done
echo "$directory - $count"
count=0
done

View File

@ -0,0 +1,7 @@
#!/bin/bash
#验证邮件
gawk --re-interval '/^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\+]+)\.([a-zA-Z]{2,5})/{print $0}'