Shell 函数 和 Shell echo命令 和 test命令

简介:

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

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

[ function ] funname [()]

{

    action;

    [return int;]

}

说明:

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


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

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

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

输出结果:

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

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

#!/bin/bash
funWithReturn(){
    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))
}
funWithReturn
echo "The sum of two numbers is $? !"

输出类似下面:

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

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

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


函数参数

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

带参数的函数示例:

#!/bin/bash
funWithParam(){
    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.sh
OK                     #标准输入
OK It is a test        #输出

4.显示换行

  echo "It it a test"

输出结果:

OK!

It it a test

5.显示不换行

#!/bin/sh

echo "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=100
num2=100
if 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=100
num2=100
if test num1=num2
then
    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 /bin
if test -e ./bash
then
    echo 'The file already exists!'
else
    echo 'The file does not exists!'
fi

输出结果:

The file already exists!

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


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


cd /bin
if test -e ./notFile -o ./bash
then
    echo 'One file exists at least!'
else
    echo 'Both dose not exists!'
fi

输出结果:

One file exists at least!









本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1545141,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
Shell Linux 程序员
【Linux】Shell 命令以及运行原理
【Linux】Shell 命令以及运行原理
|
17天前
|
存储 Shell 数据安全/隐私保护
Shell 内建命令:Shell 的内在魔力
Shell 内建命令比外部命令执行快,不需额外进程。`type` 命令用来检查命令类型。内建命令如 `cd`、`alias` 和 `echo` 直接在 Shell 中执行,不涉及磁盘 I/O。`type` 示例展示了 `cd` 是内建的,`ifconfig` 是外部的。`bash` 包含多种内建命令,如 `cd` 用于切换目录,`alias` 定义别名,`read` 从输入读取数据。`echo` 默认加换行,`echo -n` 可避免。
21 5
|
16天前
|
Shell 虚拟化
分布式系统详解--框架(Zookeeper-基本shell命令)
分布式系统详解--框架(Zookeeper-基本shell命令)
15 1
|
19天前
|
安全 Shell Linux
探索Linux命令chsh:更改用户的默认shell
`chsh`是Linux命令,用于更改用户的默认登录shell。它涉及用户环境配置和系统安全,允许用户选择更适合自己的shell以提升效率。命令有交互式选项和参数如`-s`来指定新shell。在使用时要注意新shell的可执行性、权限问题及选择合适的shell。例如,要更改为bash,用户可运行`chsh`后按提示操作,而root用户能用`sudo chsh -s /bin/zsh john`为用户`john`设定zsh。在更改前,确认shell路径、权限,并了解不同shell的特点。
|
18天前
|
Shell 开发者
Shell 函数深入解析与实践
了解 Shell 函数的基础,包括定义、参数传递及返回值。函数定义有多种语法,如 `function func() {...}` 或 `func() {...}`。参数通过 `$1`, `$2` 等访问,`$@` 代表所有参数。`return` 用于返回退出状态码(0-255),非数值数据需用 `echo`。正确获取函数返回值应立即检查 `$?`,例如:`result=$?`。实践中不断探索和学习!
11 1
|
4天前
|
Shell Linux
Linux环境变量之shell中export定义全局变量和echo 变量的区别
Linux环境变量之shell中export定义全局变量和echo 变量的区别
|
2月前
|
存储 算法 安全
shell 脚本之 函数与数组
shell 脚本之 函数与数组
|
16天前
|
分布式计算 Hadoop Shell
分布式系统详解--框架(Hadoop-基本shell命令)
分布式系统详解--框架(Hadoop-基本shell命令)
12 0
|
20天前
|
Shell
Shell [[]] 命令:条件判断的升级版
`[[ ]]` 是 Bash 中增强型的条件判断命令,提供比 `[ ]` 更多的灵活性,如无需引号包裹变量、直接字符串比较及正则支持。例如: ```markdown - 不加引号比较:`[[ -z $str1 ]]` - 逻辑运算:`[[ -z $str1 ]] || [[ -z $str2 ]]` - 正则匹配:`[[ $phone =~ ^1[0-9]{10}$ ]]` ``` 它不支持 `-a` 和 `-o`,但能用 `&&` 和 `||` 进行逻辑操作。用于复杂字符串验证和文件处理时非常有用。
18 0
|
20天前
|
Shell Windows
Shell test [] 命令:条件判断的艺术
`test` 命令在Shell脚本中用于条件检测,涉及数值、字符串和文件比较。例如,`test $a -eq $b` 检查两个数是否相等;`-e` 检查文件是否存在;`-w` 检查文件是否可写。数值比较不支持 `>=` 和 `<=`,需用 `-ge` 和 `-le`。字符串比较时注意空值,使用双引号。逻辑运算包括 `-a`(与)、`-o`(或)和 `!`(非)。文件类型和权限检测也是`test`的重要用途。
12 0