笔试练习(一):
1、求2个数之和
[root@VM_0_5_centos test]# vi 1.sh
[root@VM_0_5_centos test]# cat 1.sh
#! /bin/sh
first=0
second=0
read -p "Input the first number: " first
read -p "Input the second number: " second
result=$[$first+$second]
echo "result is : $result"
exit 0
[root@VM_0_5_centos test]# sh 1.sh
Input the first number: 13
Input the second number: 23
result is : 36
2、计算1-100的和
[root@VM_0_5_centos test]# vi 2.sh
[root@VM_0_5_centos test]# cat 2.sh
#!/bin/sh
i=0
sum=0
echo "你想求1-?的和: "
read max
while [ $i -le $max ]; do
sum=$((sum+i))
i=$((i+1))
done
echo "由1+2+3+...+$max的和是:$sum"
[root@VM_0_5_centos test]# sh 2.sh
你想求1-?的和:
100
由1+2+3+...+100的和是:5050
3、将当前目录下所有的文件的扩展名改为bak
[root@VM_0_5_centos test]# vi 3.sh
[root@VM_0_5_centos test]# cat 3.sh
#! /bin/sh
for i in *.*; do
mv $i ${i%%.*}.bak
done
[root@VM_0_5_centos test]# sh 3.sh
注:
${varible##*string} 从左向右截取最后一个string后的字符串
${varible#*string}从左向右截取第一个string后的字符串
${varible%%string*}从右向左截取最后一个string后的字符串
${varible%string*}从右向左截取第一个string后的字符串
“*”只是一个通配符有时可以不要
例如:
[root@VM_0_5_centos test]# j=1.2.3.4.sh
[root@VM_0_5_centos test]# echo ${j%%.*}
1
[root@VM_0_5_centos test]# echo ${j##*.}
sh
4、编译当前目录下的所有.c文件:
$(basename $file .c)的含义:例如main.c的basename就是去掉.c后的main
gcc -o filename.c filename的含义:定制目标名称,缺省的时候,gcc 编译出来的文档是a.out;
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4
5 void display_usage(void)
6 {
7 printf("please usage\n");
8 printf("./a.out -f -t time -n num -ddate\n");
9 }
10
11 int main(int argc, char *argv[])
12 {
13 char *optstring = "ft:n:d::?";
14 int opt;
15 int flag = 0;
16 int num = 0;
17 int time = 0;
18 int date= 0;
19
20 while ((opt = getopt(argc, argv, optstring)) != -1) {
21 switch (opt) {
22 case 'f':flag = 1; break;
23 case 'n':num = atoi(optarg);break;
24 case 't':time = atoi(optarg);break;
25 case 'd':date = atoi(optarg);break;
26 case '?':display_usage();exit(0);
27 default:display_usage();exit(0);
28 }
29 }
30
31 printf("flag = %d\tnum=%d\ttime=%d\tdate=%d\n", flag, num, time, date);
32
33 return 0;
34 }
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <syslog.h>
4
5
6 int main(void)
7 {
8 openlog("xwptest", LOG_PID, LOG_USER);
9 syslog(LOG_INFO|LOG_LOCAL2, "xwp info log OK");
10 syslog(LOG_NOTICE|LOG_LOCAL2, "xwp notice log OK");
11 syslog(LOG_DEBUG|LOG_LOCAL2, "xwp debug log OK");
12 closelog();
13 return 0;
14 }
[root@VM_0_5_centos 4]# ll
total 8
-rw-r--r-- 1 root root 787 May 20 2015 getopt.c
-rw-r--r-- 1 root root 315 May 20 2015 testlog.c
[root@VM_0_5_centos 4]# vi 4.sh
[root@VM_0_5_centos 4]# cat 4.sh
#! /bin/bash
for file in *.c; do echo $file ; gcc -o $(basename $file .c) $file ; sleep 2; done > compile 2>&1
[root@VM_0_5_centos 4]# ll
total 12
-rw-r--r-- 1 root root 114 Jul 20 14:58 4.sh
-rw-r--r-- 1 root root 787 May 20 2015 getopt.c
-rw-r--r-- 1 root root 315 May 20 2015 testlog.c
[root@VM_0_5_centos 4]# sh 4.sh
[root@VM_0_5_centos 4]# ll
total 40
-rw-r--r-- 1 root root 114 Jul 20 14:58 4.sh
-rw-r--r-- 1 root root 19 Jul 20 14:59 compile
-rwxr-xr-x 1 root root 8800 Jul 20 14:58 getopt
-rw-r--r-- 1 root root 787 May 20 2015 getopt.c
-rwxr-xr-x 1 root root 8624 Jul 20 14:59 testlog
-rw-r--r-- 1 root root 315 May 20 2015 testlog.c
5、打印root可执行文件数,处理结果: root's bins: 2306
[root@VM_0_5_centos test]# ll -a
total 28
drwxr-xr-x 3 root root 4096 Jul 20 15:06 .
dr-xr-xr-x. 22 root root 4096 Jul 20 15:11 ..
-rw-r--r-- 1 root root 173 Jul 20 13:59 1.sh
-rw-r--r-- 1 root root 161 Jul 20 14:10 2.sh
-rw-r--r-- 1 root root 54 Jul 20 14:24 3.sh
drwxr-xr-x 2 root root 4096 Jul 20 14:59 4
-rw-r--r-- 1 root root 91 Jul 20 15:06 5.sh
[root@VM_0_5_centos test]# vi 5.sh
[root@VM_0_5_centos test]# cat 5.sh
#! /bin/bash
echo "root's bins: $(find ./ -user root -type f | xargs ls -l | sed '/-..x/p' | wc -l)"
[root@VM_0_5_centos test]# sh 5.sh
root's bins: 12
注:-..x表示可执行权限,-rw-r--r--分为三部分,分别为用户、组、其它。
6、打印当前sshd的端口和进程id,处理结果: sshd Port&&pid: 22 1176
[root@VM_0_5_centos test]# vi 6.sh
[root@VM_0_5_centos test]# cat 6.sh
#! /bin/bash
netstat -apn | grep sshd | sed -n 's/.*:::\([0-9]*\)\ .* \ \([0-9]*\)\/sshd/\1 \2/p'
[root@VM_0_5_centos test]# sh 6.sh
[root@VM_0_5_centos test]# netstat -apn | grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1176/sshd
tcp 0 52 172.27.0.5:22 222.211.249.187:16769 ESTABLISHED 18016/sshd: root@pt
unix 2 [ ] DGRAM 20254712 18016/sshd: root@pt
unix 3 [ ] STREAM CONNECTED 15365 1176/sshd
[root@VM_0_5_centos test]# netstat -apn | grep sshd | sed -n 's/.*:\([0-9]*\)\ .* \ \([0-9]*\)\/sshd/\1 \2/p'
22 1176
7、输出本机创建20000个目录所用的时间,处理结果:
real 0m3.367s
user 0m0.066s
sys 0m1.925s
提示:time是一个测试时间的函数,time()在()中的就是需要测试的内容。
[root@VM_0_5_centos test]# vi 7.sh
[root@VM_0_5_centos test]# cat 7.sh
#! /bin/bash
time (
for i in {1..2000} ; do
mkdir /tmp/nnn$i
done
)
[root@VM_0_5_centos test]# sh 7.sh
real 0m1.801s
user 0m0.780s
sys 0m0.852s
[root@VM_0_5_centos test]# rm -rf /tmp/nnn*
8、打印本机的交换分区大小,处理结果: Swap:1021M
[root@VM_0_5_centos test]# vi 8.sh
[root@VM_0_5_centos test]# cat 8.sh
#! /bin/bash
free -m | sed -n '/Swap/p' | awk '{ print $2}'
[root@VM_0_5_centos test]# free -m | sed -n '/Swap/p'
Swap: 1021 0 1021
[root@VM_0_5_centos test]# sh 8.sh
1021
9、文本分析,取出/etc/password中shell出现的次数:
[root@VM_0_5_centos test]# vi 9.sh
[root@VM_0_5_centos test]# cat 9.sh
#! /bin/sh
echo "第一种方法:"
cat /etc/passwd | awk -F: '{if ($7!="") print $7}' | sort | uniq -c
echo "第二种方法:"
cat /etc/passwd|awk -F: '{if ($7!="") print $7}'| sort | uniq -c | awk '{print $2,$1}'
[root@VM_0_5_centos test]# sh 9.sh
第一种方法:
2 /bin/bash
1 /bin/false
1 /bin/sync
1 /sbin/halt
22 /sbin/nologin
1 /sbin/shutdown
第二种方法:
/bin/bash 2
/bin/false 1
/bin/sync 1
/sbin/halt 1
/sbin/nologin 22
/sbin/shutdown 1
10、文件整理,employee文件中记录了工号和姓名,(提示join)
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
答案:
[root@VM_0_5_centos test]# mkdir -p 10
[root@VM_0_5_centos test]# cd 10
[root@VM_0_5_centos 10]# vi employee.txt
[root@VM_0_5_centos 10]# cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
[root@VM_0_5_centos 10]# vi bonus.txt
[root@VM_0_5_centos 10]# cat bonus.txt
100 $5,000
200 $500
300 $3,000
400 $1,250
[root@VM_0_5_centos 10]# vi 10.sh
[root@VM_0_5_centos 10]# cat 10.sh
#! /bin/bash
join employee.txt bonus.txt | sort -k 2
[root@VM_0_5_centos 10]# sh 10.sh
400 Ashok Sharma $1,250
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000
获取脚本
注:所有脚本均可通过关注右侧公众号,后台回复"shell编程练习"获取百度网盘链接。