假设您有一个带有很多选项的函数,并且您决定动态存储它们的值:
#!/bin/bash
fun() {
local OPTIND __option
while getopts ":$(printf '%s:' {a..z} {A..Z})" __option
do
case $__option in
[[:alpha:]])
# ...
local "__$__option=$OPTARG"
esac
done
# here you might get a conflict with external variables:
[[ ${__a:+1} ]] && echo "option a is set"
[[ ${__b:+1} ]] && echo "option b is set"
# etc...
[[ ${__Z:+1} ]] && echo "option Z is set"
}
Run Code Online (Sandbox Code Playgroud)
有没有办法检查变量是否在本地定义?
Léa*_*ris 10
您绝对可以检测到变量是本地变量(如果它已使用 Bash 的declare
, local
,语句之一typeset
声明)。
这是一个例子:
#!/usr/bin/env bash
a=3
b=2
fn() {
declare a=1
b=7
if local -p a >/dev/null 2>&1; then
printf 'a is local with value %s\n' "$a"
else
printf 'a is not local with value %s\n' "$a"
fi
if local -p b >/dev/null 2>&1; then
printf 'b is local with value %s\n' "$b"
else
printf 'b is not local with value %s\n' "$b"
fi
}
fn
Run Code Online (Sandbox Code Playgroud)
输出是:
a is local with value 1
b is not local with value 7
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
552 次 |
最近记录: |