我想将 -C8 解析为 bash 函数并获取变量中的数值。
我见过 -C 8 和 -C=8 的参数解析实现,但不是 -C8。
while (( $# > 0 )); do
case $1 in
("-C") cnt="$2" ; shift ;;
("-C="*) cnt="${1#*=}" ; shift ;;
esac
done
但不是怎么处理-C8
。
我正在使用以下 bash 函数来捕获 和 之间的行## mode: rec
,## # End of rec
但无法删除#
结果的前导。
capture ()
{
local efile="$1"
local begorg endorg
begorg='^[[:space:]]*## mode: org$'
endorg='^[[:space:]]*## # End of org$'
awk -v bego="$begorg" -v endo="$endorg" \
'$0 ~ bego { flag=1; next }
$0 ~ endo { flag=0; }
flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
}
这是输入
文件:test.sh
## mode: org
## * Using case statement
## # End of org
case $arg in
("V")
echo "Author"
;;
(*)
## mode: org
## ** Silent Error Reporting Mode (SERM) in getopts
## *** Detects warnings without printing built-in messages.
## *** Enabled by colon {:} as first character in shortopts.
## # End of org
break
;;
esac
## mode: org
## HDG: Handling function argument parsing with getopts
## Rmk: No call to {shift} is required with getopts
## Rmk: No hyphen {-} required when searching option names
## + Case patterns do not start with option hyphen {-} because
## + getopts strips off the hyphen and makes the value of {arg}
## + to be just the option letter.
## Rmk: Separating options from non-options with --
## + {getopts} stops processing options when the argument is not
## + defined as an option in {shortopts}; or if the argument is
## + "--", which explicitly terminates the list of options.
## Rmk: Using -- as value to an option
## + An option value can be -- without it being considered a
## + separator between options and non-options.
## + Example { edvart-getopts -g "--" }.
## Rmk: Explicitly testing for {--}
## + There is no need to test for {--} when using {getopts}.
## # End of org
但我得到了这个结果
# * Using case statement
# ** Silent Error Reporting Mode (SERM) in getopts
# *** Detects warnings without printing built-in messages.
# *** Enabled by colon {:} as first character in shortopts.
# HDG: Handling function argument parsing with getopts
# Rmk: No call to {shift} is required with getopts
# Rmk: No hyphen {-} required when searching option names
# + Case patterns do not start with option hyphen {-} because
# + getopts strips off the hyphen and makes the value of {arg}
# + to be just the option letter.
# Rmk: Separating options from non-options with --
# + {getopts} stops processing options when the argument is not
# + defined as an option in {shortopts}; or if the argument is
# + "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
# + An option value can be -- without it being considered a
# + separator between options and non-options.
# + Example { edvart-getopts -g "--" }.
# Rmk: Explicitly testing for {--}
# + There is no need to test for {--} when using {getopts}.
# HDG: Silent Error Reporting Mode (SERM) in getopts
# Rmk: Detects warnings without printing built-in messages.
# Rmk: Enabled by colon {:} as first character in shortopts.
预期的输出是
* Using case statement
** Silent Error Reporting Mode (SERM) in getopts
*** Detects warnings without printing built-in messages.
*** Enabled by colon {:} as first character in shortopts.
HDG: Handling function argument parsing with getopts
Rmk: No call to {shift} is required with getopts
Rmk: No hyphen {-} required when searching option names
+ Case patterns do not start with option hyphen {-} because
+ getopts strips off the hyphen and makes the value of {arg}
+ to be just the option letter.
Rmk: Separating options from non-options with --
+ {getopts} stops processing options when the argument is not
+ defined as an option in {shortopts}; or if the argument is
+ "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
+ An option value can be -- without it being considered a
+ separator between options and non-options.
+ Example { edvart-getopts -g "--" }.
Rmk: Explicitly testing for {--}
+ There is no need to test for {--} when using {getopts}.
HDG: Silent Error Reporting Mode (SERM) in getopts
Rmk: Detects warnings without printing built-in messages.
Rmk: Enabled by colon {:} as first character in shortopts.
echo $HOME
只会打印 /home/user
。但在我的系统中,/home
不是挂载在/
它自己的分区下也不是。
# shared linux data partition, `/home` is here
UUID=a89334f7-59b7-4d04-b89b-a5a30c379644 /mnt/linux_data ext4 defaults 1 2
主目录使用绑定挂载挂载,
# bind mount /home to directory to a directory on /linux_data
/mnt/linux_data/01_centos /home none bind 0 0
单击 Caja 中的主目录图标,位置栏显示 /home/user
. 但是这个目录实际上应该是/mnt/linux_data/01_centos/user
那个位置确实存在的目录。/mnt/linux_data/01_centos/user
我可以通过 Caja 中的图标导航到,/root
但我想要一些方法来打印操作系统认为的完整路径 /home
。