面试题目集锦-bash

1.有文件file1

1.  查询file1里面空行的所在行号

gawk ‘{if($0~/^$/print NR)}’file

grep –n ^$ file

2.  查询file1abc结尾的行

gawk '/t$/{print $1}' xxx

grep abc$ file1

3.  打印出file1文件第1行到3

sed –n 13p file1

 

2.如何将本地端口请求转发到8080端口,当前主机为192.168.0.2

iptables –A PREROUTING –d 124.42.60.90 –p tmp –m tcp –dport 80

-j DNAT –to destination 10.0.0.18:8080

 

3.编写个shell脚本将/usr/local/test目录下大于100K的文件转移到/tmp目录下

#/bin/bash

[[ -d /usr/local/test ]] || echo "目录不存在"

file=/usr/local/test/*

for f in $file

    do

       if [ -f $f ];then

           if [ `ls -l $f | gawk '{print $5}'` -gt 100000 ];then

                mv $f /tmp

           fi

       fi

     done

 

4. 通过apache访问日志access.log统计IP和每个地址访问的次数,按访问量列出前10名。

日志样式例子如下:

192.168.1.243 – [02/Jul/2010:23:44:59 +0800] “GET/HTTP/1.1” 200 19

cat access_log | gawk ‘{print $1}’| sort –n | uniq –c | head –n 10

注:先排序后计算行数

 

5.有个文件如下

    http://a.domain.com/1.html

http://b.domain.com/1.html

http://c.domain.com/1.html

 

 

http://a.domain.com/2.html

 

 

http://b.domain.com/2.html

 

 

http://a.domain.com/3.html

 

 

要求:得到域名,并统计网址出现的次序。

得到的过应该是:

3 a.domain.com

2 b.domain.com

1 c.domain.com

方法1gawk -F[:/] '{print $4}' domain.txt | sort | uniq –c

 

6.将一个文本的奇数行和偶数行合并

    1. cat xxx.file

hello

hello

kitty

kitty

what

what

方法一:sed '$!N;s/\n/ /g' xxx

 

7.自动ftp上传

#/bin/bash

ftp n<<END_FTP

open 192.168.0.2

user zlj mima        //用户名zlj 密码mima

binary

prompt off           //关闭提示

mput test            //上传test

close

bye

END_FTP

 

8.自动ssh登录,从AB再到C

#/bin/bash/expect f

set timeout 30

spawn ssh codfei@B

expect “password:”

send “pppppp/r”

 

expect “]*”

send “ssh codfei@C\r”

expect “password:”

send “pppppp\r”

interact

 

9.打印第一个域(即每行第一个字符)

    cat xxx | cut -c1

 

10.实现字符翻转如zhang-gnahz

rev xxx.txt

 

11.从a.log文件中提取包含“WARNING”或“FATAL”同时不包含“IGNOR”的行,然后,提取“:”分割的第五个字段。

# grep -E "(WARNING|FATAL)" grep.txt |grep -v "IGNORE" |gawk –F: {print $5}

 

12. 添加一个新组为class01,添加属于这个组的30个用户,用户名的形式为stdXX,其中,XX0130

groupadd class01

#!/bin/bash

for i in std{01..30}

 do

        useradd $i -g class01

 done

 

或者php代码:

#/usr/bin/php q

<?php

exec(“groupadd class1”);

for($i=1;$i<=30;$i++){

exec(“useradd –g class1 stu”. $i);

}

?>

 

13. 在每个月的第一天备份并压缩/etc/目录下的所有内容,存放在/root/backup目录中,且文件名为为如下形式:yymmdd_etc

#!/bin/bash

name=`date +%y%m%d`

filename=${name}_etc

[[ -d /root/tmp ]] || mkdir -p /root/tmp/

tar -cf /root/tmp/$filename /etc

 

14. 某系统管理员需要每天做一定的重复工作,定制一个解决方案:

    1)在下午450删除/abc/目录下的全部子目录和全部文件

    2)从早上800~下午600每小时读取/xyz目录下x1文件中每行第一个域的全部数

    3)每逢周一下午550/data目录下的所有目录和文件归档并压缩为文件backup.tar.gz

    4)在早上800前开机后启动

 

15. 编写shell脚本获取本机的网络地址,比如IP192.168.100.2/255.255.255.0,那么它的网络地址是192.168.0.0/255.255.255.0

方法一:

#!/bin/bash

file=/etc/sysconfig/network-scripts/ifcfg-eth0

if [ -f $file ];then

IP=`grep "IPADDR"  $file | gawk -F[=\"] '{print $3}'`

MASK= grep "NETMASK"  $file | gawk -F[=\"] '{print $3}'

echo “$IP/$MASK”

exit 1

fi

方法二:

#/bin/bash

IP=`ifconfig eth0 | grep "inet addr" | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'`

MASK= `ifconfig eth0 | grep "inet addr" | sed 's/^.*Mask://g'`

echo “$IP/$MASK”

exit

 

16.判断一文件是不是字符设备,如果是则将其拷贝到/dev目录下

#/bin/bash

FILENAME=

echo “Please input your filename:”

read FILENAME

if [ -c $FILENAME ];then

    Cp $FILENAME /dev

fi

 

17. 编写shell程序实现自动删除50个账号的功能,账号为stud1stud50

#/bin/bash

I=1

while [ $i le 50 ]

do

userdel –r stud${i}

I=$(($i+1))

done

 

18. 编写一个shell脚本,完成功能:

1)显示文字“Waiting for a while …

2) 长格式显示当前目录下面的文件和目录,并输出重定向到/home/file.txt

3)定义一个变量,名为s,初始值为“Hello

4)使该变量输出重定向到/home/string.txt文件

#/bin/bash

echo “Waiting for a while …”

ls –l > /home/file.txt

S=Hello

echo $Hello > /home/string.txt

 

19. 编写一个shell脚本,它把第二个位置参数及其以后的各个参数指定的文件复制到第一个位置参数指定的目录中。

#/bin/bash

dir=$1

shift

while [ $1 ]

do file=$1

cp $1 $dir

       shift

    done

ls $dir

 

20. 编写一个shell脚本,利用for循环将当前目录下的.c文件移动到制定的目录,并按文件大小显示出移动后指定的目录的内容

#/bin/bash

for file in `ls *.c`

do

       mv $file $1

       ls

done

ls $1 –lS

 

21. 利用数组形式存放10个城市的名字,然后利用for循环把它们打印出来。

#/bin/bash

array=(A B C D E F G H I J )

for city in ${array[@]}         #或者${array[*]}

    do

       Echo $city

    done

 

22. 编写shell脚本,使用一个echo命令输出如下格式的内容(注意对齐格式)

    Id name   msg

    01  mike   “hello”

    02  john   “hi”