引言:本人学shell也有一段时间了,感觉学习shell和其他语言相同就是多练习程式。网上很多初学shell的朋友,一定为了很难找到练习的程式而苦
恼,我整合一下学习shell过程中碰到的比较基础的练习题,希望能给初学shell的朋友一些启发。(呵呵也许不该叫整合,习惯说这个词了)
例子没有先后顺序:每个例子可能有多种方法
1.
在/home/codfei连同他的子目录中查找含有codfei的任何文档
- 方法一:
[root@localhost Linuxos]# grep -rsn "codfei" /home/
/home/codfei/c/Unix_c/2:1:codfei::::::::::::::::::::::
/home/codfei/c/Unix_c/1:1:codfei::::::::::::::::::::::
Binary file /home/codfei/c/.charset.c.swp matches
- 方法二:
[root@localhost Unix_c]# find /home/codfei/ -type f | while read i;do grep -n codfei $i && echo $i && echo -----;done
1:codfei::::::::::::::::::::::
/home/codfei/c/Unix_c/2
-----
1:codfei::::::::::::::::::::::
/home/codfei/c/Unix_c/1
-----
Binary file /home/codfei/c/.charset.c.swp matches
/home/codfei/c/.charset.c.swp
-----
2.
设计一个Shell程式,在/userdata目录下建立50个目录,即user1~user50,并配置每个目录的权限为 rwxr-xr--
方法一:
#!/bin/bash
#最简单,效率最高的办法
mkdir -p /userdata/{1..50} && chmod 754 /userdata/{1..50}
方法二:
#!/bin/bash
#利用seq命令加while read结构
seq 1 50 | while read i;do
mkdir -p /userdata/$i
chmod 754 /userdata/$i
done
方法三:
用for或while循环
#!/bin/bash
i=0
while [ $i -lt 50 ];do
let i=i 1
mkdir -p /userdata/$i
chmod 754 /userdata/$i
done
#!/bin/bash
for ((i=1;i=50;i ));do
mkdir -p /userdata/$i
chmod 754 /userdata/$i
done
方法四:
#!/bin/sh
for D in user{1..50}
do
mkdir -m 754 -p $D
done
3.
在
linux
系统中有个文档,文档名为ABC.txt。如何将当前的系统时间追加到此文档行首?
三种方法:
echo -e "`date`\n`cat ABC.txt`" > ABC.txt
echo "`date | cat - ABC.txt`" > ABC.txt
sed -i "1i`date`" ABC.txt
本文来自ChinaUnix博客,假如查看原文请点:http://blog.chinaunix.net/u2/63379/showart_513220.html
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




