本文接 《shell 常见面试题及答案(3)》


七、

一、命令部分

1.新建一个用户user,密码设为123456,并将其加到root组。写出所用命令。

Loong:/home/yee/shell# adduser julius
Adding user `julius' ...
Adding new group `julius' (1001) ...
Adding new user `julius' (1001) with group `julius' ...
Creating home directory `/home/julius' ...
Copying files from `/etc/skel' ...
输入新的 UNIX 密码:
重新输入新的 UNIX 密码:
passwd:已成功更新密码
正在改变 julius 的用户信息
请输入新值,或直接敲回车键以使用默认值
	全名 []: julius
	房间号码 []: 
	工作电话 []: 
	家庭电话 []: 
	其它 []: 
Is the information correct? [Y/n] y
Loong:/home/julius# groups julius
julius
Loong:/home/julius# groups root
root fileshare
Loong:/home/julius# usermod -g root julius     组在前,用户在后
Loong:/home/julius# groups julius
root
Loong:/home/julius#
一,组操作
1,创建组
groupadd  test
增加一个test组
2,修改组
groupmod -n test2  test
将test组的名子改成test2
3,删除组
groupdel test2
删除 组test2
4,查看组
a),查看当前登录用户所在的组 groups (用户名)

2.将当前计算机的IP地址设为192.168.1.123,子网掩码为255.255.255.0。写出所用命令。

ifconfig eth0 192.168.1.123 netmask 255.255.255.0

3.你是公司的Linux服务的管理员。你使用vi编缉/etc/inittab文件时,在命令模式下查询shutdown字符串,使用的命令。

/shutdown 回车

4.显示当前系统的日期和时间命令。

yee@Loong:~/shell$ date
2012年 11月 30日 星期五 15:22:19 CST

5.查看命令sort的帮助文件,写出所用命令。

man sort

info sort

sort --help

6.统计/etc/passwd文件的字节数和行数,写出所用命令。

yee@Loong:~/shell$ wc -l /etc/passwd
31 /etc/passwd
yee@Loong:~/shell$ wc -c /etc/passwd
1435 /etc/passwd

7.分屏显示/etc/httpd/conf/httpd.conf文件的命令。

:vsplit
:vsplit xxx(要打开的文件名)

水平切割:
:split
:split xxx(要打开的文件名)
:new(在新窗口创建文件)
<CTRL+W> k:跳转到上面的窗口 (若为大写K,则是把当前窗口调至上方)
<CTRL+W> j:跳转到下面的窗口 (类似于上)
垂直切割:
:vsplit
:vsplit xxx(要打开的文件名)
:vnew(在新窗口创建文件)
:only        取消分屏
<CTRL+W> h:跳转到左边的窗口 (若为大写H,则是把当前窗口调至左方)
<CTRL+W> l:跳转到右边的窗口 (类似于上)
<CTRL+W> w:在窗口之间跳转
<CTRL+W> +:扩大窗口 (或在当前窗口位置,输入命令:res +9)
<CTRL+W> -:缩小窗口

8.将hello!写入 /home/wang/test.txt 的命令(vi及vim编辑器除外)。

Loong:/home/yee/shell# cat kkk.txt 
name=abc
pwd=123456
permission=rwx
Loong:/home/yee/shell# echo "hello" >> kkk.txt 
Loong:/home/yee/shell# cat kkk.txt 
name=abc
pwd=123456
permission=rwx
hello

9.让named守护进程运行在级别3,5的命令。

# telinit 3                    #用init也可以
# telinit 5                   #不懂named守护进程是啥进程,没有找到

        守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程。它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。守护进程常常在系统引导装入时启动,在系统关闭时终止。Linux系统有很多守护进程,大多数服务都是通过守护进程实现的,同时,守护进程还能完成许多系统任务,例如,作业规划进程crond、打印进程lqd等(这里的结尾字母d就是Daemon的意思)。

  由于在Linux中,每一个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端就称为这些进程的控制终端,当控制终端被关闭时,相应的进程都会自动关闭。但是守护进程却能够突破这种限制,它从被执行开始运转,直到整个系统关闭时才退出。如果想让某个进程不因为用户或终端或其他地变化而受到影响,那么就必须把这个进程变成一个守护进程。

