面试shell常见问题(内含shell相关文档)

背景

运维过程经常会使用shell获取数据或者做些自动化任务,当然也是面试运维工程师常常被问到的问题,为了方便记忆,我也整理了部分shell常问的知识点。

更多资料获取

在这里插入图片描述
文章内容有限,更多shell相关资料获取如下
*链接:https://pan.baidu.com/s/1jud_edmbmekHKYZNxIC0vw
提取码: s8je
提取码过期请关注公众号回复shell重新获取:

在这里插入图片描述

常见问题

1. 解释下$*和$@ 有什么区别,分别使用在什么场景?

$*和$@ 在不加双引号的时候作用类似,都可以通过循环遍历出每个参数。
$*和$@ 在加双引号的时候作用不一样,$*会将传递的参数看成一个整体,循环遍历的时候也是一个整体字符串;$@则会将传递参数通过空格分隔,可以通过循环遍历每个参数。
**场景:**当需要传递参数作为一个整体的时候可以使用"$*",如果需要分别获取每个参数的时候使用“$@”

[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ./get_info.sh  1 2 3
不加引号=====$*
1 2 3
1
2
3
不加引号=====$@
1 2 3
1
2
3
加上引号====='$*'
1 2 3
1 2 3
加上引号====='$@'
1 2 3
1
2
3

2. 如何查找文件夹下所有包含某个字符串的文件?

第一印象应该使用grep,最终查找的是文件,所以这里要使用到递归,grep 有个参数选项-r 就是递归查找某个字符串所在的文件。下面介绍下grep的option含义
场景: 常常用于查找相关日志或者帮助信息。

-i : 不区分查找字符串的大小写
-r:递归查找文件夹中包含某字符串的文件信息及所在行
-l: 只显示匹配到的文件名,不包含文件内容
[root@iZbp1ihrdhzp9kwn7g94jiZ ~]# grep -r "echo" scripts/
scripts/get_info.sh:echo "不加引号=====\$*"
scripts/get_info.sh:echo $*
scripts/get_info.sh:    do echo "$i"
scripts/get_info.sh:echo "不加引号=====\$@"
scripts/get_info.sh:echo $@
scripts/get_info.sh:    do echo "$i"
scripts/get_info.sh:echo "加上引号====='\$*'"
scripts/get_info.sh:echo "$*"
scripts/get_info.sh:    do echo "$i"
scripts/get_info.sh:echo "加上引号====='\$@'"
scripts/get_info.sh:echo "$@"
scripts/get_info.sh:    do echo "$i"
[root@iZbp1ihrdhzp9kwn7g94jiZ ~]# grep -rl "echo" scripts/
scripts/get_info.sh

3. shell 中$! 有什么作用?

$! 显示的运行该程序的进程号id
**场景:**一般通过获取pid用于监控后台进程的运行状态

[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ./get_info.sh
346820
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ps -ef | grep 346820
root      346820       1  0 16:05 pts/0    00:00:00 sleep 20

4. var=value和export赋值变量有什么区别?

var-value和export变量赋值主要区别是作用域不同。
使用export进行变量赋值,该变量在该shell以及子进程中都可以访问到,是共享的。
var=vaule变量赋值,该变量不能够在子进程中共享。
**场景:**理解了变量的作用范围才能更好的声明变量,才能编写能够正常交互的脚本和命令行程序。

[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# a="xiaomi"
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# export b="su7"
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# echo "$a,$b"
xiaomi,su7
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ./get_info.sh
打印=====$a,$b
,su7

5. 如何调试shell脚本?

日常写shell脚本时大都使用的vim直接编写,没有像python、java等强大的代码编写工具等友好的提示,所以没写完一段代码都要运行调试。
常用的调试方式有以下几种:

  • 使用-x 查看脚本的执行流程和变量的变化,也可以结合set在脚本内进行使用set -x是开始调试 set +x是结束调试
#   执行脚本时-x 打印的调试信息
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# cat get_info.sh
echo "打印=====\$a,\$b"
echo "$a,$b"
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# bash -x get_info.sh
+ echo '打印=====$a,$b'
打印=====$a,$b
+ echo ,su7
,su7
#  在脚本内对指定代码进行调试
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# cat get_info.sh
echo "打印=====\$a,\$b"
set -x
echo "$a,$b"
set +x
echo "end"
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ./get_info.sh
打印=====$a,$b
+ echo ,su7
,su7
+ set +x
end
  • 使用trap和DEBUG对执行到指定的代码前执行特定的命令
[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# cat  get_info.sh
result=`/usr/sbin/ping www.baidu.com -c 1`
echo "打印=====\$a,\$b"
trap 'echo "executing: $result"' DEBUG
echo "$a,$b"
echo "end"

[root@iZbp1ihrdhzp9kwn7g94jiZ scripts]# ./get_info.sh
打印=====$a,$b
executing: PING www.a.shifen.com (180.101.50.242) 56(84) bytes of data.
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=1 ttl=50 time=15.2 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 15.247/15.247/15.247/0.000 ms
,su7
executing: PING www.a.shifen.com (180.101.50.242) 56(84) bytes of data.
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=1 ttl=50 time=15.2 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 15.247/15.247/15.247/0.000 ms
end

  • 使用-v主要查看脚本的执行的流程而不关注执行结果
主要用于查看执行的顺序以及打印执行后的结果
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据工匠大壮

请狠狠粗暴的爱我!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值