美文网首页
linux习题

linux习题

作者: sixmonk | 来源:发表于2017-08-20 16:28 被阅读0次

    1.文件操作

    echo "演示与文件和文件夹创建处理等相关命令"

    echo "查看文件目录"

    echo "命令:ls -l"

    echo "\n`ls -l`"

    echo "========================================="

    read

    echo ">>>>>>>>>创建backup/old目录<<<<<<<<<"

    echo "命令:mkdir -p /home/q/test/backup/old"

    echo `mkdir -p /home/q/test/backup/old`

    read

    echo ">>>>>>>>>进入到backup/old目录<<<<<<<<<"

    echo "命令:cd /home/q/test/backup/old"

    echo `cd /home/q/test/backup/old`

    read

    echo ">>>>>>>>>创建两个file文件<<<<<<<<<"

    echo "命令:touch touchfile.txt touchfile2.txt"

    echo `touch touchfile.txt touchfile2.txt`

    read

    echo ">>>>>>>>>>拷贝gc_logrotate文件内容到touchfile文件<<<<<<<"

    echo "命令:cat /home/q/test/shelldemo/gc_logrotate.sh>touchfile.txt"

    echo "`cat /home/q/test/shelldemo/gc_logrotate.sh>touchfile.txt`"

    read

    echo ">>>>>>>查看touchfile文件,看内容是否正确<<<<<<<"

    echo "命令:cat touchfile.txt"

    echo "`cat touchfile.txt`"

    read

    echo ">>>>>>>>>>>拷贝touchfile文件到touchfile2文件中<<<<<<<<"

    echo "命令:cp -r touchfile.txt touchfile2.txt"

    echo "`cp -r touchfile.txt touchfile2.txt`"

    read

    echo ">>>>>>>>>查看touchfile2文件,看内容是否正确<<<<<<<"

    echo "命令:cat touchfile2.txt"

    echo "`cat touchfile2.txt`"

    read

    echo ">>移动touchfile2文件,使其覆盖touchfile,看touchfile内容是否正确<<<"

    echo "命令:mv touchfile2.txt touchfile.txt"

    echo "`mv touchfile2.txt touchfile.txt`"

    read

    echo "命令:cat touchfile.txt"

    echo "`cat touchfile.txt`"

    read

    echo ">>>>>>>查看touchfile文件的前五行<<<<<<<<<<<<"

    echo "命令:head -5 touchfile.txt"

    echo "`head -5 touchfile.txt`"

    read

    echo ">>>>>>>查看touchfile文件的后五行<<<<<<<<<<<"

    echo "命令:tail -5 touchfile.txt"

    echo "`tail -5 touchfile.txt`"

    read

    echo ">>>>>>>>删除touchfile文件<<<<<<"

    echo "命令:rm -f touchfile.txt"

    echo "`rm -f /home/q/test/backup/old/touchfile.txt`"

    2.实战习题1

    echo "需求:Tomcat服务中的catalina日志中,有很多exception,想查看一下内容,并且根据上下定位问题,如何操作?"

    read

    echo "命令:cat catalina.out | grep --color=always -i -ab5 "exception" | more"

    read

    echo |cat catalina.out | grep --color=always -i -ab5 "exception"| more

    read

    echo "说明:--color是高亮显示关键字 always是用于more命令执行后还可以高亮,-i是忽略大小写,-ab是查看关键字所在行前后各五行上下文,more是分页显示,按"空格"键翻页"

    read

    echo "需求:统计access所有404 条数"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==404) print \$9}' | wc -l | awk '{print "Total suceess item :"\$1}' "

    read

    echo |cat access.log | awk '{if ($9>0&&$9==404) print $9}' | wc -l | awk '{print "Total suceess item :"$1}'

    read

    echo "需求:首先查看下access日志长什么样子,查看前10行:"

    read

    echo "命令:cat access.log|head -10"

    read

    echo |cat access.log|head -10

    read

    echo "需求:以\t作为分隔符,获取第9列的内容,并且取出第九列为404的内容,查看10行"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==404) print \$9}’|tail -10"

    read

    echo |cat access.log | awk '{if ($9>0&&$9==404) print $9}'

    read

    echo "需求:统计包含404的所有行数"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==404) print \$9}' | wc -l"

    read

    echo|cat access.log | awk '{if ($9>0&&$9==404) print $9}' | wc -l

    read

    echo "需求:打印出最终的结果"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==404) print \$9}' | wc -l | awk '{print "Total suceess item :"\$1}'"

    read

    echo|cat access.log | awk '{if ($9>0&&$9==404) print $9}' | wc -l | awk '{print "Total suceess item :"$1}'

    read

    echo "需求:统计所有200条目中重复最多的前十条,并且打印出重复次数"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==200) print \$7}' | sort | uniq -c | sort -k1nr | head -10 "

    read

    echo |cat access.log | awk '{if ($9>0&&$9==200) print $7}' | sort | uniq -c | sort -k1nr | head -10

    read

    echo "需求:首先查看下access日志第七列长什么样子,查看前10行:"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==200) print \$7}' |head -10"

    read

    echo |cat access.log |awk '{if($9>0&&$9==200) print $7}' |head -10

    read

    echo "需求:查看下sort之后的样子,获取前10行"

    read

    echo "命令:cat access.log | awk '{if(\$9>0&&\$9==200)print \$7}' |sort|head -10"

    read

    echo |cat access.log |awk '{if($9>0&&$9==200) print $7}' |sort|head -10

    read

    echo "需求:统计一下重复的条数有哪些,在第一列打印出重复的行数,获取前10行"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==200) print \$7}' |sort|uniq -c |head -10"

    read

    echo |cat access.log |awk '{if($9>0&&$9==200) print $7}' |sort|uniq -c|head -10

    read

    echo "需求:排序统计结果,把重复最多的按照倒叙排列展示出来,获取前10行"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==200) print \$7}' |sort|uniq -c| sort -k1nr |head -10"

    read

    echo |cat access.log | awk '{if ($9>0&&$9==200) print $7}' |sort|uniq -c| sort -k1nr |head -10

    read

    echo "说明:sort是排序命令,其中:-k指明使用哪一列进行排序,-k1:表示使用第一列进行排序,n指明排序列为数字,r代表使用倒叙"

    read

    echo "需求:在上述结果中,只保留自己想要的sessionid的相关内容"

    read

    echo "命令:cat access.log | awk '{if (\$9>0&&\$9==200) print \$7}' | grep '/appback/api/auth/login' | sort | uniq -c | sort -k1nr | head -10 | sed 's/\/appback.*?//'"

    read

    echo |cat access.log | awk '{if ($9>0&&$9==200) print $7}' | grep '/appback/api/auth/login' | sort | uniq -c | sort -k1nr | head -10 | sed 's/\/appback.*?//'

    read

    echo "说明:sed是替换命令/原始值/被替换值/,本实例中使用正则表达式(/appback\.*?)匹配/appback到?之间的所有数据,并替换为null,sed是替换命令's/被替换值/替换该值/'"

    read

    echo "需求:查找/home/q/test下面大于50M的文件,并打印出文件路径信息"

    read

    echo "命令:find /home/q/test/ -type f -size +50M -exec ls {} \;"

    read

    echo |find /home/q/test/ -type f -size +50M -exec ls -lh {} \;

    read

    echo "说明:find是文件或者目录查找命令,第一个参数表示查找的目录,-type代表查找的类型,f表示文件类型,-size表示文件大小 -exec表示查找完成后执行的命令"

    read

    3.实战习题2

    echo "需求:查看6379端口的tcp连接,并且只看状态是 : ESTABLISHED,并排序"

    read

    echo "命令:netstat -ant | grep ":6379" | grep ESTABLISHED | awk '{printf "%s %s\n",\$5,\$6}'|sort"

    read

    echo |netstat -ant | grep ":6379" | grep "ESTABLISHED" | awk '{printf "%s %s\n",$5,$6}'|sort

    read

    echo "说明:netstat -ant是查看当前服务器上所有的tcp连接"

    read

    echo "需求:先看一下所有的tcp连接情况"

    read

    echo "命令:netstat -ant | more"

    read

    echo |netstat -ant | more

    read

    echo "需求:查看端口号是6379的连接,并且查看状态是ESTABLISHED"

    read

    echo "命令:netstat -ant |  grep ":6379" | grep "ESTABLISHED""

    read

    echo |netstat -ant |  grep ":6379" | grep "ESTABLISHED"

    read

    echo "需求:打印第五、第六列,并排序"

    read

    echo "命令:netstat -ant | grep \":6379\" | grep ESTABLISHED | awk '{printf \"%s %s\n\",\$5,\$6}'|sort"

    read

    echo |netstat -ant | grep ":6379" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}'|sort

    read

    echo "需求:查看本服务器IP地址"

    read

    echo "命令:ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2|awk '{print $1}'"

    read

    echo |ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2|awk '{print $1}'

    read

    echo "需求:查看本服务器网络配置状况"

    read

    echo "命令:ifconfig"

    read

    echo | ifconfig

    read

    echo "需求:查获取包含IP地址行信息"

    read

    echo "命令:ifconfig | grep 'inet addr:'  "

    read

    echo |ifconfig | grep 'inet addr:'

    read

    echo "需求:去掉本机虚拟地址干扰项"

    read

    echo "命令:ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' "

    read

    echo |ifconfig | grep 'inet addr:' | grep -v '127.0.0.1'

    read

    echo "需求:截取出IP地址"

    echo "命令:ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2|awk '{print $1}'"

    read

    echo |ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2|awk '{print $1}'

    read

    echo "需求:查看本服务器相关负载、cpu、内存以及交换分区信息"

    read

    echo "命令:top -ab -n 1 | head -5 "

    read

    echo |top -ab -n 1 | head -5

    read

    echo "说明:在这个top -ab -n 1 命令中,a表示按内存倒序排列,b表示将所有列输出,n表示只执行一次"

    read

    echo "需求:查看本机java进程相关信息"

    read

    echo "命令:top -ab -n 1|grep java "

    read

    echo | top -ab -n 1|grep java

    read

    echo "需求:从网上下载maven的压缩包"

    read

    echo "命令:curl -O http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz"

    read

    echo |cd /home/q/test/backup

    echo |rm -rf apache-maven-3.3.9*

    echo |rm -rf testmaven.jar

    echo |curl -O http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz

    read

    echo "需求:在当前目录解压apache-maven-3.3.9-bin.tar.gz"

    read

    echo "命令:tar -zxvf apache-maven-3.3.9-bin.tar.gz "

    read

    echo | tar -zxvf apache-maven-3.3.9-bin.tar.gz

    read

    echo "说明:tar是压缩/解压命令,z : 表示 tar 包是被 gzip 压缩过的,x : 从 tar 包中把文件提取出来,v : 显示详细信息,f x 指定被处理的文件"

    read

    echo "需求:在当前目录压缩apache-maven-3.3.9成testmaven.jar"

    read

    echo "命令:tar -zcvf testmaven.jar apache-maven-3.3.9"

    read

    echo | tar -zcvf testmaven.jar apache-maven-3.3.9

    read

    echo "说明:tar是压缩/解压命令,z : 表示 tar 包是被 gzip 压缩过的, 创建新的档案文件,v : 显示详细信息,f x 指定被处理的文件"

    read

    echo "需求:查看backup下刚才相关操作的信息"

    read

    echo "命令 : ls -l /home/q/test/backup"

    read

    echo | ls -l /home/q/test/backup

    read

    4.实战习题3

    echo "判断cpu的类型"

    read

    echo "执行命令 :grep "vendor_id" /proc/cpuinfo | uniq | cut -d: -f2"

    Vendor=`grep "vendor_id" /proc/cpuinfo | uniq | cut -d: -f2`

    read

    echo "VenderCpu is : $Vendor"

    read

    echo "执行if条件测试,匹配到相应的类型,并打印结果"

    if [[ $Vendor =~ [[:space:]]*GenuineIntel$ ]]; then

    echo "intel"

    elif [[ $Vendor =~ [[:space:]]*AuthenticAMD$ ]]; then

    echo "AMD"

    else

    echo "Unknown"

    fi

    read

    #判断磁盘使用空间并报警

    echo "判断磁盘使用空间并报警"

    read

    echo "执行命令 :df |sed 1d | awk '{print $1,$5}'|tr -d %"

    read

    disks=(`df |sed 1d | awk '{print $1,$5}'|tr -d %`)

    echo "打印disk信息,循环判断打印超过20%使用率的磁盘"

    read

    len=${#disks[@]}

    for ((i=1;i<=$len;i=i+2));do

    if [ ${disks[i]} -gt 20 ];then

    echo "${disks[$i-1]} ${disks[$i]}"

    fi

    done

    相关文章

      网友评论

          本文标题:linux习题

          本文链接:https://www.haomeiwen.com/subject/tkvfdxtx.html