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
[root@tom shell]# ./shell_fun.sh 3 4
7
[root@tom shell]# echo $?
2
1.必须在调用函数地方之前,先声明函数,shell脚本是逐行运行。不会像其它语言一样先编译。
2.函数返回值,只能通过$?系统变量获得,可以在脚本中加上return int 返回,如果不加,将以最后一条命令运行结果,作为返回值。return后跟数值n(0-255)
3、shell工具
1、cut
cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。
语法:cut (参数) 文件名
参数 | 描述 |
---|---|
-f | 列号,提取第几列 |
-d | 分隔符 ,按照指定分隔符分割列 |
-e | 指定具体的字符 |
cut.txt
[root@tom shell]# vim cut.txt
dong shen
guan zhen
wo wo
lai lai
le le
需求:切割第一列
[root@tom shell]# cut -d " " -f 1 cut.txt
dong
guan
wo
lai
le
需求:切割第二、三列
[root@tom shell]# cut -d " " -f 2-3 cut.txt
shen
zhen
wo
lai
le
需求:guan切割出来
[root@tom shell]# cat cut.txt | grep "guan" | cut -d " " -f 1
guan
来源:CSDN
作者:逆风向上
链接:https://blog.csdn.net/qq_45563305/article/details/103569831