接 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 root:x:0:0:root:/root:/bin/bash
  2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
  3 bin:x:2:2:bin:/bin:/bin/sh
  4 sys:x:3:3:sys:/dev:/bin/sh
  5 sync:x:4:65534:sync:/bin:/bin/sync
  6 games:x:5:60:games:/usr/games:/bin/sh
  7 man:x:6:12:man:/var/cache/man:/bin/sh
  8 lp:x:7:7:lp:/var/spool/lpd:/bin/sh
  9 mail:x:8:8:mail:/var/mail:/bin/sh

  

在该文件中,每一行用户记录的各个数据段用“:”分隔,分别定义了用户的各方面属性。各个字段的顺序和含义如下:

                                             注册名:口令:用户标识号:组标识号:用户名:用户主目录:命令解释程序


(1)注册名(login_name):用于区分不同的用户。在同一系统中注册名是惟一的。在很多系统上,该字段被限制在8个字符(字母或数字)的长度之内;并且要注意,通常在Linux系统中对字母大小写是敏感的。这与MSDOS/Windows是不一样的。

  (2)口令(passwd):系统用口令来验证用户的合法性。超级用户root或某些高级用户可以使用系统命令passwd来更改系统中所有用户的口令,普通用户也可以在登录系统后使用passwd命令来更改自己的口令。

  现在的Unix/Linux系统中,口令不再直接保存在passwd文件中,通常将passwd文件中的口令字段使用一个“x”来代替,将/etc /shadow作为真正的口令文件,用于保存包括个人口令在内的数据。当然shadow文件是不能被普通用户读取的,只有超级用户才有权读取。

  此外,需要注意的是,如果passwd字段中的第一个字符是“*”的话,那么,就表示该账号被查封了,系统不允许持有该账号的用户登录。

  (3)用户标识号(UID):UID是一个数值,是Linux系统中惟一的用户标识,用于区别不同的用户。在系统内部管理进程和文件保护时使用 UID字段。在Linux系统中,注册名和UID都可以用于标识用户,只不过对于系统来说UID更为重要;而对于用户来说注册名使用起来更方便。在某些特 定目的下,系统中可以存在多个拥有不同注册名、但UID相同的用户,事实上,这些使用不同注册名的用户实际上是同一个用户。

  (4)组标识号(GID):这是当前用户的缺省工作组标识。具有相似属性的多个用户可以被分配到同一个组内,每个组都有自己的组名,且以自己的组标 识号相区分。像UID一样,用户的组标识号也存放在passwd文件中。在现代的Unix/Linux中,每个用户可以同时属于多个组。除了在 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)答案的输出并不完全正确,如:
Hello,mDNS,your UID is mDNS
Hello,daemon,,,,your UID is /bin/false
Hello,saned,your UID is 107
Hello,polkituser,your UID is 108
Hello,pulse,your UID is 109
Hello,daemon,,,,your UID is /bin/false
Hello,ftp,your UID is 110
Hello,daemon,,,,your UID is /bin/false
there are 43 users
UID一栏并不是数字,而是/ .../;
yee@Loong:~/shell$ cat pass
Debian-exim:x:104:110::/var/spool/exim4:/bin/false
hplip:x:105:7:HPLIP system user,,,:/var/run/hplip:/bin/false
polkituser:x:108:115:PolicyKit,,,:/var/run/PolicyKit:/bin/false
ftp:x:110:120:ftp daemon,,,:/srv/ftp:/bin/false
yee@Loong:~/shell$ 
只有4行,而实际读取时认为有7行
Loong:/home/yee/shell# sh b.sh 
Hello,Debian-exim,your UID is 104
Hello,hplip,your UID is 105
Hello,system,your UID is system
Hello,user,,,,your UID is /bin/false
Hello,polkituser,your UID is 108
Hello,ftp,your UID is 110
Hello,daemon,,,,your UID is /bin/false
there are 7 users
为啥呢?

我们看下for line读取的过程:
 
  1 #!/bin/bash
  2 file=pass
  3 num=0
  4 for line in `cat $file`
  5 do
  6         echo "the $num line is $line"
  7         num=$(($num+1))
  8 done    

yee@Loong:~/shell$ sh b.sh 
the 0 line is Debian-exim:x:104:110::/var/spool/exim4:/bin/false
the 1 line is hplip:x:105:7:HPLIP
the 2 line is system
the 3 line is user,,,:/var/run/hplip:/bin/false
the 4 line is polkituser:x:108:115:PolicyKit,,,:/var/run/PolicyKit:/bin/false
the 5 line is ftp:x:110:120:ftp
the 6 line is daemon,,,:/srv/ftp:/bin/false
因为该行有空格,有空格就当做另一行的开始,所以这是不靠谱的,看来是行读取出问题了!!
该如何处理呢?
经过请教可以用如下方法:
while read line
do 
   echo $line