常用的守护进程简单介绍:
Kudzu(硬件检测)  sshd(远程连接)  inetd(网络连接) ldap(目录访问协议)

Linux运行等级介绍:
最常用的是3,5

run level 0:是作关机用的,一开机就会作关机的动作。
run level 1:都是Single user mode模式,只允许root账号登录,主要是作一些系统维护的工作。
run level 2:可以使所有的用户登录,但不会启用NFS working,也就是没有网络功能
run level 3:可以使所有用户登录,并拥有完整的功能。包含run level 2没有的功 能,但是开机后是用文本模式
run level 4 :使用者自己定义,但是默认情况下和run level 3完全相同,
run level 5:和run level 3几乎一样,唯一的不同是开机后是图形界面,
run level 6:重启。开机后立刻重启

显示系统当前运行级别:
# runlevel          查看运行级别。
改变系统运行级别:
# init [0123456]   更改运行级别。(所更改的内容保存在内存中,关机或重启后还原)
系统服务程序在各运行级别的启动状态:
查看服务启动状态:
# Chkconfig  --list 查看当前系统中所有进程。
# chkconfig  --list    查看单个进程。
设置独立服务的启动状态:
  Chkconfig  --level   ]
例:# chkconfig --level 234 init off     将init进程设置为在2 3 4运行级别为off。

10.更改用户zhang的登录shell为/sbin/nologin的命令。

# usermod –s /sbin/nologin  yangang

11.查看当前linux服务器的主机名的命令。

yee@Loong:~$ uname -a
Linux Loong 2.6.27.1 #6 Wed Nov 10 16:17:35 CST 2010 mips64 GNU/Linux
yee@Loong:~$ cat /proc/sys/kernel/ho
hostname  hotplug   
yee@Loong:~$ cat /proc/sys/kernel/hostname 
Loong

12.查看当前主机的路由的命令。
yee@Loong:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.0.0      *               255.255.0.0     U     1      0        0 eth0
default         172.16.1.254    0.0.0.0         UG    0      0        0 eth0
yee@Loong:~$ 

13.配置当前主机的默认网关为192.168.2.254的命令。

 # route add default gw 192.168.2.254

14.连续ping目的IP为192.168.2.245为10次的的命令。

yee@Loong:~$ ping -c 10 172.16.0.30
PING 172.16.0.30 (172.16.0.30) 56(84) bytes of data.
64 bytes from 172.16.0.30: icmp_seq=1 ttl=64 time=4.48 ms
64 bytes from 172.16.0.30: icmp_seq=2 ttl=64 time=0.241 ms
64 bytes from 172.16.0.30: icmp_seq=3 ttl=64 time=0.231 ms
64 bytes from 172.16.0.30: icmp_seq=4 ttl=64 time=0.242 ms
64 bytes from 172.16.0.30: icmp_seq=5 ttl=64 time=0.226 ms
64 bytes from 172.16.0.30: icmp_seq=6 ttl=64 time=0.214 ms
64 bytes from 172.16.0.30: icmp_seq=7 ttl=64 time=0.259 ms
64 bytes from 172.16.0.30: icmp_seq=8 ttl=64 time=0.230 ms
64 bytes from 172.16.0.30: icmp_seq=9 ttl=64 time=0.212 ms
64 bytes from 172.16.0.30: icmp_seq=10 ttl=64 time=0.234 ms

--- 172.16.0.30 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9012ms
rtt min/avg/max/mdev = 0.212/0.657/4.484/1.275 ms
yee@Loong:~$ 

15.查看当前主机TCP协议连接情况的命令。
yee@Loong:~$ netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     52 Loong-3.local:ssh       172.16.7.94:52508       ESTABLISHED
tcp        0      0 Loong-3.local:ssh       172.16.7.94:53189       ESTABLISHED
yee@Loong:~$ 
NETSTAT(8)                           Linux Programmer's Manual                           NETSTAT(8)

NAME
       netstat  -  Print network connections, routing tables, interface statistics, masquerade con-
       nections, and multicast memberships

