热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

shell编程练习(一):笔试110

笔试练习(一):1、求2个数之和[rootVM_0_5_centostest]#vi1.sh[rootVM_0_5_centostest]#cat1.sh#!bi

笔试练习(一):

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 ]; dosum=$((sum+i))i=$((i+1))
doneecho
"由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 *.*; domv $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
2 #include
3 #include
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 }

getopt.c

1 #include
2 #include
3 #include
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 }

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/bashecho "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/bash1 /bin/false1 /bin/sync1 /sbin/halt22 /sbin/nologin1 /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,250100 jason smith $5,000200 john doe $500300 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编程练习"获取百度网盘链接。

转:https://www.cnblogs.com/mmzs/p/9342381.html



推荐阅读
  • C语言注释工具及快捷键,删除C语言注释工具的实现思路
    本文介绍了C语言中注释的两种方式以及注释的作用,提供了删除C语言注释的工具实现思路,并分享了C语言中注释的快捷键操作方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • switch语句的一些用法及注意事项
    本文介绍了使用switch语句时的一些用法和注意事项,包括如何实现"fall through"、default语句的作用、在case语句中定义变量时可能出现的问题以及解决方法。同时也提到了C#严格控制switch分支不允许贯穿的规定。通过本文的介绍,读者可以更好地理解和使用switch语句。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
author-avatar
小葵小小葵_530
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有