Shell脚本中判断变量为数字的3种方法

时间:2023年09月15日

/

来源:好听的名字

/

编辑:本站小编

收藏本文

下载本文

下面是小编给大家带来的Shell脚本中判断变量为数字的3种方法,本文共4篇,以供大家参考,我们一起来看看吧!本文原稿由网友“好听的名字”提供。

篇1:Shell脚本中判断变量为数字的3种方法

这篇文章主要介绍了Shell脚本中判断变量为数字的3种方法,本文分别介绍了expr、sed两种方法,并给出了一个判断脚本,需要的朋友可以参考下

方法1:通过expr 计算变量与一个整数值相加,如果能正常执行则为整数,否则执行出错,$?将是非0的值

代码如下:

expr $args + 0 &>/dev/null

方法2:打印变量通过sed替换的方式,将变量中的数字替换为空,如果执行替换后变量为空,则为整数

代码如下:

echo $args | sed ‘s/[0-9]//g‘

如果判断负数则再用sed过滤负号

代码如下:

echo $args | sed ‘s/[0-9]//g‘ | sed ‘s/-//g‘

下面的脚本通过两个函数来实现数值判断,代码很简单,就不加注释了。

代码如下:

#!/bin/bash

usage{

cat <

USEAGE:sh $0args1 args2

exit 1

EOF

}

checkInt(){

expr $1+ 0&>/dev/null

[ $? -ne 0] && { echo “Args must be integer!”;exit 1; }

}

checkInt1(){

tmp=`echo $1|sed ‘s/[0-9]//g‘`

[ -n “${tmp}”]&& { echo “Args must be integer!”;exit 1; }

}

