1. 函数传参
#!/bin/bash
test_function() {
echo $1 $2
}
test_function "hello" "world"
Result:
~$ ./example.sh
hello world
2. 分割字符串
#!/bin/bash
LINE="test_name,test_address,test_phone_number"
name=$(echo $LINE | cut -d ',' -f 1)
address=$(echo $LINE | cut -d ',' -f 2)
phone_number=$(echo $LINE | cut -d ',' -f 3)
echo $name
echo $address
echo $phone_number
Result:
~$ ./example.sh
test_name
test_address
test_phone_number
3. 读取文件
#!/bin/bash
i=0
SPACE=" "
while read line
do
echo $i $SPACE $line
i=$((i+1))
done < test.txt
Result:
~$ cat test.txt
hahaha
haha
ha
~$ ./example.sh
0 hahaha
1 haha
2 ha
4. 正则表达式匹配
#!/bin/bash
LINE="This is a test string. Let's see if the UUID will be matched: ea77b80d-d17f-476a-b55f-916423d02dcc. Hahaha!"
uuid=$(echo $LINE | grep -E -o '[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}')
echo $uuid
Result:
~$ ./example.sh
ea77b80d-d17f-476a-b55f-916423d02dcc
没有评论:
发表评论