case 语句范例


[root@raspberry ~]# cat case.sh
#!/bin/bash
read -p "enter info(host,ip,mem,cpu): "  info

host(){
  hostname -s
}

ip(){
  hostname -i | awk '{print $2}'
}
mem(){
  free -mh
}
top(){
  ps aux | sort -k4nr | head -n 5
}

case  "$info"  in
  host )
    host ;;
  ip   )
    ip   ;;
  mem  )
    mem  ;;
  cpu  )
    top  ;;
  *    )
  echo "Only for (os,ip,mem,cpu)"!!! ;;
esac
[root@raspberry ~]# chmod +x case.sh
[root@raspberry ~]# ./case.sh
enter info(host,ip,mem,cpu): host
raspberry
[root@raspberry ~]# ./case.sh
enter info(host,ip,mem,cpu): ip
192.168.0.51
[root@raspberry ~]# ./case.sh mem
enter info(host,ip,mem,cpu): mem
              total        used        free      shared  buff/cache   available
Mem:           3.7G         73M        3.4G         16M        319M        3.6G
Swap:            0B          0B          0B
[root@raspberry ~]# ./case.sh
enter info(host,ip,mem,cpu): cpu
root       592  0.3  0.8 956604 33040 ?        Ssl  16:09   0:46 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
root       590  0.0  0.4  64540 16652 ?        Ssl  16:09   0:03 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
polkitd    257  0.0  0.2  85988  9828 ?        Ssl  16:09   0:00 /usr/lib/polkit-1/polkitd --no-debug
root       271  0.0  0.2  61012 11648 ?        Ssl  16:09   0:05 /usr/sbin/NetworkManager --no-daemon
root       871  0.2  0.2 932880 10384 ?        Ssl  16:09   0:26 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
[root@raspberry ~]#