1.字符串包含子串
# 使用方法1
[root@vm1 zk]# [[ "aaabb" =~ "bb" ]] && echo "包含" || echo "不包含"
包含
[root@vm1 zk]#
[root@vm1 zk]# [[ "aaab" =~ "bb" ]] && echo "包含" || echo "不包含"
不包含
# 使用方法2
[root@vm1 zk]# pswd="123"
[root@vm1 zk]#
[root@vm1 zk]# [[ "$pswd" =~ [a-z] ]] && echo "有字母" || echo "没有字母"
没有字母
[root@vm1 zk]#
[root@vm1 zk]# pswd="123m"
[root@vm1 zk]#
[root@vm1 zk]# [[ "$pswd" =~ [a-z] ]] && echo "有字母" || echo "没有字母"
有字母
2.显示多少行
[root@stream9 ~]# cat demo.sh
# 111
# 2222
echo 123
echo 456
echo 789
# 这是注释
[root@stream9 ~]# cat demo.sh | wc -l
13
[root@stream9 ~]#
[root@stream9 ~]# cat -n demo.sh
1 # 111
2
3 # 2222
4 echo 123
5 echo 456
6
7 echo 789
8
9
10
11 # 这是注释
12
13
# 行数
[root@stream9 ~]# wc -l demo.sh
13 demo.sh
# word 单词,默认空格分割
[root@stream9 ~]# wc -w demo.sh
12 demo.sh
# bytes 字节数
[root@stream9 ~]# wc -c demo.sh
62 demo.sh
# 综合
[root@stream9 ~]# wc -lcw demo.sh
13 12 62 demo.sh
3. tr => transform 转换
# 用法1
[root@stream9 ~]# echo "abc" | tr "[a-z]" "[A-Z]"
ABC
# 用法2
[root@stream9 ~]# cat demo1.sh
#!/bin/bash
echo "hello world"
[root@stream9 ~]#
[root@stream9 ~]# cat demo1.sh | tr "[a-z]" "[A-Z]"
#!/BIN/BASH
ECHO "HELLO WORLD"
# 用法3
[root@stream9 ~]# ls
demo1.sh demo.sh
[root@stream9 ~]# tr "[a-z]" "[A-Z]" <demo1.sh
#!/BIN/BASH
ECHO "HELLO WORLD"
# 用法4
[root@stream9 ~]# cat demo2.sh
aaa
bbb
ccc
ddd
[root@stream9 ~]#
[root@stream9 ~]# cat demo2.sh | tr '\n' ' '
aaa bbb ccc ddd [root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# cat demo2.sh | tr '\n' '\t'
aaa bbb ccc ddd [root@stream9 ~]#
[root@stream9 ~]#
# 用法5 处理字符串
[root@stream9 ~]# arr="aa bb cc dd"
[root@stream9 ~]# echo $arr
aa bb cc dd
[root@stream9 ~]# echo $arr | tr ' ' '\n'
aa
bb
cc
dd
[root@stream9 ~]#
[root@stream9 ~]# for i in `echo $arr | tr ' ' '\n'`
> do
> echo "是: $i"
> done
是: aa
是: bb
是: cc
是: dd
# 用法6
[root@stream9 ~]# echo "11abc789" | tr "[1-9]" "Z"
ZZabcZZZ
# 用法7
[root@stream9 ~]# echo "aa222bb" | tr -d "222"
aabb
4.关于 $ 的变量
# 1. $? => 上一条命令是否执行成功
[root@stream9 ~]# echo 123
123
[root@stream9 ~]# echo $?
0
[root@stream9 ~]#
[root@stream9 ~]# wfowehfweof
-bash: wfowehfweof: command not found
[root@stream9 ~]# echo $?
127
# 2. 其他
[root@stream9 ~]# cat demo.sh
#!/bin/bash
echo $1
echo $2
echo $@ # 列出所有参数
echo $# # 参数的数量或者长度
echo $$ # demo.sh 脚本执行是,该脚本的进程ID
echo $0 # 打印当前脚本名称
[root@stream9 ~]#
[root@stream9 ~]# bash demo.sh aa bb cc
aa
bb
aa bb cc
3
1628
demo.sh
# 3. 给脚本添加说明文档
[root@stream9 ~]# cat demo.sh
#!/bin/bash
if [ $# -eq 0 ];then
echo "此脚本$0: "
echo "Usage: xx [OPTION]... [-T] SOURCE DEST"
echo " -h,--help 这是帮助"
echo " -c,--cccc 这是cccc"
echo " -x,--xxxx 这xxx"
exit 1
fi
echo $1
echo $2
echo $@
echo $#
echo $$
echo $0
echo $*
[root@stream9 ~]#
[root@stream9 ~]# bash demo.sh
此脚本demo.sh:
Usage: xx [OPTION]... [-T] SOURCE DEST
-h,--help 这是帮助
-c,--cccc 这是cccc
-x,--xxxx 这xxx
标签:demo,03,18,echo,sh,常用命令,root,stream9,vm1
From: https://www.cnblogs.com/ckh2023/p/17771007.html