From b3f56ab59f067e2bf41b70f3195a7c1bba56cfe1 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 18 May 2016 15:54:22 +0800 Subject: [PATCH] 2016-05-18 --- sed进阶/1.sh | 1 + sed进阶/2.sh | 1 + sed进阶/3.sh | 1 + sed进阶/4.sh | 1 + sed进阶/new | 8 ++ sed进阶/test | 5 + 初识sed和gawk/gawk.sh | 19 ++++ 初识sed和gawk/script | 2 + 初识sed和gawk/sed文件操作.sh | 24 ++++ 初识sed和gawk/sed编辑器基础.sh | 104 ++++++++++++++++++ .../使用msgbox部件.sh | 3 + .../使用select命令.sh | 36 ++++++ .../使用脚本菜单.sh | 49 +++++++++ .../在脚本中使用dialog命令.sh | 47 ++++++++ 正则表达式/目录文件计数.sh | 18 +++ 正则表达式/邮件验证.sh | 7 ++ 16 files changed, 326 insertions(+) create mode 100644 sed进阶/1.sh create mode 100644 sed进阶/2.sh create mode 100644 sed进阶/3.sh create mode 100644 sed进阶/4.sh create mode 100644 sed进阶/new create mode 100644 sed进阶/test create mode 100644 初识sed和gawk/gawk.sh create mode 100644 初识sed和gawk/script create mode 100644 初识sed和gawk/sed文件操作.sh create mode 100644 初识sed和gawk/sed编辑器基础.sh create mode 100644 图形化桌面的脚本编程/使用msgbox部件.sh create mode 100644 图形化桌面的脚本编程/使用select命令.sh create mode 100644 图形化桌面的脚本编程/使用脚本菜单.sh create mode 100644 图形化桌面的脚本编程/在脚本中使用dialog命令.sh create mode 100644 正则表达式/目录文件计数.sh create mode 100644 正则表达式/邮件验证.sh diff --git a/sed进阶/1.sh b/sed进阶/1.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/sed进阶/1.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/sed进阶/2.sh b/sed进阶/2.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/sed进阶/2.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/sed进阶/3.sh b/sed进阶/3.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/sed进阶/3.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/sed进阶/4.sh b/sed进阶/4.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/sed进阶/4.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/sed进阶/new b/sed进阶/new new file mode 100644 index 0000000..c7cbc45 --- /dev/null +++ b/sed进阶/new @@ -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 diff --git a/sed进阶/test b/sed进阶/test new file mode 100644 index 0000000..61a4ee0 --- /dev/null +++ b/sed进阶/test @@ -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 diff --git a/初识sed和gawk/gawk.sh b/初识sed和gawk/gawk.sh new file mode 100644 index 0000000..15f6088 --- /dev/null +++ b/初识sed和gawk/gawk.sh @@ -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 diff --git a/初识sed和gawk/script b/初识sed和gawk/script new file mode 100644 index 0000000..5ce73be --- /dev/null +++ b/初识sed和gawk/script @@ -0,0 +1,2 @@ +{ print $1 "'s home directory is " $6 } + diff --git a/初识sed和gawk/sed文件操作.sh b/初识sed和gawk/sed文件操作.sh new file mode 100644 index 0000000..046001e --- /dev/null +++ b/初识sed和gawk/sed文件操作.sh @@ -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 diff --git a/初识sed和gawk/sed编辑器基础.sh b/初识sed和gawk/sed编辑器基础.sh new file mode 100644 index 0000000..011c93f --- /dev/null +++ b/初识sed和gawk/sed编辑器基础.sh @@ -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 + diff --git a/图形化桌面的脚本编程/使用msgbox部件.sh b/图形化桌面的脚本编程/使用msgbox部件.sh new file mode 100644 index 0000000..c9aa826 --- /dev/null +++ b/图形化桌面的脚本编程/使用msgbox部件.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +dialog --title text --msgbox "This is a test" 10 20 diff --git a/图形化桌面的脚本编程/使用select命令.sh b/图形化桌面的脚本编程/使用select命令.sh new file mode 100644 index 0000000..536a896 --- /dev/null +++ b/图形化桌面的脚本编程/使用select命令.sh @@ -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 diff --git a/图形化桌面的脚本编程/使用脚本菜单.sh b/图形化桌面的脚本编程/使用脚本菜单.sh new file mode 100644 index 0000000..8634404 --- /dev/null +++ b/图形化桌面的脚本编程/使用脚本菜单.sh @@ -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 diff --git a/图形化桌面的脚本编程/在脚本中使用dialog命令.sh b/图形化桌面的脚本编程/在脚本中使用dialog命令.sh new file mode 100644 index 0000000..eceb20b --- /dev/null +++ b/图形化桌面的脚本编程/在脚本中使用dialog命令.sh @@ -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 diff --git a/正则表达式/目录文件计数.sh b/正则表达式/目录文件计数.sh new file mode 100644 index 0000000..689ca6f --- /dev/null +++ b/正则表达式/目录文件计数.sh @@ -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 + diff --git a/正则表达式/邮件验证.sh b/正则表达式/邮件验证.sh new file mode 100644 index 0000000..1857ac8 --- /dev/null +++ b/正则表达式/邮件验证.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +#验证邮件 + +gawk --re-interval '/^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\+]+)\.([a-zA-Z]{2,5})/{print $0}' + +