-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env bash | ||
set -o errtrace # -E trap inherited in sub script | ||
set -o errexit # -e | ||
set -o functrace # -T If set, any trap on DEBUG and RETURN are inherited by shell functions | ||
set -o pipefail # default pipeline status==last command status, If set, status=any command fail | ||
|
||
# 模版代码,获取文件真实路径 | ||
# On Mac OS, readlink -f doesn't work, so use._real_path get the real path of the file | ||
_real_path() { | ||
cd "$(dirname "$1")" || exit | ||
file="$PWD/$(basename "$1")" | ||
while [[ -L "$file" ]]; do | ||
file="$(readlink "$file")" | ||
cd -P "$(dirname "$file")" || exit | ||
file="$PWD/$(basename "$file")" | ||
done | ||
echo "$file" | ||
} | ||
|
||
SCRIPT_PATH="$(_real_path "${BASH_SOURCE[0]}")" | ||
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" | ||
#SCRIPT_FILE="$(basename "$SCRIPT_PATH")" | ||
|
||
source "$SCRIPT_DIR/../note.common.bash" | ||
|
||
print.markdown <<'MARKDOWN_END' | ||
# bash 核心 - 命令行、字符串、分割符、转义 | ||
了解字符串,字符串分割,转义,是bash的核心 | ||
## 转义字符串 | ||
MARKDOWN_END | ||
|
||
|
||
( | ||
# 了解字符串,字符串分割,转义,是bash的核心 | ||
unescaped_string="Hello | ||
World" | ||
# 使用printf命令,"%b"参数可以正确处理转义字符 | ||
printf -v escaped_string "%q" "$unescaped_string" | ||
|
||
echo "escaped_string: $escaped_string" | ||
) | ||
|
||
|
||
( | ||
# 了解字符串,字符串分割,转义,是bash的核心 | ||
escaped_string="Hello\nWorld" | ||
# 使用printf命令,"%b"参数可以正确处理转义字符 | ||
printf -v unescaped_string "%b" "$escaped_string" | ||
|
||
echo "unescaped_string: $unescaped_string" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
set -o errtrace # -E trap inherited in sub script | ||
set -o errexit # -e | ||
set -o functrace # -T If set, any trap on DEBUG and RETURN are inherited by shell functions | ||
set -o pipefail # default pipeline status==last command status, If set, status=any command fail | ||
|
||
# 模版代码,获取文件真实路径 | ||
# On Mac OS, readlink -f doesn't work, so use._real_path get the real path of the file | ||
_real_path() { | ||
cd "$(dirname "$1")" || exit | ||
file="$PWD/$(basename "$1")" | ||
while [[ -L "$file" ]]; do | ||
file="$(readlink "$file")" | ||
cd -P "$(dirname "$file")" || exit | ||
file="$PWD/$(basename "$file")" | ||
done | ||
echo "$file" | ||
} | ||
|
||
SCRIPT_PATH="$(_real_path "${BASH_SOURCE[0]}")" | ||
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" | ||
SCRIPT_FILE="$(basename "$SCRIPT_PATH")" | ||
|
||
print.markdown(){ | ||
# IFS= 表示将IFS清空,也就是将内部字段分隔符设置为空字符串,这样一来,在执行 read 命令时, | ||
# 将整行文本视为一个单独的字段(即使行中有多个空格、制表符或换行符)。 | ||
# -r 参数告诉 read 命令不进行反斜杠转义(backslash escaping),即不对行中的反斜杠进行特殊处理。 | ||
while IFS= read -r line; do | ||
echo "$line" | ||
done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters