shell编程练习(一): 笔试1-10
笔试练习(一):
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的更多相关文章
- shell编程练习(三): 笔试21-30
笔试练习(三): 21.编写shell程序,实现自动删除30个账号的功能. 账号名为std01至std30. [root@VM_0_5_centos test]# vi 21.sh [root@VM_ ...
- shell编程练习(二): 笔试11-20
笔试练习(二): 11.写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. [root@VM_0_5_centos test]# vi 11.sh [root@VM_0_5_cento ...
- shell编程练习(四): 笔试31-68
笔试练习(四): 31.找查较多的SYN连接 netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uni ...
- linux 10 -Bash Shell编程
二十三. Bash Shell编程: 1. 读取用户变量: read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入.在read命令后 ...
- linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行
linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行 crontab每分钟执行一次,但5秒以上才有更新数据,有时候一分钟可能跑不完上一个进程,需要先等10秒再判 ...
- 10、shell编程+流程控制+分支嵌套
SHELL 编程 shell 是一个命令解释器,侦听用户指令.启动这些指令.将结果返回给用户(交互式的shell) shell 也是一种简单的程序设计语言.利用它可以编写一些系统脚本. ...
- Linux学习笔记(17) Shell编程之基础
1. 正则表达式 (1) 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表达式:通配符用来匹配符合条件的文件名,通配符是完全匹配.ls.find ...
- Linux Shell编程入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...
- Shell编程菜鸟基础入门笔记
Shell编程基础入门 1.shell格式:例 shell脚本开发习惯 1.指定解释器 #!/bin/bash 2.脚本开头加版权等信息如:#DATE:时间,#author(作者)#mail: ...
随机推荐
- 缓存,减少对sql语句的访问
一级缓存 SqlSession 的缓存 ------>自动开启 二级缓存: 做到从不同的缓存中共享数据 SqlSessionFactory 的缓存 --->需要手动开启 映射配置文件中配 ...
- ubuntu安装spyder和jupyter notebook
ubuntu安装spyder和jupyter notebook 安装spyder 安装spyder sudo apt install spyder sudo apt install spyder3 安 ...
- js获取当前页面的url网址信息小汇总
在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结. 下面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblog ...
- 性能测试-----monkey稳定性测试
我们稳定性测试用的monkey,跑monkey的同时存储log monkey脚本: @echo.@set /p name=请输入你的名字(比如liuyl): set YYYYmmdd=%date:~0 ...
- Unity3D在移动平台下加载AssetBundle导致Shader效果不正确的问题
这个问题,主要还是在移动平台下开发导致的. 在编辑器里调试加载AB时会导致Shader效果不正确的原因,主要还是编辑器下加载以IOS或是ANDROID平台打包的AB它所使用的shader已经编译成对应 ...
- DOS 命令 os系统(windows)
一.cd 相关操作 1."cd .. "or "cd ..\" --返回上一级 2.cd E:\Python -- 进入目录 二.dir --drectory ...
- AbstractRoutingDataSource 实现动态切换数据源
扩展AbstractRoutingDataSource类 package com.datasource.test.util.database; import org.springframework.j ...
- pwnable.kr详细通关秘籍(二)
i春秋作家:W1ngs 原文来自:pwnable.kr详细通关秘籍(二) 0x00 input 首先看一下代码: 可以看到程序总共有五步,全部都满足了才可以得到flag,那我们就一步一步来看 这道题考 ...
- eclipse中如何自动生成构造函数
eclipse中如何自动生成构造函数 eclipse是一个非常好的IDE,我在写java程序的时候使用eclipse感觉开发效率很高.而且有很多的快捷和简便方式供大家使用,并且能直接生成class文件 ...
- 为什么要学习 Spring Boot?
我们知道,从 2002 年开始,Spring 一直在飞速的发展,如今已经成为了在Java EE(Java Enterprise Edition)开发中真正意义上的标准,但是随着技术的发展,Java E ...