怎么样较准确地衡量一个面试者的shell编程 编程水平

5个实用的shell脚本面试题和答案
这边提到的5个面试问题,延续之前的有关Linux面试问题和答案。如果你是Tecmint的读者,你的支持我非常感谢。
1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录。
答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd。
现在,创建一个名为userstats.sh文件,将下面的代码添加到它。
#!/bin/bash
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current directory `pwd`"
给它添加执行权限,并且执行他。
# chmod 755 userstats.sh
# ./userstats.sh
代码如下:Hello, avi
Current date is Sat Jun& 7 13:05:29 IST 2014
User is avi&&&&& pts/0&&&&&&&
11:59 (:0)
Current directory /home/avi/Desktop
2.写一个shell脚本,进行两个数字的相加,如果没有输入参数就输出错误信息和一行使用说明
答案 : 下面是简单的shell脚本以及描述,如果没有命令行参数,它会抛出错误与如何使用脚本的说明。
再创建一个名为twonumbers.sh文件和下面的内容添加到文件里。
#!/bin/bash
# The Shebang
if [ $# -ne 2 ]
# If two Inputs are not received from Standard Input
# then execute the below statements
&&& echo "Usage - $0&& x&&& y"
&&& # print on standard output, how-to use the script (Usage - ./1.sh&& x&&& y )
&&& echo "&&&&&&& Where x and y are two nos for which I will print sum"
&&& # print on standard output, “Where x and y are two nos for which I will print sum ”
&&& exit 1
&&& # Leave shell in Error Stage and before the task was successfully carried out.
# End of the if Statement.
&&& echo "Sum of $1 and $2 is `expr $1 + $2`"
&&& # If the above condition was false and user Entered two numbers as a command Line Argument,&&
&&&&&& it will show the sum of the entered numbers.
给他添加可执行权限,并且执行。
代码如下:# chmod 755 two-numbers.sh
情形一: 未输入两个数字作为命令行参数运行脚本,你将得到下面的输出。
代码如下:# ./two-numbers.sh
Usage - ./two-numbers.sh&& x&&& y
&&&&&&& Where x and y are two nos for which I will print sum
情形二: 当数字存在时,你会得到如图所示的结果。
$ ./two-numbers.sh 4 5
Sum of 4 and 5 is 9
因此,上述shell脚本满足了问题的要求。
3.你需要打印一个给定的数字的反序,如输入10572,输出27501,如果没有输入数据,应该抛出错误和使用脚本说明。在此之前,告诉我你需要在这里使用的算法。
1.输入的数字为n
2.赋值 rev=0, sd=0 (反向和单个数字设置为0)
3.n % 10, 将得到最左边的数字
4.反向数字可以用这个方法生成 rev * 10 + sd
5.对输入数字进行右位移操作(除以10)
6.如果n & 0, 进入第三步,否则进行第七步
现在,创建一个名为`numbers.sh`文件,并添加以下代码。
#!/bin/bash
if [ $# -ne 1 ]
&&& echo "Usage: $0&& number"
&&& echo "&&&&&& I will find reverse of given number"
&&& echo "&&&&&& For eg. $0 0123, I will print 3210"
&&& exit 1
while [ $n -gt 0 ]
&&& sd=`expr $n % 10`
&&& rev=`expr $rev \* 10& + $sd`
&&& n=`expr $n / 10`
&&& echo& "Reverse number is $rev"
授予对文件的执行权限,并运行如下所示的脚本。
代码如下:# chmod 755 numbers.h
情形一: 当输入不包含命令行参数,你将得到下面的输出。
代码如下:./numbers.sh
Usage: ./numbers.sh& number
&&&&&& I will find reverse of given number
&&&&&& For eg. ./2.sh 123, I will print 321
情形二: 正常输入
$ ./numbers.sh 10572
Reverse number is 27501
上面的脚本非常完美,输出正是我们需要的。
4. 你应该直接用终端,而不是依靠任何shell脚本来进行实数计算。你会怎么做(比如实数7.56+2.453)?
答案 : 我们需要用如下所述的特殊方式使用bc命令。将7.56+2.453作为输入通过管道进入bc中。
$ echo 7.56 + 2.453 | bc
5. 你需要给出圆周率的值,精度为小数点后100位,什么是最简单的方法。
答案 : 找圆周率的值最简单的方法,我们只是需要发出以下命令。
很明显!安装我们必须有包pi。只用一个apt或yum命令,就能获得所需的软件包,同时用最简单方法来实现这个需求。
顶一下(0) 踩一下(0)
python 中文解决方法 gb2312
热门标签:smileliuyb 的BLOG
用户名:smileliuyb
文章数:40
访问量:21073
注册日期:
阅读量:5863
阅读量:12276
阅读量:414754
阅读量:1102261
51CTO推荐博文
1、 用sed修改test.txt的23行test为tset;
&&&&sed &i &23s/test/tset/g& test.txt
<font color="#、 查看/web.log第25行第三列的内容。
&&&&sed &n &25p& /web.log | cut &d & & &f3
&&&&head &n25 /web.log | tail &n1 | cut &d & & &f3
&&&&awk &F & & &NR==23{print $3}& /web.log
<font color="#、 删除每个临时文件的最初三行。
&&&&sed &i &1,3d& /tmp/*.tmp
<font color="#、 脚本编程:求100内的质数。
&&&&#!/bin/bash
&&&&while [ $i -le 100 ];do
&&&&&&&&ret=1
&&&&&&&&for (( j=2;j&$i;j++ ));do
&&&&if [ $(($i%$j)) -eq 0 &];then
&&&&&&&&done
&&&&&&&&if [ $ret -eq 1 ];then
&&&&&&&&&&&&echo -n &$i &
&&&&&&&&fi
&&&&&&&&i=$(( i+1 ))
<font color="#、 晚上11点到早上8点之间每两个小时查看一次系统日期与时间,写出具体配置命令
&&&&echo 1 23,1-8/2 * * * root /tmp/walldate.sh && /etc/crontab
<font color="#、 编写个shell脚本将当前目录下大于10K的文件转移到/tmp目录下
&&&&#!/bin/bash
&&&&fileinfo=($(du ./*))
&&&&length=${#fileinfo[@]}
&&&&for((i=0;i&$i=$(( i+2 ))));do
&&&&&&&&if [ ${fileinfo[$i]} -le 10 ];then
&&&&mv ${fileinfo[$(( i+1 ))]} /tmp
&&&&&&&&fi
<font color="#、 如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.2.1
&&&&/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.2.1:8080
&&&&/sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
<font color="#、 在11月份内,每天的早上6点到12点中,每隔2小时执行一次/usr/bin/httpd.sh 怎么实现
&&&&echo &1 6-12/2 * * * root /usr/bin/httpd.sh && /etc/crontab&
<font color="#、 在shell环境如何杀死一个进程?
&&&&ps aux& | grep | cut -f? 得到pid
&&&&cat /proc/pid
&&&&kill pid
<font color="#、 在shell环境如何查找一个文件?
&&&&find / -name abc.txt
<font color="#、 在shell里如何新建一个文件?
&&&&touch ~/newfile.txt
<font color="#、 linux下面的sed和awk的编写
<font color="#) &如何显示文本file.txt中第二大列大于56789的行?
& & awk -F &,& '{if($2&56789){print $0}}' file.txt
<font color="#) 显示file.txt的1,3,5,7,10,15行?
& & sed -n &1p;3p;5p;7p;10p;15p& file.txt
& & awk 'NR==1||NR==3||NR==5||&||NR=15{print $0}' file.txt
<font color="#) 将file.txt的制表符,即tab,全部替换成&|&
&&&&sed -i &s#\t#\|#g& file.txt
<font color="#、 把当前目录(包含子目录)下所有后缀为&.sh&的文件后缀变更为&.shell&& &&
&&&&#!/bin/bash
&&&&str=`find ./ -name \*.sh`
&&&&for i in $str
&&&&&&&&mv $i ${i%sh}shell
<font color="#、 编写shell实现自动删除50个账号功能,账号名为stud1至stud50
& & #!/bin/bash
& & for((i=1;i&=50;i++));do
&&&&& & userdel stud$i
<font color="#、 请用Iptables写出只允许10.1.8.179 访问本服务器的22端口。
& & /sbin/iptables -A input -p tcp -dport 22 -s 10.1.8.179 -j ACCEPT
& &&/sbin/iptables -A input -p udp -dport 22 -s 10.1.8.179 -j ACCEPT
& & /sbin/iptables -P input -j DROP&
<font color="#、 在shell中变量的赋值有四种方法,其中,采用name=12的方法称( & A & ) &。
A直接赋值&&&&& & &&&&&&&&&&&&&&B使用read命令
C使用命令行参数&&&&&&&&&&&&D使用命令的输出
<font color="#、 有文件file1
<font color="#) 查询file1里面空行的所在行号
& & grep -n ^$ file1
<font color="#) 查询file1以abc结尾的行
& & grep abc$ file1
<font color="#) 打印出file1文件第1到第三行
& & head -n3 file1
& & sed &3q& file1
& & sed -n &1,3p& file1
<font color="#、 假设有一个脚本scan.sh,里面有1000行代码,并在vim模式下面,请按照如下要求写入对应的指令
<font color="#) 将shutdown字符串全部替换成reboot
& & :%s/shutdown/reboot/g
<font color="#) 清空所有字符
<font color="#) 不保存退出
<font color="#、 1到10数字相加,写出shell脚本
& & #!/bin/bash
& & for((i=1;i&=10;i++));do
&&&&& & j=$[j+i ]
& & echo $j
<font color="#、 常见shell有哪些?缺省的是哪个?
& & /bin/sh& &&/bin/bash&&& /bin/ash& & /bin/bsh& & /bin/csh& & /bin/tcsh& & /sbin/nologin
<font color="#、 Shell循环语句有哪些?
& & for& & while& & until
<font color="#、 用SHELL模拟LVS,脚本怎么写
& & /sbin/iptable -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.1.11-192.168.1.12
<font color="#、 找出系统内大于50k,小于100k的文件,并删除它们。
& & #!/bin/bash
& & file=`find / -size +50k -size -100k`&
& & for i in $do
&&&&& & rm -rf $i
<font color="#、 脚本(如:目录dir1、dir2、dir3下分别有file1、file2、file2,请使用脚本将文件改为dir1_file1、dir2_file2、dir3_file3)
& & #!/bin/bash
& & file=`ls dir[123]/file[123]`
& & for i in $do
& & & & mv $i ${i%/*}/${i%%/*}_${i##*/}
<font color="#、 将A 、B、C目录下的文件A1、A2、A3文件,改名为AA1、AA2、AA3.使用shell脚本实现。
& & #!/bin/bash
& & file=`ls [ABC]/A[123]`
& & for i in $do
& & & & mv $i ${i%/*}/A${i#*/}
本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)2013年3月 C/C++大版内专家分月排行榜第三
2014年6月 Linux/Unix社区大版内专家分月排行榜第二2014年1月 Linux/Unix社区大版内专家分月排行榜第二2013年11月 Linux/Unix社区大版内专家分月排行榜第二2010年6月 Windows专区大版内专家分月排行榜第二2010年4月 Windows专区大版内专家分月排行榜第二2010年3月 Windows专区大版内专家分月排行榜第二2009年12月 Windows专区大版内专家分月排行榜第二2009年11月 Windows专区大版内专家分月排行榜第二2008年7月 Windows专区大版内专家分月排行榜第二2008年1月 Windows专区大版内专家分月排行榜第二2007年12月 Windows专区大版内专家分月排行榜第二2007年11月 Windows专区大版内专家分月排行榜第二2007年10月 Windows专区大版内专家分月排行榜第二2007年6月 Windows专区大版内专家分月排行榜第二
2014年7月 Linux/Unix社区大版内专家分月排行榜第三2014年4月 Linux/Unix社区大版内专家分月排行榜第三2013年1月 Linux/Unix社区大版内专家分月排行榜第三2010年5月 Windows专区大版内专家分月排行榜第三2009年9月 Windows专区大版内专家分月排行榜第三2009年8月 Windows专区大版内专家分月排行榜第三2008年8月 Windows专区大版内专家分月排行榜第三2008年6月 Windows专区大版内专家分月排行榜第三2007年9月 Windows专区大版内专家分月排行榜第三2007年7月 Windows专区大版内专家分月排行榜第三2007年4月 Windows专区大版内专家分月排行榜第三
2013年3月 C/C++大版内专家分月排行榜第三
2010年5月 Linux/Unix社区大版内专家分月排行榜第二2009年11月 Linux/Unix社区大版内专家分月排行榜第二
2010年4月 Linux/Unix社区大版内专家分月排行榜第三2009年10月 Linux/Unix社区大版内专家分月排行榜第三2009年6月 Linux/Unix社区大版内专家分月排行榜第三2008年7月 Linux/Unix社区大版内专家分月排行榜第三2007年12月 Linux/Unix社区大版内专家分月排行榜第三
2013年3月 C/C++大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。}

我要回帖

更多关于 linux shell 编程入门 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信