Linux Bash编程:shell语法及实用技巧
在Linux Bash编程中,介绍了Bash编程基本语法,本文记录一下Bash编程中使用到的相关shell 语法技巧。
目录
字符串处理
在字符串中传递变量
$ num=8
# 方法1:双引号
$ text="There are $num ducks swimming in a pond"
$ echo $text
There are 8 ducks swimming in a pond
$
$ text="There are "$num" ducks swimming in a pond"
$ echo $text
There are 8 ducks swimming in a pond
# 方法2:单引号
$ text='There are $num ducks swimming in a pond'
$ echo $text
There are $num ducks swimming in a pond
$
$ text='There are '$num' ducks swimming in a pond'
$ echo $text
There are 8 ducks swimming in a pond
$ text="There are '$num' ducks swimming in a pond"
$ echo $text
There are '8' ducks swimming in a pond
tr命令
tr (translate) 命令用于删除或者转换字符,比如大小写转换,删除字符等。命令格式如下:
$ tr [OPTION] SET1 [SET2]
OPTION参数说明:
-c | --complement
:删除或者替换SET1以外的字符-d | --delete
:删除SET1以外的字符-s, --squeeze-repeats
:压缩连续重复的字符为单个字符-t, --truncate-set1
:截取 SET1 使之与 SET2 长度相等
1. 大小写转换
$ echo HELLO WORLD | tr "A-Z" "a-z"
hello world
$ echo HELLO WORLD | tr "[:upper:]" "[:lower:]"
hello world
$
$ echo hello world | tr "a-z" "A-Z"
HELLO WORLD
$ echo hello world | tr "[:lower:]" "[:upper:]"
HELLO WORLD
2. 替换
$ echo "(hello world)" | tr "()" "{}"
{hello world}
3. 压缩重复字符串
比如可以将多个连续空格压缩为一个空格
$ echo "hello world !" | tr -s [:space:] ' '
hello world !
4. 删除指定字符
$ echo "10MB" | tr -d MB
10
提取数字还可以这样:
$ echo "10MB" | tr -cd "[0-9]"
10
$ echo "10MB" | tr -cd [:digit:]
10
删除数字:
$ echo "10MB" | tr -d "[0-9]"
MB
$ echo "10MB" | tr -d [:digit:]
MB
字母字符用[:alpha:]
表示
判断字符串是否包含某个子串
1、使用通配符*
SIZE=10M
if [[ $SIZE == *M* ]]
then
echo "$SIZE include M"
fi
2、使用操作符~
if [[ $SIZE =~ M ]]
then
echo "$SIZE include M"
fi
3、利用grep查找
SIZE=10Mb
result=`echo $SIZE | grep "Mb"`
if [ "$result" != "" ]
then
echo "$SIZE include Mb"
fi
4、利用grep查找,不区分大小写
SIZE=10Mb
result=`echo $SIZE | grep -i "mb"`
if [ "$result" != "" ]
then
echo "The units are correct"
fi
数组操作
冒泡排序
#!/bin/bash
BubbleSort()
{
# 冒泡排序
# Performing Bubble sort
num_length=${#arry[*]}
for (( i = 0; i<$num_length; i++ ))
do
for (( j = 0; j<$num_length-i-1; j++ ))
do
if [[ ${arry[j]} -gt ${arry[$(( j+1 ))]} ]]
then
# swap
temp=${arry[j]}
arry[$j]=${arry[$((j+1))]}
arry[$(( j+1 ))]=$temp
fi
done
done
echo "Array in sorted order :"
echo -e "\E[1;31m${arry[*]} \033[0m"
}
arry=(9 8 5 6 2 4 7 1)
BubbleSort
字体颜色
常用颜色格式:
normal='\033[0m' # 默认颜色
style='\033[1m' # 高亮
style='\033[4m' # 添加下划线
style='\033[7m' # 反显
color='\033[32m' # 绿色字体
color='\033[30m' # 黑色
color='\033[31m' # 红色
color='\033[33m' # 黄色
color='\033[34m' # 蓝色
color='\033[35m' # 紫色
color='\033[36m' # 深绿
color='\033[37m' # 白色
使用方法:
echo -e "\033[1m" "There are 8 ducks swimming in a pond"
echo -e "\033[0m" "There are 8 ducks swimming in a pond"
echo -e "\033[4m" "There are 8 ducks swimming in a pond"
echo -e "\033[7m" "There are 8 ducks swimming in a pond"
echo -e "\033[32m" "There are 8 ducks swimming in a pond"

文本处理
文件操作
sed -i '/^$/d' test.txt # 删除空行
sed -i 's/ *//g' test.txt # 删除空格
IFS=$'\n' # linux分隔符,默认是空格
for lines in `cat test.txt`; do # 循环读取每一行
pic=`echo $lines | grep '\!\[\]('` # 处理读取的内容:使用Linux三剑客进行文本处理
if [ "$pic" != "" ]
then
echo $pic >> new_file.txt
fi
# do something
done
清空文件内容
用于每次写文件时清空文件内容,下面介绍5种方法,前面4种方法中,如果文件不存在会创建文件。
$ cat /dev/null > test.txt
$ : > test.txt
$ > test.txt
$ true > test.txt
$ sed -i '1,$d' test.txt # 如果文件test.txt不存在会报错
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期