括号的使用说明
参考:Double parenthesis with and without dollar
- $(…) means execute the command in the parens and return its stdout.
$ echo "The current date is $(date)"
The current date is Mon Jul 6 14:27:59 PDT 2015
- (…) means run the commands listed in the parens in a subshell. Example:
$ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
inside: a=2
outside: a=1
- $((…)) means perform arithmetic and return the result of the calculation. Example:
$ a=$((2+3)); echo "a=$a"
a=5
- ((…)) means perform arithmetic, possibly changing the values of shell variables, but don’t return its result. Example:
$ ((a=2+3)); echo "a=$a"
a=5
- ${…} means return the value of the shell variable named in the braces. Example:
$ echo ${SHELL}
/bin/bash
- {…} means execute the commands in the braces as a group. Example:
$ false || { echo "We failed"; exit 1; }
We failed
有用脚本收集
文件读取
读取文件目录的所有文件,按行读取每个文件,判断行文字是否包含特定字符串;如果包含,通过特殊字符来split并输出想要的值。
#!/bin/sh
for filename in /home/okchem/mysqlbackup/*.sql; do
while IFS= read line
do
# display $line or do somthing with $line
if [[ $line == *"/ocf/"* ]]; then
SUBSTRING=$(echo $line| cut -d'`' -f 2) # 用'`'来拆分,输出数组第二个
echo $SUBSTRING
fi
done <"${filename}.sql"
done
本文介绍了Shell脚本中括号的不同用法,包括命令替换、子shell、算术运算等,并提供实用示例。此外,还展示了如何利用Shell脚本来进行文件读取和处理,特别关注于从文件中搜索特定字符串并提取相关信息。
33万+

被折叠的 条评论
为什么被折叠?