done < filename


Loong:/home/yee/shell# cat pass
Debian-exim:x:104:110::/var/spool/exim4:/bin/false
hplip:x:105:7:HPLIP system user,,,:/var/run/hplip:/bin/false
polkituser:x:108:115:PolicyKit,,,:/var/run/PolicyKit:/bin/false
ftp:x:110:120:ftp daemon,,,:/srv/ftp:/bin/false
Loong:/home/yee/shell# sh passwd_hello.sh 
hello, Debian-exim, your UID is 104
hello, hplip, your UID is 105
hello, polkituser, your UID is 108
hello, ftp, your UID is 110
there are 4 users in this system
Loong:/home/yee/shell# 
虽然用户名并没有完全打印完整,但是已经比原来文不对题的错误好很多了,而且用户数没有计算错误!!
附验证后的code:
  1 #!/bin/bash
  2 
  3 file=pass
  4 total_user=`wc -l $file|cut -d " " -f1`    统计行数后再截取数目信息,否则后面有文件名
  5 while read line 
  6 do
  7         uid=`echo $line|cut -d: -f3 `
  8         username=`echo $line|cut -d: -f1`
  9         echo "hello, $username, your UID is $uid"       
 10 done < $file
 11 echo "there are $total_user users in this system"
再举一例:while read line
shell脚本里读取配置文件,并且把值一一赋给相应的变量
  1 #!/bin/bash
  2 
  3 while read line
  4 do
  5         name=`echo $line|awk -F '=' '{print $1}'`
  6         value=`echo $line|awk -F '=' '{print $2}'`
  7         case $name in  
  8                 "name")
  9                 name=$value;;
 10                 "pwd")
 11                 pwd="$value";;
 12                 "permission")
 13                 permission="$value";;
 14                 *)
 15                 echo "select wrong !";;
 16         esac    
 17 done < kkk.txt
 18 
 
kkk.txt:内容
  1 name=abc
  2 pwd=123456
  3 permission=rwx
Loong:/home/yee/shell# sh -x while_line.sh 
+ read line
++ echo name=abc
++ awk -F = '{print $1}'
+ name=name
++ echo name=abc
++ awk -F = '{print $2}'
+ value=abc
+ case $name in
+ name=abc
+ read line
++ echo pwd=123456
++ awk -F = '{print $1}'
+ name=pwd
++ echo pwd=123456
++ awk -F = '{print $2}'
+ value=123456
+ case $name in
+ pwd=123456
+ read line
++ echo permission=rwx
++ awk -F = '{print $1}'
+ name=permission
++ echo permission=rwx
++ awk -F = '{print $2}'
+ value=rwx
+ case $name in
+ permission=rwx
+ read line

  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 #!/bin/bash
  2 file=pass
  3 num=0
  4 for line in `cat $file`
  5 do
  6         username=`echo $line | cut -d: -f1`
  7         userid=`echo $line | cut -d: -f3`
  8         echo "Hello,$username,your UID is $userid"
  9         num=$(($num+1))
 10 done
 11 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"
  1 #!/bin/bash
  2 
  3 num=0
  4 for line in `ls /var`
  5 do
  6         echo "hello, $line"
  7         num=`expr $num + 1`
  8 done    
  9 echo "there total have $num file"
练习使用: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的用法参考下篇文章:http://blog.csdn.net/julius_lee/article/details/8224451
可借鉴的用法:
读取指定行,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 #!/bin/bash
  2 
  3 read -p "the first number:" num1
  4 read -p "the second number:" num2
  5 sum=`expr $num1 + $num2 `
  6 sub=`expr $num1 - $num2 `
  7 mul=`expr $num1 \* $num2 `
  8 div=`expr $num1 / $num2 `
  9 echo "sum=$sum; sub=$sub; mul=$mul; div=$div"

Loong:/home/yee/shell# sh -x calculate_math.sh 
+ read -p 'the first number:' num1
the first number:45
+ read -p 'the second number:' num2
the second number:32
++ expr 45 + 32
+ sum=77
++ expr 45 - 32
+ sub=13
++ expr 45 '*' 32
+ mul=1440
++ expr 45 / 32
+ div=1
+ echo 'sum=77; sub=13; mul=1440; div=1'
sum=77; sub=13; mul=1440; div=1


作业一:写一个脚本:
       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 #!/bin/bash
  2 
  3 mkdir /tmp/scripts
  4 cd /tmp/scripts
  5 cp -r /etc/pam.d .
  6 mv pam.d test
  7 chown -R redhat root test
  8 chmod -R 700 ./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:]]*
方法二:
利用正则表达式

  1 #!/bin/bash
  2 
  3 date
  4 mkdir /tmp/lstest
  5 cd /tmp/lstest
  6 mkdir a1d b56e 6test
  7 touch xy x2y 732
  8 ls -l|grep "^6"|ls|grep "^x";ls|grep "^a"
  9 ls|grep "^[a-z][0-9]"                            利用正则表达式
