shell常用命令面试总结(脚本格式、函数运用、流程控制if、for、while)(5)

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

请输入ip地址:www.baidu.com
主机百度在线


### ✨案例2:实现只能输入y/n;输入密码不可见



#!/bin/bash

author:kobe

#date:2022-05-29
#defcription: 实现只能输入y/n;输入密码不可见

read -n1 -p "请输入y/n: "
echo
read -s -p “请输入密码:” password
echo
echo $REPLY
echo $password


**🌟测试**



zhilong@zhilong-virtual-machine:~/Desktop/shell_text/02shell$ bash read.sh
请输入y/n: y
请输入密码:
y
666666


## 三、流程控制


### 🌙1、if条件语句


**🌟方式一:**



if [条件1]
then
指令1
elif [条件2]
then
指令2
else
指令3
fi


**🌟方式二:**



if [条件1]; then
指令1
elif [条件2]; then
指令2
else
指令3
fi


### ✨案例3:



#!/bin/bash
#author: kobe
#date: 2022-5-29
#desc: if条件表达式

read -p “请输入路径(如:/etc/passwd):”

if [ -f “ R E P L Y " ] ; t h e n e c h o " {REPLY}" ];then echo " REPLY"];thenecho"{REPLY}是常规文件”
elif [ -d “ R E P L Y " ] ; t h e n e c h o " {REPLY}" ];then echo " REPLY"];thenecho"{REPLY}是目录”
else
echo “${REPLY}是其他文件”
fi


### 🌙2、for循环


**🌟格式一:**



for 值 in 列表
do
执行语句
done


**🌟格式二:**



max=0
for ((i=1;i<=10;i++))
do
echo “$i”
done


### ✨案例4、打印1到10值



for ((i=1;i<=10;i++))
do
echo $i
done


### ✨案例5、打印根目录下所有的目录和文件



index=1
for item in ( l s / ) d o e c h o " (ls /) do echo " (ls/)doecho"{index}“.” i t e m " i n d e x = {item}" index= item"index=(($index+1))
done


执行结果



1.bin
2.boot
3.cdrom
4.dev
5.etc
6.home
7.lib
8.lib32
9.lib64
10.libx32
11.lost+found
12.media
13.mnt
14.opt
15.proc
16.root
17.run
18.sbin
19.snap
20.srv
21.swapfile
22.sys
23.tmp
24.usr
25.var


### ✨案例6、打印etc下所有以.con结尾的文件`ls /etc/*.conf`



index=1
for item in KaTeX parse error: Undefined control sequence: \* at position 10: {ls /etc/\̲*̲.conf} do echo…index"." i t e m " i n d e x = item" index= item"index=(($index+1))
done


**🌟执行脚本**



1./etc/adduser.conf
2./etc/apg.conf
3./etc/appstream.conf
4./etc/brltty.conf
5./etc/ca-certificates.conf
6./etc/debconf.conf
7./etc/deluser.conf
8./etc/e2scrub.conf
9./etc/fprintd.conf
10./etc/fuse.conf
11./etc/gai.conf
12./etc/hdparm.conf
13./etc/host.conf
14./etc/kernel-img.conf
15./etc/kerneloops.conf
16./etc/ld.so.conf
17./etc/libao.conf
18./etc/libaudit.conf
19./etc/logrotate.conf
20./etc/ltrace.conf
21./etc/mke2fs.conf
22./etc/mtools.conf
23./etc/nsswitch.conf
24./etc/pam.conf
25./etc/pnm2ppa.conf
26./etc/popularity-contest.conf
27./etc/resolv.conf
28./etc/rsyslog.conf
29./etc/rygel.conf
30./etc/sensors3.conf
31./etc/sysctl.conf
32./etc/ucf.conf
33./etc/usb_modeswitch.conf
34./etc/xattr.conf


### 🌙3、while循环


只要条件满足,就一直循环  
 **🌟格式:**



while 条件
do
执行语句
done

while 条件; do
执行语句
done


### ✨案例7:判断主机是否可以ping通



while read hostname ip; do
ping -c1 -w1 “$ip” &> /dev/null
if [ ? − e q 0 ] ; t h e n e c h o " 主机 ? -eq 0 ]; then echo "主机 ?eq0];thenecho"主机{ip}在线"

    else
            echo "主机${ip}不在线"
    fi

done < /home/zhilong/Desktop/shell_text/02shell/server.txt


**🌟执行结果:**



zhilong@zhilong-virtual-machine:~/Desktop/shell_text/02shell$ bash demo_while.sh
主机114.114.114.114在线
主机www.baidu.com在线
主机192.168.17.3不在线
主机192.168.17.129在线


### 🌙4、case语句


**🌟格式**



case 变量名 in
值1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

img-3G95QoKZ-1715688345946)]
[外链图片转存中…(img-gMgJsrIl-1715688345946)]
[外链图片转存中…(img-HbtidshJ-1715688345947)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值