如何以交互方式生成导出并将其导出?

时间:2019-01-30 23:43:46

标签: bash

我希望能够生成导出和完成并使用相同的命令来获取它们。

cmd create-config --interactive # creates configs for cmd
. <(cmd completion)             # creates bash completion (uses configs)
. <(cmd exports)                # creates exports (uses configs)

我想拥有一个可以完成所有任务的命令

. <(cmd init)
. <(cmd init --interactive)

示例代码有效,但是我不确定是否应该做其他事情:

#! /bin/bash


chmod +x random/test.sh

. <(random/test.sh init)
# Sourcing exports
# ===============
# export FOO=foo
# export BAR=bar
# ===============
# ok

. <(random/test.sh init --interactive)
# FOO:asd
# BAR:zxc
#
# Sourcing exports
# ===============
# export FOO=asd
# export BAR=zxc
# ===============
# ok

random/test.sh

创建配置和可靠的输出。

#! /bin/bash


tmp_exports=/tmp/exports


config() {
if [ "$1" = "--interactive" ]; then
  echo -n "FOO:"; read -r foo
  echo -n "BAR:"; read -r bar
else
  foo=foo
  bar=bar
fi

cat << EOF > "$tmp_exports"
export FOO=$foo
export BAR=$bar
EOF
}


init() {
cat << EOF
  echo
  $0 config $@
  echo "Sourcing exports"
  echo "==============="
  cat "$tmp_exports"
  . "$tmp_exports"
  echo "==============="
  echo ok
EOF
}


"$@"

runnable code

0 个答案:

没有答案