本文共 3943 字,大约阅读时间需要 13 分钟。
[ 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!
#!/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 !
#!/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 !"
参数处理 | 说明 |
---|---|
$# | 传递到脚本的参数个数 |
$* | 以一个单字符串显示所有向脚本传递的参数 |
$$ | 脚本运行的当前进程ID号 |
$! | 后台运行的最后一个进程的ID号 |
$@ | 与$#相同,但是使用时加引号,并在引号中返回每个参数。 |
$- | 显示Shell使用的当前选项,与set命令功能相同。 |
$? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
echo string
echo "It is a test"
echo It is a test
echo "\"It is a test\""
"It is a test"
#!/bin/sh read name echo "$name It is a test"
[root@www ~]# sh test.shOK #标准输入OK It is a test #输出
echo "It it a test"
OK!It it a test
#!/bin/shecho "It is a test"
OK! It is a test
echo "It is a test" > myfile
echo '$name\"'
$name\"
echo `date`
Thu Jul 24 10:08:46 CST 2014
参数 | 说明 |
---|---|
-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!
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/