设为首页 加入收藏

TOP

Shell脚本练习(一)
2023-07-23 13:39:42 】 浏览:144
Tags:Shell

企业面试题

京东

问题1:使用Linux命令查询file1中空行所在的行号。

[root@server ~]# cat file1
问题1:使用Linux命令查询file1中空行所在的行号。
[root@server ~]# awk '/^$/{print NR}' file1
2

问题2:有文件chengji.txt内容如下''
张三 40
李四 50
王五 60
使用Linux命令计算第二列的和并输出。

[root@server ~]# cat chengji.txt
张三 40
李四 50
王五 60
[root@server ~]# awk '{sum+=$2}END{print sum}' chengji.txt
150

搜狐 讯网

问题1:Shell脚本里如何检查一个文件是否存在?如果不存在该如何处理?

[root@server ~]# cat 3.sh
#!/bin/bash
read -p "please input filename:" filename
if [ -f $filename ]
then
 echo "file exist"
else
 echo "file is not exist"
 touch $filename
fi

新浪

问题1:用shell写一个脚本,对文本中无序的一列数字排序

[root@server ~]# vim test.txt
[root@server ~]# vim sort.sh +
[root@server ~]# chmod 755  sort.sh
[root@server ~]# ./sort.sh
12
21
45
56
89
169
256
646
659
6595
7879
65965
[root@server ~]# cat sort.sh
#!/bin/bash
sort -n test.txt

金和网络

问题1:请用shell脚本写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符”shen”的文件名称

小米

问题1:
一个文本文件info.txt的内容如下:
aa,201
zz,502
bb,1
ee,42
每行都是按照逗号分隔,其中第二列都是数字,请对该文件按照第二列数字从大到小排列

[root@server ~]# sort  -t "," -k 2 -nr info.txt

美团

问题1:编写脚本实现以下功能;
每天早上5点开始做备份
要备份的是/var/mylog里所有文件和目录可以压缩进行备份
备份可以保存到别一台器上192.168.1.2 FTP帐号 aaa 密码 bbb
要求每天的备份文件要带有当天的日期标记

[root@server ~]# vim bak.sh

#!/bin/bash
bakfir=log
date='date +%F'
cd /var
tar zcf ${bakdir}_${date}.tar.gz ${bakdir}
sleep 1
ftp -n <<- EOF
open 192.168.142.129 #远程ftp服务器IP
user aaa bbb
put mylog_*.tar.gz
bye
EOF

[root@server ~]# vim /etc/crontab
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
00 05 * * * /bin/bash /root/bak.sh

问题2:请用shell脚本创建一个组class、一组用户,用户名为stdX,X从01-30,并归属class组

#!/bin/bash
id -g class &>/dev/null || groupadd class
user=std
for i in {01..30}
do
id -u ${user}$i &>/dev/null || useradd -G class ${user}$i
done

百度

· ## 处理以下文件内容,将域名取出并进行计数排序,如处理:

http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://www.sina.com.cn/head/www20021123am.shtml
http://www.sina.com.cn/head/www20041223am.shtml

cut -d'/' -f3 test.txt | sort | uniq -c | sort -nr

奇虎360

1、写一个脚本查找最后创建时间是3天前,后缀是*.log的文件并删除。**

find / -name “*.log” -ctime +3 -exec rm -f {} \;

2、写一个脚本将某目录下大于100k的文件移动至/tmp下。

for i in `find /test -type f -size +100k`;do cd /test && mv $i /tmp;done

3、写一个脚本进行nginx日志统计,得到访问ip最多的前10个(nginx日志路
径:/home/logs/nginx/default/access.log

awk '{a[$1]++}END{for (j in a) print a[j],j}' /home/logs/nginx/default/access.log|sort -nr|head

4、写一个脚本把指定文件里的/usr/local替换为别的目录。

sed 's:/user/local:/tmp:g' filename

滴滴出行

1、指令:ls | grep “[ad]*.conf” 命令解释正确的是:
正确答案: B
A 显示包含a 或者d 为开头,后接任何字符,再后面是.conf字符的文件(或目录)
B 显示包含a 或者d 出现0 次或无数次,后面是.conf字符的文件(或目录)
C 显示包含字母a 或者d出现0次或1次,后面是.conf字符的文件(或目录)
D 显示从字母a 到d ,后接任何字符,再后面是.conf字符的文件(或目录)
2、找出IO重定向执行结果与其他三个不同的:
正确答案: B
A ./run.sh >run.log 2>&1;
B ./run.sh 2>&1 >run.log;
C ./run.sh &>run.log;
D ./run.sh 2>run.log >&2
3、一个文件,大概1亿行,每行一个ip,将出现次数最多的top10输出到一个新的文件中

awk -F" " '{IP[$1]++}END{fo
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MIT 6.828 Homework: Xv6 System .. 下一篇用bpftrace窃取Mysql账号密码

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目