笔试练习(一):

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;

 #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> void display_usage(void)
{
printf("please usage\n");
printf("./a.out -f -t time -n num -ddate\n");
} int main(int argc, char *argv[])
{
char *optstring = "ft:n:d::?";
int opt;
int flag = ;
int num = ;
int time = ;
int date= ; while ((opt = getopt(argc, argv, optstring)) != -) {
switch (opt) {
case 'f':flag = ; break;
case 'n':num = atoi(optarg);break;
case 't':time = atoi(optarg);break;
case 'd':date = atoi(optarg);break;
case '?':display_usage();exit();
default:display_usage();exit();
}
} printf("flag = %d\tnum=%d\ttime=%d\tdate=%d\n", flag, num, time, date); return ;
}

getopt.c

 #include <stdio.h>
#include <unistd.h>
#include <syslog.h> int main(void)
{
openlog("xwptest", LOG_PID, LOG_USER);
syslog(LOG_INFO|LOG_LOCAL2, "xwp info log OK");
syslog(LOG_NOTICE|LOG_LOCAL2, "xwp notice log OK");
syslog(LOG_DEBUG|LOG_LOCAL2, "xwp debug log OK");
closelog();
return ;
}

testlog.c

[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编程练习"获取百度网盘链接。

shell编程练习(一): 笔试1-10的更多相关文章

  1. shell编程练习(三): 笔试21-30

    笔试练习(三): 21.编写shell程序,实现自动删除30个账号的功能. 账号名为std01至std30. [root@VM_0_5_centos test]# vi 21.sh [root@VM_ ...

  2. shell编程练习(二): 笔试11-20

    笔试练习(二): 11.写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. [root@VM_0_5_centos test]# vi 11.sh [root@VM_0_5_cento ...

  3. shell编程练习(四): 笔试31-68

    笔试练习(四): 31.找查较多的SYN连接 netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uni ...

  4. linux 10 -Bash Shell编程

    二十三. Bash Shell编程:     1.  读取用户变量:     read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入.在read命令后 ...

  5. linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行

    linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行 crontab每分钟执行一次,但5秒以上才有更新数据,有时候一分钟可能跑不完上一个进程,需要先等10秒再判 ...

  6. 10、shell编程+流程控制+分支嵌套

    SHELL 编程     shell 是一个命令解释器,侦听用户指令.启动这些指令.将结果返回给用户(交互式的shell)     shell 也是一种简单的程序设计语言.利用它可以编写一些系统脚本. ...

  7. Linux学习笔记(17) Shell编程之基础

    1. 正则表达式 (1) 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表达式:通配符用来匹配符合条件的文件名,通配符是完全匹配.ls.find ...

  8. Linux Shell编程入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  9. Shell编程菜鸟基础入门笔记

    Shell编程基础入门     1.shell格式:例 shell脚本开发习惯 1.指定解释器 #!/bin/bash 2.脚本开头加版权等信息如:#DATE:时间,#author(作者)#mail: ...

随机推荐

  1. centos 安装部署zabbix

    Zabbix_server初始安装部署 各模块要安装的模块 Server:server+nginx+mysql+php Agentd:agentd Proxy:proxy+mysql 1.准备环境: ...

  2. 代码文档生成工具Doxygen的使用备忘

    Doxygen备忘 下载与安装 一般步骤 生成配置文件Doxyfile: doxygen (-s) -g 建立目录结构, 比如Doxyfile文件\doc文件夹(输出)\src文件夹(放代码) -&g ...

  3. Docker构建其它组件

    构建mysql 运行centos7容器 docker run --privileged -dti --name=centos-container centos:7 /usr/sbin/init 查询c ...

  4. ubuntu设置ssh登录

    1.为ubuntu添加root用户(其实Ubuntu中的root帐号默认是被禁用了的,所以登陆的时候没有这个账号),打开初始账号,输入命令:sudo passwd root,设置root的密码 2.切 ...

  5. Hadoop2.0源码包简介

    Hadoop2.0源码包简介 1.解压源码包: 2.目录结构: hadoop-common-project:Hadoop基础库所在目录,如RPC.Metrics.Counter等.包含了其它所有模块可 ...

  6. ASP.NET Core MVC 源码学习:详解 Action 的匹配

    前言 在 上一篇 文章中,我们已经学习了 ASP.NET Core MVC 的启动流程,那么 MVC 在启动了之后,当请求到达过来的时候,它是怎么样处理的呢? 又是怎么样把我们的请求准确的传达到我们的 ...

  7. Windows反复重启的可能的解决办法

    Windows反复重启,原因很多,下面提供两个可能的解决办法: 1. 查看BIOS中关于SATA的设置,一般只有两种PCIE和IDE,调整成另外一个试试: 2. 查看BIOS的启动模式,如果是UEFI ...

  8. java处理数据库不支持的emoji表情符

    一般数据库的编码是utf8,utf8是不支持存储表情符的,当存入的微信昵称带有表情符时就会出现乱码情况,有两种解决方法: 1.mysql数据库升级到5.5版本以上,utf8改为utf8mb4,utf8 ...

  9. IM系统的MQ消息中间件选型:Kafka还是RabbitMQ?

    1.前言 在IM这种讲究高并发.高消息吞吐的互联网场景下,MQ消息中间件是个很重要的基础设施,它在IM系统的服务端架构中担当消息中转.消息削峰.消息交换异步化等等角色,当然MQ消息中间件的作用远不止于 ...

  10. Android Studio 调试各种国产手机经验总结

    为何加上“国产”二字呢,因为目前测试时就国产手机存在的安装问题多,而且都很奇葩,不得不说对于开发者时很不友好的. 下面就是个人总结的针对不同的机型调试时出现的问题做的总结: 1.VIVO 手机 解决方 ...