SYNOPSIS
       netstat   [address_family_options]   [--tcp|-t]   [--udp|-u]   [--raw|-w]   [--listening|-l]
       [--all|-a]  [--numeric|-n]  [--numeric-hosts]  [--numeric-ports]  [--numeric-users]  [--sym-
       bolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continu-
       ous|-c]

       netstat   {--route|-r}  [address_family_options]  [--extend|-e[--extend|-e]]  [--verbose|-v]
       [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c]

       netstat  {--interfaces|-i}  [--all|-a]  [--extend|-e[--extend|-e]]  [--verbose|-v]   [--pro-
       gram|-p]  [--numeric|-n]  [--numeric-hosts]  [--numeric-ports] [--numeric-users] [--continu-
       ous|-c]

       netstat {--groups|-g} [--numeric|-n] [--numeric-hosts]  [--numeric-ports]  [--numeric-users]
       [--continuous|-c]

       netstat  {--masquerade|-M}  [--extend|-e] [--numeric|-n] [--numeric-hosts] [--numeric-ports]
       [--numeric-users] [--continuous|-c]

       netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--raw|-w]
16.打包并压缩/etc目录的命令。
yee@Loong:~$ tar -czvf etc.tar.gz /etc

17.测试httpd是否安装的命令。

 # rpm  -q  httpd


18.卸载named软件包的命令。

 # rpm –e named   

19.查看月历并将内容追加到文件/dd中的命令。

yee@Loong:~$ cal -j
        十二月 2012        
 一  二  三  四  五  六  日
                    336 337
338 339 340 341 342 343 344
345 346 347 348 349 350 351
352 353 354 355 356 357 358
359 360 361 362 363 364 365
366                        
yee@Loong:~$ cal -j >>qq
CAL(1)                              BSD General Commands Manual                              CAL(1)

NAME
     cal, ncal - displays a calendar and the date of easter

SYNOPSIS
     cal [-3jmy] [[month] year]
     ncal [-jJpwy] [-s country_code] [[month] year]
     ncal [-Jeo] [year]
yee@Loong:~$ cal -3j
    十一月 2012           十二月 2012            一月 2013        
一 二 三 四 五 六 日  一 二 三 四 五 六 日  一 二 三 四 五 六 日  
          1  2  3  4                  1  2      1  2  3  4  5  6
 5  6  7  8  9 10 11   3  4  5  6  7  8  9   7  8  9 10 11 12 13
12 13 14 15 16 17 18  10 11 12 13 14 15 16  14 15 16 17 18 19 20
19 20 21 22 23 24 25  17 18 19 20 21 22 23  21 22 23 24 25 26 27
26 27 28 29 30        24 25 26 27 28 29 30  28 29 30 31         
                      31                       

20.查看当前目录的命令。

Pwd或ls

21.显示历史命令表的所有内容的命令。

history

22.将/etc/passwd的内容追加到文件aaa中的命令。

cat /etc/passwd >>aaa

23.查看/etc/passwd文件的前10行的命令。

head -n 10 /etc/passwd

24.查看/etc/vsftpd/vsftpd.conf的后10行的命令。

tail -n 10 /etc/vsftpd/vsftpd.conf

25.重启samba服务的命令。

yee@Loong:~$ /etc/init.d/samba restart
service smb restart
26.查看进程状态的命令。

ps -aux 或top

27.列出后台作业的命令。

jobs -l

28.将作业ID为5的后台作业放到前台的命令。

fg 5

29.停止ssh服务的命令。

 /etc/init.d/ssh stop
service sshd stop

30.将name=wxstc配置为全局变量的命令。

export name=wxstc

31.重启linux服务器的命令。

shutdown -r now

reboot

32.要给文件file1加上其他人可执行属性的命令。

chmod o+x file1

33.为了达到使文件sudent.txt的所有者有读(r)和写(w)的许可,而其他用户只能进行只读访问的命令。

chmod u=rw,o=r student.txt

34.把大写字母转换为小写字母的命令。

tr  'A-Z' 'a-z'

35.显示操作系统核心版本详细信息的命令。

uname -a

36.将新建文件权限设置为600的命令。

umask 600 filename

umask命令的一般格式:

  umask [选项] [ 掩码]  该命令用来设置限制新文件权限的掩码。当新文件被创建时,其最初的权限由文件创建掩码决定。用户每次注册进入系统时,umask命令都被执行,并自动设置掩码改变默认值,新的权限将会把旧的覆盖。  选项及其含义如下。  -S:显示当前的掩码。  umask是从权限中“拿走”相应的位,且文件创建时不能赋予执行权限

