shell~练习小结

1:如何获取主机IP?

方法一:awk:

 [root@server ~]# ifconfig eth0 | grep "inet " | awk '{print $2}'
172.25.254.100

方法二:cut

[root@server ~]# ifconfig eth0 | grep "inet " | cut -d " " -f 10
172.25.254.100

2:检测网络

[root@server ~]# vim ping.sh
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
[root@server ~]# sh ping.sh 100
172.25.254.100 is up

3:找出/mnt目录中的文件大小最大的:

[root@server mnt]# ll /mnt
total 4
drwx------. 2 root root 23 Dec 22 06:42 ssh-o63BsvHdpEJR
drwx------. 3 root root 16 Dec 22 06:40 systemd-private-Bsnm8D
drwx------. 3 root root 16 Dec 9 07:54 systemd-private-knpjXn
drwx------. 3 root root 16 Dec 9 07:52 systemd-private-LpoUQZ
drwx------. 3 root root 16 Dec 22 06:42 systemd-private-OmwKO5
drwx------. 3 root root 16 Dec 22 06:40 systemd-private-OWrNwD
drwx------. 3 root root 16 Dec 9 07:52 systemd-private-xoctWX
-rw-------. 1 root root 563 Dec 22 06:42 systemd-private-aksNwoX

第一种:

[root@server mnt]# ll /tmp | sort -nk 5 | cut -d " " -f 9 | tail -n 1
systemd-private-aksNwoX

第二种:

[root@server mnt]# ls -Sl /tmp/ | head -2 | cut -d " " -f 9
systemd-private-aksNwoX

4:判断输入的数字是否在10以内

1:输入是否为空
2:是否在10以内
3:0< a &lt; 10 − − &gt; y e s 4 : a&lt;10 --&gt; yes 4: a<10>yes4a<1 $a>10 --> no

#!/bin/bash
[ -z "$1" ] && {
    echo "please input a number!"
    exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ] && {
    echo "YES"
}||{
    echo "NO"
}

5:判断文件类型

#!/bin/bash
[ -z "$1" ] && {
    echo "please enter the file name!"
    exit 1
}
[ -e "$1" ] || {
        echo "This file does not exist!"
        exit 1
} 

[ -f "$1" ] && {
        echo "The file is a normal file."
} 
[ -S "$1" ] && {
        echo "The file is a socket."
} 
[ -c "$1" ] && {
        echo "The file is a character device.0"
}  
[ -L "$1" ] && {
        echo "The file is a soft link"
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值