Home

zhangyiqun

Thoughts, stories and ideas.

Research 2014年以前 スーパーマリオ 关于

13 Mar 2009
bash的边边角角1

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 </span>find /usr/share/doc -name index.html<span style="color: #ff0000;">

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是错误

Research 2014年以前 スーパーマリオ 关于