1、读取控制台输入的参数
语法: read (参数) (参数)
参数 | 描述 |
---|---|
-t | 在规定的时间内(秒) |
-p | 取值时的变量名 |
[root@tom shell]# read -t 10 -p "输入名字在10秒内" name
输入名字在10秒内tom
[root@tom shell]# echo $name
tom
2、函数
1.系统内置的函数
basename [string / pathname] [suffix]
功能描述:basename命令会删掉所有的前缀包括最后一个(‘/’)字符,然后将字符串显示出来。
suffix为后缀,如果suffix被指定了,basename会将pathname或string中的suffix去掉。
[root@tom shell]# basename /root/shell/shell_arr.sh .sh
shell_arr
dirname 文件绝对路径
功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分)
[root@tom shell]# dirname /root/shell/shell_arr.sh
/root/shell
2.自定义函数
[root@tom shell]# vim shell_fun.sh
#!/bin/bash
:<<!
自定义函数
!
function sum(){
s=0
s=$[$1+$2]
echo $s
return 2
}
sum $1 $2