接 shell 常见面试题及答案(1)本来是汇总成一篇的,后来发现太多了,分开分析;
二、
练习一:写一个脚本
1.设定变量FILE的值为/etc/passwd
2.依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么
形如:(提示:LINE=`wc -l /etc/passwd | cut -d" " -f1`)
Hello,root,your UID is 0.
3.统计一共有多少个用户
解析:
passwd 文件内容格式:
在该文件中,每一行用户记录的各个数据段用“:”分隔,分别定义了用户的各方面属性。各个字段的顺序和含义如下:
注册名:口令:用户标识号:组标识号:用户名:用户主目录:命令解释程序
(1)注册名(login_name):用于区分不同的用户。在同一系统中注册名是惟一的。在很多系统上,该字段被限制在8个字符(字母或数字)的长度之内;并且要注意,通常在Linux系统中对字母大小写是敏感的。这与MSDOS/Windows是不一样的。
(2)口令(passwd):系统用口令来验证用户的合法性。超级用户root或某些高级用户可以使用系统命令passwd来更改系统中所有用户的口令,普通用户也可以在登录系统后使用passwd命令来更改自己的口令。
通常将passwd文件中的口令字段使用一个“x”来代替,将/etc /shadow作为真正的口令文件,用于保存包括个人口令在内的数据。当然shadow文件是不能被普通用户读取的,只有超级用户才有权读取。
如果passwd字段中的第一个字符是“*”的话,那么,就表示该账号被查封了,系统不允许持有该账号的用户登录。
(3)用户标识号(UID):UID是一个数值,是Linux系统中惟一的用户标识,用于区别不同的用户。在系统内部管理进程和文件保护时使用 UID字段。在Linux系统中,注册名和UID都可以用于标识用户,只不过对于系统来说UID更为重要;而对于用户来说注册名使用起来更方便。在某些特 定目的下,系统中可以存在多个拥有不同注册名、但UID相同的用户,事实上,这些使用不同注册名的用户实际上是同一个用户。
除了在 passwd文件中指定其归属的基本组之外,还在/etc/group文件中指明一个组所包含用户。
(5)用户名(user_name):包含有关用户的一些信息,如用户的真实姓名、办公室地址、联系电话等。在Linux系统中,mail和finger等程序利用这些信息来标识系统的用户。
(6)用户主目录(home_directory):该字段定义了个人用户的主目录,当用户登录后,他的Shell将把该目录作为用户的工作目录。 在Unix/Linux系统中,超级用户root的工作目录为/root;而其它个人用户在/home目录下均有自己独立的工作环境,系统在该目录下为每 个用户配置了自己的主目录。个人用户的文件都放置在各自的主目录下。
(7)命令解释程序(Shell):Shell是当用户登录系统时运行的程序名称,通常是一个Shell程序的全路径名,
如/bin/bash。
注意的问题:
(1)使用 awk
使用awk的话,中间文件需要生产两个,一个存放用户ID,一个存放用户名,到最后输出时会出现一个问题:利用一次for line in无法进行输出,因为需要同时读取两个文件的每行作为变量用echo输出;这是很少用的,也不知道如何使用,除非将两个文件合并到一起;或者awk一次直接生成一个文件;
另外使用awk也不能按下面答案将每行值依次作赋值处理,因为awk处理的是文件,否则报错
++ awk -F : '{print $5}' daemon,,,:/var/run/pulse:/bin/false
awk: cannot open daemon,,,:/var/run/pulse:/bin/false (No such file or directory),它是处理文件的,怎能去处理行呢?!处理行还是sed或者cut 吧!
(2)拷贝的代码出错
'.sh: line 5: syntax error near unexpected token `do
'.sh: line 5: `do
主要是由于windows上的所处理的文件换行符是dos格式的"\r\n"可以使用cat -v 文件名 来查看换行符是否是,如果是上述的,则行结尾会是^m需要转换成linux/unix格式的"\n"具体转换办法就是转换换行符可以用sed命令处理一下文件,命令如下:sed 's/\r//' 原文件 >转换后文件
(3)答案的输出并不完全正确,如:
UID一栏并不是数字,而是/ .../;
只有4行,而实际读取时认为有7行
为啥呢?
我们看下for line读取的过程:
因为该行有空格,有空格就当做另一行的开始,所以这是不靠谱的,看来是行读取出问题了!!
该如何处理呢?
经过请教可以用如下方法:
while read line
do
echo $line
done < filename
虽然用户名并没有完全打印完整,但是已经比原来文不对题的错误好很多了,而且用户数没有计算错误!!
附验证后的code:
再举一例:while read line
shell脚本里读取配置文件,并且把值一一赋给相应的变量
1 name=abc
2 pwd=123456
3 permission=rwx
1. while read line; do
done < kkk.txt
按行读到变量 line 中
2. name=`echo $line|awk -F '=' '{print $1}'`
awk 命令是把一个字符串进行解析成一个数组, -F指定了 间隔符为 “=” , $1代表数组下标为0的字符串,即name值
另外,$0代表字符串本身。
以下答案来自网络,仅供参考:
答案一:#!/bin/bash
file="/etc/passwd"
LINES=`wc -l $file | cut -d" " -f1`
for I in `seq 1 $LINES`;do
userid=`head -$I $file | tail -1 |cut -d: -f3`
username=`head -$I $file | tail -1 |cut -d: -f1`
echo "hello $username,your UID is $userid"
done
echo "there are $LINES users"
答案二:#!/bin/bash
file=/etc/passwd
let num=0
for I in `cat $file`;do
username=`echo "$I" | cut -d: -f1`
userid=`echo "$I" | cut -d: -f3`
echo "Hello,$username,your UID is $userid"
num=$[$num+1]
用户数有计算错误!!前方有解释。
done
echo "there are $num users"
练习二:写一个脚本
1.切换工作目录至/var
2.依次向/var目录中的每个文件或子目录问好,形如:
(提示:for FILE in /var/*;或for FILE in `ls /var`;)
Hello,log
3.统计/var目录下共有多个文件,并显示出来
答案:#!/bin/bash
cd /var
let num=0
for I in `ls /var/*`;do
echo "hello $I"
num=$[$num+1]
done
echo "the number of files is $num"
练习使用:num=
`expr $num + 1` 中间有空格,否则 expr 0+1+1+1+1+1+1+1+1+1+1+1+1
练习三:写一个脚本
1.设定变量file的值为/etc/passwd
2.使用循环读取文件/etc/passwd的第2,4,6,10,13,15行,并显示其内容
3.把这些行保存至/tmp/mypasswd文件中
答案:
#!/bin/bash
file="/etc/passwd"
for I in 2 4 6 10 13 15;do
exec 3>/tmp/mypasswd
#将/tmp/mypasswd的内容作为标准写出
line=`head -$I $file | tail -1`
#先取出该行和前面的内容,再取出最后一行,如先取出前四行,在取最后一行;
echo "$line"
echo "$line" >&3
#将该行内容写到fd3;
exec 3>&-
# 关闭fd3;
done
答案中使用到了重定向的知识,具体exec的用法参考下篇文章:
可借鉴的用法:
读取指定行,for line in 指定行;line= head 前面内容| tail 最后一行;
通过exec将指定行内容写到某些文件;
练习四:写一个脚本
传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商
答案如下:vim test.sh
#!/bin/bash
echo "first number $1" (表示输出第一个数)
echo "second number $2" (表示输出第二个数)
echo " $(($1+$2))" (输出两数之和)
echo "$[$1-$2]" (输出两数之差)
echo "$[$1*$2]" (输出两数之积)
echo "$[$1/$2]" (输出两数之商)
:wq (表示保存并退出vi编辑器)
chmod +x test.sh (给test.sh执行的权限)
./test.sh 2 3 (传递两个参数并执行脚本
方法2:利用表达式计算:
作业一:写一个脚本:
1.创建目录/tmp/scripts
2.切换工作目录至此目录中
3.复制/etc/pam.d目录至当前目录,并重命名为test
4.将当前目录的test及其里面的文件和子目录的属主改为redhat
5.将test及其子目录中的文件的其它用户的权限改为没有任何权限
答案:
#!/bin/bash
mkdir -v /tmp/scripts
cd /tmp/scripts
cp -r /etc/pam.d ./test
chown -R redhat ./test
chmod -R o=--- ./test
作业二:写一个脚本
1.显示当前系统日期和时间,而后创建目录/tmp/lstest
2.切换工作目录至/tmp/lstest
3.创建目录a1d,b56e,6test
4.创建空文件xy,x2y,732
5.列出当前目录下以a,x或者6开头的文件或目录
6.列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
答案:
#!/bin/bash
date
mkdir -pv /tmp/lstest
cd /tmp/lstest
mkdir a1d b56e 6test
touch xy x2y 732
ls [ax6]*
ls [[:alpha:]][[:digit:]]*
方法二:
三、
利用top取某个进程的CPU的脚本:
#/bin/sh
Max_CPU=0
Avg_CPU=0
Total_Time=1
Process=$1
Interval=$2
# check the parameters
if [ $# -ne 2 ]; then
echo “Usage: $0 ProcessName Interval”
exit
fi
LogFile=”Per.txt”
echo “`date`” > $LogFile
while sleep $Interval
do
top -d 1 -n 1|grep $Process|grep -v grep|awk ‘{print $9″\t”$10}’ >> $LogFile
done
判断是否是设备文件
#/bin/bash
echo -e “The program will Judge a file is or not a device file.\n\n”
read -p “Input a filename:” filename
if [ -b $filename -o -c $filename ]; then
echo “$filename is a device file”
exit 0
else
echo “$filename is not a device file”
exit 1
firead –p:用于在读数据时输出提示信息
注意!
[ 之间是有空格的:if ! [ -f $filename ] ; then。一般用if [ ! * ]
-b
该『文件名』是否为一个 block device 装置?
-c
该『文件名』是否为一个 character device 装置?
-o
(or)两状况任何一个成立!例如 test -r file -o -x file,则file 具有 r 或 x 权限时,就可回传 true。
设备文件解释:
Linux里的文件分为:正规文件,设备文件,链接文件。
设备类型分为:字符设备,块设备,网络设备(只有网络设备没有设备文件,因为由于历史原因,网络是socket接口,这由于tcp/ip协议栈最早是BSD系统中实现(Unix-like)
字符设备
字符设备是指每次与系统传输1个字符的设备。这些设备节点通常为传真、虚拟终端和串口调制解调器之类设备提供流通信服务,它通常不支持随机存取数据。
字符设备在实现时,大多不使用缓存器。系统直接从设备读取/写入每一个字符。
块设备
块设备是指与系统间用块的方式移动数据的设备。这些设备节点通常代表可寻址设备,如硬盘、CD-ROM和内存区域。
块设备通常支持随机存取和寻址,并使用缓存器。操作系统为输入输出分配了缓存以存储一块数据。当程序向设备发送了读取或者写入数据的请求时,系统把数据中的每一个字符存储在适当的缓存中。当缓存被填满时,会采取适当的操作(把数据传走),而后系统清空缓存。
伪设备
在类Unix操作系统中,设备节点并不一定要对应物理设备。没有这种对应关系的设备是伪设备。操作系统运用了它们提供的多种功能。部份经常使用到的伪设备包括:
/dev/null
接受和丢弃所有输入;即不产生任何输出。
/dev/full
永远在被填满状态的设备。
/dev/loop
Loop设备
/dev/zero
产生连续的NUL字符的串流(数值为0)。
添加用户:
#/bin/bash
groupadd -f class1
for i in {9909..9911}
do
xx=`echo $i | sed ‘s/99//g’` 删除99
useradd -g class1 std${xx}
echo std${xx} | passwd std${xx} –stdin 自动交互的方法
echo -e “user std${xx} passwd is std${xx}”>>/root/newuser.txt
done
exit 0
注意等号的前后不要有空格:xx=`echo $i | sed ‘s/99//g’`
变量如果前后有字符,要是大括号
统计IP访问:
要求分析apache访问日志,找出访问页面数量在前100位的IP数。日志大小在78M左右。以下是apache的访问日志节选
202.101.129.218 – - [26/Mar/2006:23:59:55 +0800] “GET /online/stat_inst.php?pid=d065 HTTP/1.1″ 302 20-”-” “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)”
# awk ‘{print $1}’ log |sort |uniq -c|sort -r |head -n10
5 221.224.78.15
3 221.233.19.137
1 58.63.148.135
1 222.90.66.142
1 222.218.90.239
1 222.182.95.155
1 221.7.249.206
1 221.237.232.191
1 221.235.61.109
1 219.129.183.122
这个地方有个疑问,为什么在使用uniq之前要sort。
因为uniqu 只能判别相邻的内容是否一样!
求2个数之和
#/bin/bash
typeset first second
read -p “Input the first number:” first
read -p “Input the second number:” second
result=$[$first+$second]
echo “result is : $result”
exit 0
文本分析
取出password中shell出现的次数
第一种方法结果:
4 /bin/bash
1 /bin/sync
1 /sbin/halt
31 /sbin/nologin
1 /sbin/shutdown
第二种方法结果:
/bin/sync 1
/bin/bash 1
/sbin/nologin 30
/sbin/halt 1
/sbin/shutdown 1
答案:
cat /etc/passwd|awk -F: ‘{if ($7!=”") print $7}’|sort|uniq –c
cat /etc/passwd|awk -F: ‘{if ($7!=”") print $7}’|sort|uniq -c | awk ‘{print $2,$1}’
文件整理
employee文件中记录了工号和姓名
employee.txt:
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
bonus文件中记录工号和工资
bonus.txt:
100 $5,000
200 $500
300 $3,000
400 $1,250
要求把两个文件合并并输出如下
处理结果:
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000
答案:join employee bonus | sort -k 2 join 找出两个文件中指定栏位相同内容的行,并合并,-k 2 指定按第二部分排序
打印本机的交换分区大小
处理结果:
Swap:1024M
free -m | sed -n ‘/Swap/p’ | awk ‘{ print $2}’
free -m | sed -n ‘s/Swap:\ *\([0-9]*\).*/\1/p’
输出本机创建20000个目录所用的时间
处理结果:
real 0m3.367s
user 0m0.066s
sys 0m1.925s
答案:
# time for i in {1..2000} ; do mkdir /root/neil$i; done
real 0m6.200s
user 0m1.128s
sys 0m4.710s
打印当前sshd的端口和进程id
处理结果:
sshd Port&&pid: 22 5412
答案:netstat -anp | grep sshd | sed -n ‘s/.*:::\([0-9]*\)\ .* \ \([0-9]*\)\/sshd/\1 \2/p’
打印root可以使用可执行文件数
处理结果:
root’s bins: 2306
echo “root’s bins: $(find ./ -type f | xargs ls -l | sed ‘/-..x/p’ | wc -l)”
root’s bins: 3664
编译当前目录下的所有.c文件:
for file in *.c; do echo $file ; gcc -o $(basename $file .c) $file ; sleep 2; done > compile 2>&1
将一目录下所有的文件的扩展名改为bak
for i in *.*;do mv $i ${i%%.*}.bak;done