umask设置了用户创建文件的默认 权限,它与chmod的效果刚好相反,umask设置的是权限“补码”,而chmod设置的是文件权限码。


37.从IP地址查找域名或从域名来查找IP地址应使用什么命令。

host

38.如何查看用户zhang属于哪个组。

cat /etc/group

39.假如当前系统是在 level 3 运行,不重启系统就可转换到 level 5 运行的命令。

telinit 5

40.查看Linux的启动信息的命令。

dmesg

41.创建一个以自己姓的拼音缩写为名的用户。

useradd lee

42.进入用户主目录,显示当前的路径。

cd /home/lee

pwd

43.复制文件/etc/group到用户主目录,文件名不变。

cp /etc/group /home/lee

44.统计文件/etc/fstab的行数、单词数、字符数。

yee@Loong:~$ wc -lwc /etc/fstab 
  5  32 179 /etc/fstab
yee@Loong:~$ 

45.查看/etc/passwd文件的后5行。

ail -5 /etc/passwd

46.创建普通文件/home/abc。

touch /home/abc

47.修改/home/abc的属性为:属主具有一切权限,同组用户和其他用户具有可读可写的权限。

chmod 766 /home/abc

chmod u=rwx,go=rw /home/abc

48. 显示当前的时间。

date

49. 写出系统1分钟后关机的命令。

shutdown -t -s 60       #因系统不同参数有些差异

50. 启动ssh服务器。

service sshd start

56.显示2011年1月的日历。

yee@Loong:~$ ncal 01 2011
    一月 2011
一     3 10 17 24 31
二     4 11 18 25   
三     5 12 19 26   
四     6 13 20 27   
五     7 14 21 28   
六  1  8 15 22 29   
日  2  9 16 23 30   
yee@Loong:~$ cal 01 2011
     一月 2011      
一 二 三 四 五 六 日
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31                  
yee@Loong:~$ 

57.建立文件/etc/group的符号链接文件/root/abc。

ln –s /etc/group /root/abc

       -d, -F, --directory
              allow the superuser to attempt to hard link directories (note: will probably fail due
              to system restrictions, even for the superuser)

       -f, --force
              remove existing destination files

       -n, --no-dereference
              treat destination that is a symlink to a directory as if it were a normal file

       -i, --interactive
              prompt whether to remove destinations

       -s, --symbolic
              make symbolic links instead of hard links


       -S, --suffix=SUFFIX
              override the usual backup suffix

       -t, --target-directory=DIRECTORY
              specify the DIRECTORY in which to create the links

58.后台启动程序gedit。

/usr/bin/gedit &

67.写出30分钟后系统重启的命令。

shutdown –s –t 1800


77.设置当前时间设为15:00,显示当前系统时间。

date –s “15:00”
date

80.查看DHCP服务器的状态。

 service dhcpd status

二、操作题

1. 某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:

(1)在下午4 :50删除/abc目录下的全部子目录和全部文件;