Loong:/home/yee/shell# sh file_management1.sh 
2012年 11月 27日 星期二 11:18:40 CST
x2y
xy
a1d
a1d
b56e
x2y
            

作业三:写一个脚本        添加10个用户user1到user10,但要求只有用户不存在的情况下才能添加  答案:       #!/bin/bash       for I in `seq 1 10`;do       cut -d: -f1 /etc/passwd |grep "user$I" 2>>/tmp/etc.err || useradd user$I       done作业四:写一个脚本       通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线      
 如果在线,就显示“ip is up”       如果不在线,就显示“ip is down”      答案: #!/bin/bash       for I in `seq 151 254`;do       ping -c1 -w1 192.168.0.$I &>/dev/null && echo "192.168.0.$I is up" || echo "192.168.0.$I is down"       done方法二: 循环
  1 #!/bin/bash
  2 
  3 for ((i=15;i<254;))
  4 do      
  5         ping -c 3 172.16.0.$i|grep Unreachable
  6         if [ $? -eq 0 ]                                 如果grep找到 Unreachable  则命令执行成功,表示没有ping通
  7         then
  8                 echo "the ip 172.16.0.$i is down"
  9         else
 10                 echo "the ip 172.16.0.$i is up"
 11         fi
 12         
 13         ((i++))
 14 done

Loong:/home/yee/shell# sh -x ping_test.sh 
+ (( i=15 ))
+ (( i<254 ))
+ ping -c 3 172.16.0.15
+ grep Unreachable
From 172.16.3.62 icmp_seq=1 Destination Host Unreachable
From 172.16.3.62 icmp_seq=2 Destination Host Unreachable
From 172.16.3.62 icmp_seq=3 Destination Host Unreachable
+ '[' 0 -eq 0 ']'
+ echo 'the ip 172.16.0.15 is down'
the ip 172.16.0.15 is down
+ (( i++ ))
+ (( 1 ))
+ (( i<254 ))
+ ping -c 3 172.16.0.16
+ grep Unreachable
+ '[' 1 -eq 0 ']'
+ echo 'the ip 172.16.0.16 is up'
the ip 172.16.0.16 is up
+ (( i++ ))
+ (( 1 ))
+ (( i<254 ))
+ ping -c 3 172.16.0.17
+ grep Unreachable
From 172.16.3.62 icmp_seq=1 Destination Host Unreachable
From 172.16.3.62 icmp_seq=2 Destination Host Unreachable
From 172.16.3.62 icmp_seq=3 Destination Host Unreachable
+ '[' 0 -eq 0 ']'
+ echo 'the ip 172.16.0.17 is down'
the ip 172.16.0.17 is down
+ (( i++ ))
+ (( 1 ))


三、

利用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 只能判别相邻的内容是否一样!

yee@Loong:~/lottery$ cat a.txt 
01,03,15,20,22,31|03 2012-11-04
01,03,15,20,22,31|03 2012-11-04
01,07,09,17,21,29|01 2012-11-01
05,09,12,29,30,31|05 2012-10-30
01,03,15,20,22,31|03 2012-11-04
yee@Loong:~/lottery$ uniq -c a.txt 
      2 01,03,15,20,22,31|03 2012-11-04
      1 01,07,09,17,21,29|01 2012-11-01
      1 05,09,12,29,30,31|05 2012-10-30
      1 01,03,15,20,22,31|03 2012-11-04
yee@Loong:~/lottery$ sort a.txt|uniq -c
      3 01,03,15,20,22,31|03 2012-11-04
      1 01,07,09,17,21,29|01 2012-11-01
      1 05,09,12,29,30,31|05 2012-10-30

求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’

Loong:/home/yee/lottery# free -m
             total       used       free     shared    buffers     cached
Mem:           495        488          6          0        108        163
-/+ buffers/cache:        216        278
Swap:         2055          8       2046
Loong:/home/yee/lottery# free -m|sed -n '/Swap/p'
Swap:         2055          8       2046
Loong:/home/yee/lottery# free -m|sed -n '/Swap/p'|awk '{print $2}'
2055
Loong:/home/yee/lottery# 


输出本机创建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

Loong:/home/yee/shell# sh  self_add2.sh 
please input the stop number:100

real	0m0.023s
user	0m0.024s
sys	0m0.000s
the result is: 5050
Loong:/home/yee/shell# cat self_add2.sh 
#!/bin/bash

read -p "please input the stop number:" input
s=0
i=0
until [ "$i" = "$input" ]
do
	i=$(($i+1));# echo "i=$i"
	s=$(($s+$i));# echo "s=$s"
done
echo "the result is: $s"

打印当前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


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