- 文件查找
find . -iname "*.c"
find . -iname "*.[ch]"
find . -iname "*.c" -o -iname "*.sql"
# find默认emacs正则
find . -regex ".*\.java|.*\.xml"
#指定posix-extended正则
find . -regextype posix-extended -regex ".*\.(java|xml)"
- 关于引号""
**字符串作为参数
字符串作为参数,输送给函数、executable、builtin等。
. 如果参数不加"" ,则shell会按照空白符(空格,换行等),分割为多个参数。
. 如果加"",则整体作为一个参数。
【例子1】
#!/bin/bash
function test1
{
test_str="i am
a test
so cool
"
echo "====without quotes===="
echo $test_str | grep "test"
echo "====with quotes===="
echo "$test_str" | grep "test"
}
test1
执行结果如下。
. 不带引号,多行字符串被作为多个参数传递。echo接收到多个参数,以空格为分割打印。效果为变为单行的字符串。grep后还是单行。
. 带引号,该变量对应字符串整体作为一个参数传递。换行得以保留,grep最终只grep到了一行的内容。
[root@host4 tpcc]# bash h.sh
====without quotes====
i am a test so cool
====with quotes====
a test
【例子2】
但有时候反而需要用到该特性,如下面例子,
变量dirs如下:
./gprof/41280/
./gprof/41281/
./gprof/41282/
通过for dir in $dirs对该变量代表的各个文件夹遍历。如果$dirs加了引号"$dirs",则只会得到所有文件夹以空格连接的一个字符串。因为for只得到了一个字符串。
function test2
{
dirs=$(find gprof/ -maxdepth 1 -type d|sort)
echo "====without quotes===="
### not in quotes
for dir in $dirs
do
echo "find a dir: $dir"
done
echo "====with quotes===="
### in quotes
for dir in "$dirs"
do
echo "find a dir: $dir"
done
}
test2
输出结果:
====without quotes====
find a dir: ./gprof/41280/
find a dir: ./gprof/41281/
find a dir: ./gprof/41282/
====with quotes====
find a dir: ./gprof/41280/
./gprof/41281/
./gprof/41282/
【例子3】
#!/bin/bash
myFunction()
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "second string with spaces" "thirdString"
输出
firstString
second
string
myFunction()
{
echo "$1"
echo "$2"
echo "$3"
}
myFunction "firstString" "second string with spaces" "thirdString"
输出
firstString
second string with spaces
thirdString
- Shell字符串截取
格式 | 说明 |
---|---|
${string: start :length} | 从 string 字符串的左边第 start 个字符开始,向右截取 length 个字符。 |
${string: start} | 从 string 字符串的左边第 start 个字符开始截取,直到最后 |
${string: 0-start :length} | 从 string 字符串的右边第 start 个字符开始,向右截取 length 个字符。 |
${string: 0-start} | 从 string 字符串的右边第 start 个字符开始截取,直到最后。 |
${string#*chars} | 从 string 字符串第一次出现 chars 的位置开始,截取 chars 右边的所有字符。 |
${string##*chars} | 从 string 字符串最后一次出现 chars 的位置开始,截取 chars 右边的所有字符。 |
${string%*chars} | 从 string 字符串第一次出现 chars 的位置开始,截取 chars 左边的所有字符。 |
${string%%*chars} | 从 string 字符串最后一次出现 chars 的位置开始,截取 chars 左边的所有字符。 |
- 字符串提取
# 提取<>之间的字符串
echo '2022-08-22 01:37:15.071 UTC [31608] WARNING: [bind] consumed: <19> miliseconds'| sed 's/.*<\(.*\)>.*/\1/'
- bash terminal
vimdiff file1 file2
# $:i 命令的第i个参数
# $:! 命令的最后一个参数
# vi 打开file1
vi $:1
# vi 打开file2
vi $:2
# vi 打开file2
vi $:!
配置文件解析
key=value型配置文件,解析,保存到associative array#!/bin/bash # Replace 'config_file.conf' with the path to your configuration file CONFIG_FILE="config_file.conf" # Declare an associative array to store the option-value pairs declare -A config_map # Function to parse the configuration file and save the options to the associative array parse_config() { while IFS="=" read -r key value; do # Remove leading and trailing whitespaces key=$(echo "$key" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') value=$(echo "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') # Skip comments and empty lines if [[ ${key:0:1} == "#" ]] || [[ -z $key ]]; then continue fi # Save the option and its value to the associative array config_map["$key"]="$value" done < "$CONFIG_FILE" } # Call the function to parse the configuration file and save the options to the associative array parse_config # Access a specific value by its key echo "main_node_user: ${config_map['main_node_user']}" # Access all keys and values in the associative array for key in "${!config_map[@]}"; do echo "Option: $key, Value: ${config_map[$key]}" done
associative array遍历
#!/bin/bash # Your string of IP addresses separated by commas ip_string="ip1,ip2,ip3,ip4" # Convert the string to an array IFS=',' read -ra ip_array <<< "$ip_string" # Access elements in the array by their indices echo "First IP address: ${ip_array[0]}" echo "Second IP address: ${ip_array[1]}" echo "Third IP address: ${ip_array[2]}" echo "Fourth IP address: ${ip_array[3]}" # Loop through the array using indices array_length=${#ip_array[@]} for ((i=0; i<array_length; i++)); do echo "IP address at index $i: ${ip_array[i]}" done
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。