笔试练习(一):

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. 《Serverless架构-无服务单页应用开发》读后感

    本书的作者是[美]Ben Rady,译者郑赞美.简传挺,书中作者详细的介绍了如何使用html.js以及amazon提供的诸多云服务(Simple Storage Service(S3).Cognito ...

  2. <笔记>TP5的save方法返回值

    用save方法来更新数据时,若更新前后数据没有改变则返回0,更新成功返回影响行数,更新失败返回false 若想要数据没改变时提示修改成功,则需要严格判断 if(结果!==false){提示成功}而不是 ...

  3. (二)Javascript面向对象编程:构造函数的继承

    Javascript面向对象编程:构造函数的继承   这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承 ...

  4. Lombok轮子

    前提 自从进公司实习后,项目代码中能用 Lombok 的都用了,毕竟这么好的轮子要充分利用好.也可以减少一些 get/set/toString 方法的编写,虽说 IDEA 的插件可以自动生成 get/ ...

  5. tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案

    tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案   一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...

  6. 解析.NET对象的跨应用程序域访问(上篇)

    在目前的项目开发中,分布式开发已经逐渐成为主流.一个项目要是没有采用分布式架构,都不好意思跟别人说这是一个完整的项目.这句话虽然有些过激,但是随着人们对效率的要求在提高,以及产品需要提升用户体验.只有 ...

  7. xampp运行MySQL shutdown unexpectedly解决方案

    昨天晚上自己的网站突然打不开了,以为被人黑了.想想不应该啊,这小站不会有人关注的,于是登录服务器看了下,发现是Mysql打不开了 很奇怪,因为今天白天还是可以打开的,下班后也没有碰过服务器 首先看看是 ...

  8. C#中Quartz的简单易懂定时任务实现

    作为一个优秀的开源调度框架,Quartz 具有以下特点: 强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求: 灵活的应用方式,例如支持任务和调度的多种组合方式,支持调度数据的多种 ...

  9. [Postman]响应(7)

    Postman响应查看器有助于确保API响应的正确性.API响应由正文,标题和状态代码组成.邮递员在不同的标签中组织正文和标题.选项卡旁边会显示API调用的状态代码和完成时间. 响应还包含HTTP规范 ...

  10. Spark基础-scala学习(八、隐式转换与隐式参数)

    大纲 隐式转换 使用隐式转换加强现有类型 导入隐式转换函数 隐式转换的发生时机 隐式参数 隐式转换 要实现隐式转换,只要程序可见的范围内定义隐式转换函数即可.Scala会自动使用隐式转换函数.隐式转换 ...