Shell脚本判断用户的输入内容 您所在的位置:网站首页 linux获取用户输入 Shell脚本判断用户的输入内容

Shell脚本判断用户的输入内容

2023-07-18 09:50| 来源: 网络整理| 查看: 265

1.脚本功能:提示客户属于一串数字,并判断用户是否进行了输入,输入的是否为数字

#!/bin/bash read -p "enter a number: " number #提示用户输入数字 if [ -z $number ];then #判断用户是否输入,如果未输入则打印error echo "Error" exit else jieguo=`echo "$number*1" | bc ` #把用户的输入值和1相乘,交给bc做运算 if [ $jieguo -eq 0 ];then #判断计算结果是否为0,为0则说明number非数字(字符串和0相乘结果为0) echo "not a number" exit fi fi

Shell脚本中判断输入变量或者参数是否为空的方法,下面总结了5种方法,并分别给出了代码实例,需要的朋友可以参考下

1.判断变量

代码如下:

read -p "input a word :" word if [ ! -n "$word" ] ;then echo "you have not input a word!" else echo "the word you input is $word" fi

2.判断输入参数

代码如下:

#!/bin/bash if [ ! -n "$1" ] ;then echo "you have not input a word!" else echo "the word you input is $1" fi

以下未验证。

3. 直接通过变量判断

如下所示:得到的结果为: IS NULL

代码如下:

#!/bin/sh para1= if [ ! $para1 ]; then echo "IS NULL" else echo "NOT NULL" fi

4. 使用test判断

得到的结果就是: dmin is not set! 

代码如下:

#!/bin/sh dmin= if test -z "$dmin" then echo "dmin is not set!" else echo "dmin is set !" fi

5. 使用""判断

代码如下:

#!/bin/sh dmin= if [ "$dmin" = "" ] then echo "dmin is not set!" else echo "dmin is set !" fi

下面是我在某项目中写的一点脚本代码, 用在系统启动时:

代码如下:

#! /bin/bash echo "Input Param Is [$1]" if [ ! -n "$1" ] ;then echo "you have not input a null word!" ./app1;./app12;./app123 elif [ $1 -eq 2 ];then ./app12;./app123 elif [ $1 -eq 90 ];then echo "yy"; fi

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:使用shell脚本每天对MySQL多个数据库自动备份的讲解shell脚本操作mysql数据库删除重复的数据shell脚本批量删除es索引的方法Java调用shell脚本解决传参和权限问题的方法springboot打包不同环境配置以及shell脚本部署的方法Shell中的while循环几种使用实例详解Linux Shell在目录下使用for循环结合if查找文件的巧用Shell脚本中使用getopts处理多命令行选项Shell脚本从文件中逐行读取内容的几种方法实例shell脚本实现监控某个进程意外停止后拉起进程


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有