[ $# -ne 2]&&usage

args1=$1

args2=$2

checkInt $args1

checkInt1 $args2

if[ $args1 -gt $args2 ];then

echo “yes,$args1 greate than $args2”

else

echo “no,$args1 less than $args2”

fi

篇2:Shell脚本传递参数的3种方法比较

这篇文章主要介绍了Shell脚本传递参数的3种方法比较,本文直接给出代码示例,在代码中包含详细注解,需要的朋友可以参考下

#!/bin/bash#extracting command text_text_text_line options as parametershelp_info(){ echo “NAME” echo “\\t$0” echo “SYNOPSIS” echo “\\t$0 is a shell test about process options” echo “DESCRIPTION” echo “\\toption like -a -b param1 -c param2 -d”}if [ $# -lt 0 ]then help_infofinomal_opts_act(){ echo -e “\\n### nomal_opts_act ###\\n” while [ -n “$1” ] do case “$1” in -a)echo “Found the -a option”;; -b)echo “Found the -b option”echo “The parameter follow -b is $2” shift;; -c)echo “Found the -c option”echo “The parameter follow -c is $2”shift;; -d)echo “Found the -d option”;; *) echo “$1 is not an option”;; esac shift done}#用shell命令自建的选项解析,可以按照自己的想法实现#优点:自己定制,没有做不到,只有想不到#缺点:麻烦getopt_act(){ echo -e “\\n### getopt_act ###\\n” GETOPTOUT=`getopt ab:c:d “$@”` set -- $GETOPTOUT while [ -n “$1” ] do case $1 in -a)echo “Found the -a option”;; -b)echo “Found the -b option”echo “The parameter follow -b is ”$2“”shift;; -c)echo “Found the -c option”echo “The parameter follow -c is ”$2“”shift;; -d)echo “Found the -d option”;; --)shiftbreak;; *) echo “Unknow option: ”$1“”;; esac shift done param_index=1 for param in “$@” do echo “Parameter $param_index:$param” param_index=$[ $param_index + 1 ] done}#用getopt命令解析选项和参数#优点:相对与getopts来说是个半自动解析,自动组织选项和参数,用 -- 符号将选项与参数隔开#缺点:相对于getopts的缺点#1.需要与set -- 命令配合,不是必须,需要手动shift#2.选项参数中不支持空格如 -a -b dog -c “earth moon” -d -f param1 param2 就会解析错误getopts_act(){ echo -e “\\n### getopts_act ###\\n” while getopts :ab:c:d ARGS do case $ARGS in a)echo “Found the -a option”;; b)echo “Found the -b option”echo “The parameter follow -b is $OPTARG”;; c)echo “Found the -c option”echo “The parameter follow -c is $OPTARG”;; d)echo “Found the -d option”;; *) echo “Unknow option: $ARGS”;; esac done shift $[ $OPTIND -1 ] param_index=1 for param in “$@” do echo “Parameter $param_index:$param” param_index=$[ $param_index + 1 ] done}#getopts 命令解析选项和参数#优点:可在参数中包含空格如:-c “earth moon”#选项字母和参数值之间可以没有空格如:-bdog#可将未定义的选项绑定到?输出#Unknow option: ?nomal_opts_act -a -b dog -c earth -d -f param1 param2getopts_act -a -b dog -c “earth moon” -d -f param1 param2getopt_act -a -b dog -c earth -d -f param1 param2

篇3:Shell脚本计算字符串长度和判断字符串为空小技巧

这篇文章主要介绍了Shell脚本计算字符串长度和判断字符串为空小技巧,本文分别给出计算字符串长度和判断字符串为空各3种实现方法,需要的朋友可以参考下

一些需要注意的脚本问题

计算字符串长度可用的三种方法:

代码如下:

echo “$str”|awk ‘{print length($0)}‘

expr length “$str”

echo “$str”|wc -c

但是第三种得出的值会多1,可能是把结束符也计算在内了

判断字符串为空的方法有三种:

代码如下:

if [ “$str” = “” ]

if [ x“$str” = x ]

if [ -z “$str” ]

注意:都要代双引号,否则有些命令会报错,

Shell脚本计算字符串长度和判断字符串为空小技巧

篇4:Shell脚本中的位置变量参数(特殊字符)实例讲解

这篇文章主要介绍了Shell脚本中的位置变量参数(特殊字符)实例讲解,本文讲解了$#、$* 、$$ 、$!、$@、$-、$?等特殊字符的作用,并给出使用实例,比较清晰和简洁,需要的朋友可以参考下

$# : 传递到脚本的参数个数

$* : 以一个单字符串显示所有向脚本传递的参数,与位置变量不同,此选项参数可超过 9个

$$ : 脚本运行的当前进程 ID号

$! : 后台运行的最后一个进程的进程 ID号

$@ : 与$#相同,但是使用时加引号,并在引号中返回每个参数

$- : 显示shell使用的当前选项,与 set命令功能相同

$? : 显示最后命令的退出状态。 0表示没有错误,其他任何值表明有错误。

代码如下:

#!/bin/sh

#param.sh

# $0:文件完整路径名

echo “path of script. $0”

# 利用basename命令文件路径获取文件名

echo “name of script. $(basename $0)”

# $1:参数1

echo “parameter 1 : $1”

# $2:参数2

echo “parameter 2 : $2”

# $3:参数3

echo “parameter 3 : $3”

# $4:参数4

echo “parameter 4 : $4”

# $5:参数5

echo “parameter 5 : $5”

# $#:传递到脚本的参数个数

echo “The number of arguments passed : $#”

# $*:显示所有参数内容i

echo “Show all arguments : $*”

# $:脚本当前运行的ID号

echo “Process ID : $”

# $?:回传码

echo “errors : $?”

输入./param.sh hello world

代码如下:

[firefox@fire Shell]$ ./param.sh hello world

path of script. ./param.sh

name of script. param.sh

parameter 1 : hello

parameter 2 : world

parameter 3 :

parameter 4 :

parameter 5 :

The number of arguments passed : 2

Show all arguments : hello world

Process ID : 5181

errors : 0

下载Shell脚本中判断变量为数字的3种方法(集锦4篇)
Shell脚本中判断变量为数字的3种方法.doc
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
最新范文更多
    热门文章
      猜你喜欢
      点击下载本文文档