笔试练习(一):

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. vue学习-自动行合并的table

    测试的效果 测试的html源码截图 v-table在tableGroup.js中定义,以下就render方法,行的所有单元格都在tableGrouper.js中处理 render:function(h ...

  2. ajax轮询与长轮询

      刚刚网了关于轮询的知识,必须拿到自己这里来做个备份了! 其实以前用ajax轮询做个及时数据更新的,只是当时做了不知道那个就是轮询. 首先我们什么时候会想到用轮询技术呢? 一般而言,最多的是及时信息 ...

  3. Visual Studio Code and local web server

    It is the start of a New Year and you have decided to try Visual Studio Code, good resolution! One o ...

  4. 11、OpenCV实现图像的灰度变换

    1.灰度变换的基本概念 灰度变换指对图像的单个像素进行操作,主要以对比度和阈值处理为目的.其变换形式如下: s=T(r) 其中,T是灰度变换函数:r是变换前的灰度:s是变换后的像素.图像灰度变换的有以 ...

  5. 报错:Maximum call stack size exceeded

    最后通过查看一篇别人的博客,通过对照,发现我把参数值写反了,把参数a的值写成了参数b的值.详情可以看如下博客: https://www.cnblogs.com/dunitian/p/5865725.h ...

  6. why microsoft named their cloud service Azure?

    best guess I can personally make is that because Azure literally means “bright blue color of the clo ...

  7. 有关wkwebview和UIwebview获取html中的标签方法

    wkwebview方法如下: [webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id r ...

  8. vs链接错误解决方法

    常见引起链接错误的主要原因是由于项目不能找到所需的动态库的路径: 这里介绍一下引用第三方动态库的配置方法: 方法一: vs加载动态库需要先把动态库拷贝到exe所在文件夹,再修改项目属性: 链接器-&g ...

  9. 干了这碗鸡汤:从理发店小弟到阿里P10技术大牛

    1.引言 MIT TR 35(MIT Technology Review 35 Innovators Under 35)——“全球 35 位 35 岁以下科技创新青年”榜单,是全球最权威的青年科技创新 ...

  10. 吴恩达机器学习笔记33-评估一个假设输出(Evaluating a Hypothesis Outpute)

    当我们确定学习算法的参数的时候,我们考虑的是选择参量来使训练误差最小化,有人认为得到一个非常小的训练误差一定是一件好事,但我们已经知道,仅仅是因为这个假设具有很小的训练误差,并不能说明它就一定是一个好 ...