首页 > 系统相关 >Linux case语句用法

Linux case语句用法

时间:2022-08-20 23:34:27浏览次数:49  
标签:case 语句 libin3 echo student Linux libin root

case 命令
case语句与if-then-else语句的区别。

例1:if-then-else语句,比较繁琐
[19:37:32 root@libin3 libin]# usermod -G root student
[19:22:33 root@libin3 libin]# vim shell55
[19:37:32 root@libin3 libin]# cat shell55 
#!/bin/bash
# look for a value
#
if [ $USER = root ]
then echo "the user is root"
elif [ $USER = student ]
then echo "the user is student"
elif [ $USER = rhce ]
then echo "the user is student"
elif [ $USER =  helloword ]
then echo "the user is helloword"
else 
echo "sorry,no a user exist"
fi

[19:26:24 root@libin3 libin]# chmod u+x shell55
[19:26:31 root@libin3 libin]# ./shell55
the user is root
[student@libin3 libin]$ sudo chmod g+x shell55
[student@libin3 libin]$ ./shell55
the user is student

case命令,就不需要写elif语句检查同一个变量值。case命令会采用下面的列表格式来检查单个变量的多个值。

格式:
case variable in 
pattern1 | pattern2) commands1;; 
pattern3) commands2;; 
*) default commands;; 
esac 
case命令会将指定的变量与不同模式进行比较。如果变量和模式是匹配的,那么shell会执行 
为该模式指定的命令。通过 | 操作符在一行中分隔出多各模式。 * 号会捕获所有与已知模式不匹配的值,就相当于else。

例2:将例1的语句转换成case语句
示例1:
[19:48:36 root@libin3 libin]# vim shell56
#!/bin/bash
#using the case command
#
case $USER in
root | student)
echo "the is $USER";;
rhce)
echo "the is rhce";;
student)
echo "the is student";;
helloword)
echo "the is helloword";;
*)
echo "no is user";;
esac

[19:52:16 root@libin3 libin]# chmod +x shell56
[19:53:11 root@libin3 libin]# ./shell56
the is root

示例2:
[19:56:00 root@libin3 libin]# su - student
[student@libin3 libin]$ sudo chmod g+x /libin/libin/shell56
[student@libin3 libin]$ ./shell56
the is student

例3:这里我们可以得出case语句判断出了libin3.com 为主机名 
[21:11:43 root@libin3 libin]# echo $HOSTNAME
libin3.com
[21:11:45 root@libin3 libin]# vim shell57
#!/bin/bash
# using the case command
#
case $HOSTNAME in
libin2.com | libin4.com )
echo "the is $HOSTNAME";;
libin5.com | libin3.com )
echo "the hostname is libin5.com or $HOSTNAME";;
libin6.com)
echo "the hostname is libin6.com";;
*)
echo "no HOSTNAME";;
esac

[21:04:47 root@libin3 libin]# chmod u+x shell57
[21:05:02 root@libin3 libin]# ./shell57
the hostname is libin5.com or libin3.com

  

标签:case,语句,libin3,echo,student,Linux,libin,root
From: https://www.cnblogs.com/libin-linux/p/16609062.html

相关文章

  • linux通配符
    linux通配符通配符就是键盘上的一些特殊符号,可以实现特殊的功能,例如模糊搜索一些文件常见通配符符号作用*匹配任意,0或多个字符,字符串?匹配任意一个字符,有......
  • 语句
    if...elseswitchvarnum=1;switch(num){ case0:console.log(0); case1:console.log(1); case2:console.log(2); case3:console.log(3);}//打印3,因为没......
  • Linux驱动开发十四.使用内核自带的LED驱动
    回顾一下我们现在先后都做了几种LED的点亮试验:裸机点亮LED使用汇编语言读写寄存器点亮LED使用C语言读写寄存器点亮LED在系统下直接操作寄存器映射点亮LED在设备树下......
  • Linux常用命令
    1,文件和目录cd/home进入‘/home'目录'cd…返回上一级目录cd…/…返回上两级目录cd进入个人的主目录cd~user1进入个人的主目录cd-返回上次所在的目......
  • Linux安装redis及入门
    1.获取redis资源wgethttp://download.redis.io/releases/redis-4.0.8.tar.gz 2.解压tar-xzvfredis-4.0.8.tar.gz 3.安装进入到redis目录:cdredis-4.0.8......
  • Linux安装jdk
     1,导入插件JDK依赖包yuminstallglibc.i686 2,卸载系统自带的OpenJDK以及相关的java文件①在命令窗口键入:java-version②在命令窗口键入:rpm-qa|grep......
  • 分析lvgl的代码启动过程,对比esp32,stm32,linux
    lvgl是gui层负责绘制gui并根据输入设备的事件来响应重绘,然后把绘制的缓冲区发送给显示驱动去实际显示。以下代码参考lvglarduino官方例程,guiguider模拟器例程,,零知stm3......
  • 数据库sql语句
    数据库sql语句数据库相关:查所有数据库showdatabases;创建数据库createdatabasedb1;查看数据库showcreatedatabasedb1;创建数据库指定字符集createdatabase......
  • 备份软件Veritas NetBackup(NBU)8.1.1的Linux客户端部署
    企业级备份软件VeritasNetBackup(NBU)8.1.1的Linux客户端部署原创 yuanfan2012 WalkingCloud 2022-06-1300:00 发表于浙江收录于合集#Linux学习笔记268个#C......
  • Linux系统中/var/log下常见日志文件详解
    1)/var/log/secure记录登录系统存取数据的文件2)/var/log/message几乎所有的开机系统发生的错误都会在此记录3)/var/log/maillog记录邮件的存取和往来4)/var/log/cron记......