(2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内。

 利用crontab 编辑定时任务,举例设置每个4分钟重启一次如下:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
 */4 *  *  *  * /usr/bin/reboot   #设置每个4分钟重启一次

2. 某学校内部既存在WINDOWS操作系统,又存在LINUX操作系统,为了方便资源共享,建立一台SAMBA服务器,其地址为192.168.0.5,建立std、teacher两个用户,同时设置其密码。

 实践

3.富曼公司有2个部门,分别是研发部yf和车间cj.搭建公司WWW服务器,IP地址为192.168.2.6,公司域名为www.fuman.com,研发部域名为yf.fuman.com,车间域名为cj.fuman.com,要求根据以上域名在IP地址192.168.2.6上搭建基于域名的虚拟服务器,并且不允许192.168.2.245的IP访问研发部的网页。

  实践

4.外语系老师为了共享教学资料,需要架设一个FTP服务器,允许所有老师以匿名方式登录FTP服务器,可以浏览文件、上传文件和创建文件夹,但不允许删除和修改文件。

  实践

5.编写一个名为mul的脚本程序,参数为一个大于20的正整数。先检查参数是否符合要求。如果不符合要求,请给出提示;如果符合要求,分别输出其与1到10的乘积。

 

  1 #!/bin/bash
  2 
  3 num=0
  4 if [ $1 -gt 20 ]
  5 then    
  6         for((i=1;i<11;i++))
  7         do      
  8                 num=`expr $1 \* $i`
  9                 echo "$1 * $i = $num "
 10         done    
 11 else    
 12         echo "the argument is too small "
 13 fi      
~       
Loong:/home/yee/shell/test# sh  mul.sh 30
30 * 1 = 30 
30 * 2 = 60 
30 * 3 = 90 
30 * 4 = 120 
30 * 5 = 150 
30 * 6 = 180 
30 * 7 = 210 
30 * 8 = 240 
30 * 9 = 270 
30 * 10 = 300 
Loong:/home/yee/shell/test# sh  mul.sh 14
the argument is too small 
Loong:/home/yee/shell/test# 

6.编写一个名为move的脚本程序,格式move  <file1>  <file2>。如果file1不存在,给出提示;否则移动file1至file2。

 

  1 #!/bin/bash
  2 
  3 if [ ! -f file1 ]
  4 then
  5         echo "the file1 doesnot exist !"
  6 else
  7         mv file1 file2
  8 fi
Loong:/home/yee/shell/test# sh mv.sh 
the file1 doesnot exist !
Loong:/home/yee/shell/test# touch file1
Loong:/home/yee/shell/test# sh -x mv.sh 
+ '[' '!' -f file1 ']'
+ mv file1 file2
Loong:/home/yee/shell/test# 

7.编写一个shell脚本,能够显示下面序列的前25个数字。0,1,1,2,3,5,8,13…,前二个数字之和为第三个数字,即著名的Fibonacci序列。

 

  1 #!/bin/bash
  2 
  3 n=0
  4 m=1
  5 num=2
  6 total=1
  7 while [ $num -lt 25 ]
  8 do
  9         total=`expr $n + $m`
 10         echo "$total= $n + $m"
 11         num=`expr $num + 1`
 12         n=$m
 13         m=$total
 14 done 
Loong:/home/yee/shell/test# sh fibonacci.sh 
1= 0 + 1
2= 1 + 1
3= 1 + 2
5= 2 + 3
8= 3 + 5
13= 5 + 8
21= 8 + 13
34= 13 + 21
55= 21 + 34
89= 34 + 55
144= 55 + 89
233= 89 + 144
377= 144 + 233
610= 233 + 377
987= 377 + 610
1597= 610 + 987
2584= 987 + 1597
4181= 1597 + 2584
6765= 2584 + 4181
10946= 4181 + 6765
17711= 6765 + 10946
28657= 10946 + 17711
46368= 17711 + 28657
Loong:/home/yee/shell/test# 
8.编写一个名为square的脚本程序,参数为一大于10的正整数。先检查参数是否符合要求。如果不符合要求,请给出提示;如果符合要求,输出从1到该正整数的平方值。

  1 #!/bin/bash
  2 
  3 num=0
  4 if [ $1 -gt 10 ]
  5 then
  6         for((i=1;i<=$1;i++))
  7         do
  8                 num=`expr $i \* $i`
  9                 echo "$i * $i = $num "
 10         done
 11 else
 12         echo "the argument is too small "
 13 fi
Loong:/home/yee/shell/test# sh square.sh 9
the argument is too small 
Loong:/home/yee/shell/test# sh square.sh 14
1 * 1 = 1 
2 * 2 = 4 
3 * 3 = 9 
4 * 4 = 16 
5 * 5 = 25 
6 * 6 = 36 
7 * 7 = 49 
8 * 8 = 64 
9 * 9 = 81 
10 * 10 = 100 
11 * 11 = 121 
12 * 12 = 144 
13 * 13 = 169 
14 * 14 = 196 
Loong:/home/yee/shell/test# 


shell 面试题的归纳和整理就暂时到此,欢迎读者们提出自己的新解题方法和读后感;

下来再回顾下python的面试题或者内容了,很久没用了,忘的差不多了,呜呜..........................................抓狂抓狂抓狂抓狂


posted on 2022-07-05 18:13  我在全球村  阅读(553)  评论(0编辑  收藏  举报