使用 getopts 导出变量后无法回显值
unable to echo value after exporting variable using getopts
我试图在我的脚本中弄清楚,为什么我的变量 dbname
在显式导出该变量后使用 getopts
时没有被设置。我试着做一个回声,echo $dbname
和它唯一没有值的变量。这是我的代码:
#!/bin/bash
export SCHEMAMAIN="$dbname__source"
export OLD="/tmp/Old.out"
export NEW="/tmp/New.out"
PSQL=$(which psql)
usage () {
echo "Usage: [=10=] -o oldhosts" -d dbname -t table -n newhosts""
}
while getopts ":o:d:t:n:" opt; do
case $opt in
o) oldhosts="$OPTARG";;
d) export dbname=${OPTARG};;
t) export table=${OPTARG};;
n) newhosts="$OPTARG";;
*) echo "Error unknown option -$OPTARG"
usage
exit 1
;;
esac
done
if [[ $oldhosts == 'hosts1' ]]; then
HOST="myevents-01.foobar.au-fareast-1.foobar.amazonaws.com"
elif [[ $oldhosts == 'hosts2' ]]; then
HOST="myevents-02.foobar.au-fareast-1.foobar.amazonaws.com"
else
echo "Old source hosts is incorrect:"
exit 1
fi
if [[ $newhosts == 'hosts3' ]]; then
HOST2="myhost-foobar-hosts-3-foobarhosts-1vjldgvvna7.foobar.au-fareast-1.foobar.amazonaws.com"
elif [[ $newhosts == 'hosts4' ]]; then
HOST2="myhost-foobar-hosts-4-foobarhosts-1dy7izxwwol.foobar.au-fareast-1.foobar.amazonaws.com"
else
echo "New hosts is incorrect:"
exit
fi
echo "$HOST"
echo "$dbname"
echo "$SCHEMAMAIN"
echo "$table"
"$PSQL" -h "$HOST" -U masteruser -d "$dbname" -p 5439 << EOF
\a
\t
\o $OLD
SELECT column_name
FROM information_schema.columns
WHERE table_schema = '$SCHEMAMAIN'
AND table_name = '$TBL'
order by ordinal_position;
EOF
如果您 运行 文件顶部的 getopts
循环并更改
怎么办
export SCHEMAMAIN="$dbname__source"
至
export SCHEMAMAIN="${dbname}__source"
?
编辑
@EtanReisner 已经对此发表了评论。
我试图在我的脚本中弄清楚,为什么我的变量 dbname
在显式导出该变量后使用 getopts
时没有被设置。我试着做一个回声,echo $dbname
和它唯一没有值的变量。这是我的代码:
#!/bin/bash
export SCHEMAMAIN="$dbname__source"
export OLD="/tmp/Old.out"
export NEW="/tmp/New.out"
PSQL=$(which psql)
usage () {
echo "Usage: [=10=] -o oldhosts" -d dbname -t table -n newhosts""
}
while getopts ":o:d:t:n:" opt; do
case $opt in
o) oldhosts="$OPTARG";;
d) export dbname=${OPTARG};;
t) export table=${OPTARG};;
n) newhosts="$OPTARG";;
*) echo "Error unknown option -$OPTARG"
usage
exit 1
;;
esac
done
if [[ $oldhosts == 'hosts1' ]]; then
HOST="myevents-01.foobar.au-fareast-1.foobar.amazonaws.com"
elif [[ $oldhosts == 'hosts2' ]]; then
HOST="myevents-02.foobar.au-fareast-1.foobar.amazonaws.com"
else
echo "Old source hosts is incorrect:"
exit 1
fi
if [[ $newhosts == 'hosts3' ]]; then
HOST2="myhost-foobar-hosts-3-foobarhosts-1vjldgvvna7.foobar.au-fareast-1.foobar.amazonaws.com"
elif [[ $newhosts == 'hosts4' ]]; then
HOST2="myhost-foobar-hosts-4-foobarhosts-1dy7izxwwol.foobar.au-fareast-1.foobar.amazonaws.com"
else
echo "New hosts is incorrect:"
exit
fi
echo "$HOST"
echo "$dbname"
echo "$SCHEMAMAIN"
echo "$table"
"$PSQL" -h "$HOST" -U masteruser -d "$dbname" -p 5439 << EOF
\a
\t
\o $OLD
SELECT column_name
FROM information_schema.columns
WHERE table_schema = '$SCHEMAMAIN'
AND table_name = '$TBL'
order by ordinal_position;
EOF
如果您 运行 文件顶部的 getopts
循环并更改
export SCHEMAMAIN="$dbname__source"
至
export SCHEMAMAIN="${dbname}__source"
?
编辑
@EtanReisner 已经对此发表了评论。