博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell 函数 和 Shell echo命令 和 test命令
阅读量:2387 次
发布时间:2019-05-10

本文共 3943 字,大约阅读时间需要 13 分钟。

linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。

shell中函数的定义格式如下:

[ function ] funname [()]{    action;    [return int;]}

说明:

  • 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。


  • 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255

下面的例子定义了一个函数并进行调用:

#!/bin/bashdemoFun(){    echo "This is your first shell function!"}echo "Function begin..."helloecho "Function end!"

输出结果:

Function begin...This is your first shell function!Function end!

下面定义一个带有return语句的函数:

#!/bin/bashfunWithReturn(){    echo "The function is to get the sum of two numbers..."    echo -n "Input first number: "    read aNum    echo -n "Input another number: "    read anotherNum    echo "The two numbers are $aNum and $anotherNum !"    return $(($aNum+$anotherNum))}funWithReturnecho "The sum of two numbers is $? !"

输出类似下面:

The function is to get the sum of two numbers...Input first number: 25Input another number: 50The two numbers are 25 and 50 !The sum of two numbers is 75 !

函数返回值在调用该函数后通过 $? 来获得。

注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。


函数参数

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

带参数的函数示例:

#!/bin/bashfunWithParam(){    echo "The value of the first parameter is $1 !"    echo "The value of the second parameter is $2 !"    echo "The value of the tenth parameter is $10 !"    echo "The value of the tenth parameter is ${10} !"    echo "The value of the eleventh parameter is ${11} !"    echo "The amount of the parameters is $# !"    echo "The string of the parameters is $* !"}funWithParam 1 2 3 4 5 6 7 8 9 34 73

输出结果:

The value of the first parameter is 1 !The value of the second parameter is 2 !The value of the tenth parameter is 10 !The value of the tenth parameter is 34 !The value of the eleventh parameter is 73 !The amount of the parameters is 12 !The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

另外,还有几个特殊字符用来处理参数:

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$#相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。






Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出。命令格式:

echo string

您可以使用echo实现更复杂的输出格式控制。

1.显示普通字符串:

  echo "It is a test"

这里的双引号完全可以省略,以下命令与上面实例效果一致:

  echo It is a test

2.显示转义字符

  echo "\"It is a test\""

结果将是:

  "It is a test"

同样,双引号也可以省略

3.显示变量

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

  #!/bin/sh  read name   echo "$name It is a test"

以上代码保存为 test.sh,name 接收标准输入的变量,结果将是:

[root@www ~]# sh test.shOK                     #标准输入OK It is a test        #输出

4.显示换行

  echo "It it a test"

输出结果:

OK!It it a test

5.显示不换行

#!/bin/shecho "It is a test"

输出结果:

OK! It is a test

6.显示结果定向至文件

  echo "It is a test" > myfile

7.原样输出字符串,不进行转义或取变量(用单引号)

  echo '$name\"'

输出结果:

  $name\"

8.显示命令执行结果

  echo `date`

结果将显示当前日期

  Thu Jul 24 10:08:46 CST 2014




Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。


数值测试

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真

实例演示:

num1=100num2=100if test $[num1] -eq $[num2]then    echo 'The two numbers are equal!'else    echo 'The two numbers are not equal!'fi

输出结果:

The two numbers are equal!

字符串测试

参数 说明
= 等于则为真
!= 不相等则为真
-z 字符串 字符串长度伪则为真
-n 字符串 字符串长度不伪则为真

实例演示:

num1=100num2=100if test num1=num2then    echo 'The two strings are equal!'else    echo 'The two strings are not equal!'fi

输出结果:

The two strings are equal!

文件测试

参数 说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真

实例演示:

cd /binif test -e ./bashthen    echo 'The file already exists!'else    echo 'The file does not exists!'fi

输出结果:

The file already exists!

另外,Shell还提供了与( ! )、或( -o )、非( -a )三个逻辑操作符用于将测试条件连接起来,


其优先级为:"!"最高,"-a"次之,"-o"最低。例如:


cd /binif test -e ./notFile -o ./bashthen    echo 'One file exists at least!'else    echo 'Both dose not exists!'fi

输出结果:

One file exists at least!

转载地址:http://eisab.baihongyu.com/

你可能感兴趣的文章
oracle表分区详解
查看>>
从头做leetcode之leetcode 5 最长回文子串
查看>>
从头做leetcode之leetcode 6 Z字形变换
查看>>
将无符号偏移量添加到...溢出到...
查看>>
从头做leetcode之leetcode 11 盛最多水的容器
查看>>
设计模式、框架和架构的联系
查看>>
安装VMware虚拟机
查看>>
常用的设计模式和代码
查看>>
桥接模式-通俗的理解(转)
查看>>
MXML 文件中的xmlns是什么意思?
查看>>
Flex Builder 中的工作空间、项目
查看>>
Flex 获得远程数据
查看>>
Flex 添加效果的两种方法
查看>>
Flash Builder 4字体设置
查看>>
Actionscript 3.0 笔记一
查看>>
图像处理库OpenCV参考网址
查看>>
dllimport与dllexport作用与区别
查看>>
OpenGL坐标系
查看>>
C++用new和不用new创建类对象区别
查看>>
C++ C# JAVA 创建对象
查看>>