Jean - 记录成长历程

  • 关于
  • Language
  • RSS Feed
  • 微博
  • 豆瓣
13
Mar

bash的边边角角1

By admin|脚本编程|Be the first to comment!

1、除特别说明外,本博客内容皆为原创,可以自由转载传播,但请署名及注明出处,不尊重别人劳动成果的不欢迎;

2、本博客内容遵守“署名-非商业性使用-禁止演绎 2.5 中国大陆”协议;

前言
当你手里只有一把锤子时,所有遇到的问题都会被看成是钉子。


1.写一个脚本报告用户信息,这个脚本只能添加一个参数,此参数为用户名,脚本可以报告系统中是否有此用户。如果有,程序报告:user exist .不在报告 no this user:

我认为很好的写法

#!/bin/bash
if [ $# -ne 1 ]
then
     echo ERROR
exit 1
fi
grep ^$1: passwd &> /dev/null
if [ $? -eq 0 ]
then
     echo User exist
else
     echo No this user
fi

我的写法

#!/bin/bash
grep $1 /etc/passwd > /dev/null
if (( $? == 0 ))
then
echo “YES”
else
echo “NO”
fi

不足:没有制造错误信号的exit 1 ,细节。

2.输出一个算术乘法表 (之前完全没写对)

#!/bin/bash

for ((i=1;i<=9;i++))

do

        for ((z=1;z<=i;z++))

        do

                echo -n ${z}x$i=$[$i*$z]‘  ’

        done

        echo

done

纯属娱乐题。
3.提取出/usr/share/doc 目录下的所有的文件名为index.html的文件。把他们集中放在/tmp/index 目录中。文件名字按提取顺序更名标记index1、index2 ……

绝对的边边角角,主要是红色部分的使用
#!/bin/bash

x=0

for i in `find /usr/share/doc -name index.html`

do

        ((x++))

        cp ${i} /tmp/index/index.html.$x

done

4.统计出*.zhangyiqun.cn出现的次数,并进行排序。

http://www.zhangyiqun.cn/index.html

http://www.zhangyiqun.cn/1.html

http://post.zhangyiqun.cn/index.html

http://mp3.zhangyiqun.cn/index.html

http://www.zhangyiqun.cn/3.html

http://post.zhangyiqun.cn/2.html

前阵子写过统计攻击者ip的脚本和这个类似,主要用到了sort 和uniq,这俩命令在统计中的作用实在太大了。

awk ‘BEGIN{FS=”/”} {print $3}’ zhangyiqun | sort | uniq -c | sort -r

以下三道题来自 http://bbs.chinaunix.net/archiver/?tid-950419.html

write a shell script only to list the hidden items of a designated directory.the designated directory must be acquired as a command parameter. if the parameter is not a directory , print a warning message

主要是考察对ls是否熟悉。

ls -A -l | awk ‘$NF ~ /^\./{print $NF}’

write a shell script to remove all the empty . txt files in your current directory and print the number of removed files

find /path -name “*.txt” -size 0

there is a famous game which is “counting seven” . now we use shell script to implement the game . print the number from 1 to 1000 ,omitting the number which has 7 in it or is a multiple of 7

#!/bin/bash

count=0

for((i=1;i<=1000;i++))

do

if (( i%7 == 0 ))

then

((count++))

continue

fi

if echo $i | grep ’7′ &>/dev/null # &>的意思是把所有输出(>和2>)全部重定向

then

((count++))

continue

fi

echo $i

done

echo $count

注:在shell中if就是判断命令的返回值… 0是正确,其他非0是错误

无觅相关文章插件,快速提升流量

Tagged as: bash, shell编程
Add your comment →

0 Comments

Leave your comment below! Cancel Reply

点击取消回复

View More Posts:
  • ←
  • →

分类

  • Linux系统 (61)
  • Linux集群 (4)
  • Web服务器 (26)
  • 创业相关 (6)
  • 工作笔记 (34)
  • 我爱折腾 (16)
  • 日记 (46)
  • 网络 (3)
  • 脚本编程 (49)

最新日志

  • 使用w3 total cache给wordpress提速
  • 从零开始,我的创业感悟
  • 为大众创造更美好的日常生活
  • 我的站立式工作平台
  • 阿里云计算使用简评

最新评论

  • admin on 从零开始,我的创业感悟
  • Kovlala on 从零开始,我的创业感悟
  • jean on 从零开始,我的创业感悟
  • Kovlala on 从零开始,我的创业感悟
  • ElmaByrd35 on 解决Fedora 11中文字体难看问题

博客互联

  • HackLog
  • leapsoul
  • Myredhat
  • Ninth element
  • Ti’blog
  • William Hua
  • young001
  • 懒人运维
  • 曙光博客
  • 月夜
  • 月影
  • 杂货店一号铺
  • 王先进’s blog
  • 疯中舞步
  • 菜鸟的技术站
  • 运维进行时

技术

  • DBA BLOG
  • 回忆未来[张宴]
  • 扶凯
  • 王炜's Blog
  • 百变贝贝

Powered by the inLine Minimal WordPress Theme