linux - 在 Bash 中导入远程源文件

标签 linux bash shell unix curl

我的 VM 中有一个名为 color.sh 的文件:

#!/bin/bash

# --------------------------------------------------------------------------------
# Goal    : include all color
# Run     : curl 45.55.88.57/code/color.sh  | bash

# grap user input first argument
# ------------------------------

#================================
#            Colors             =
#================================

declare -A colors

# Reset
colors[Color_Off]='\033[0m'       # Text Reset

# Regular Colors
colors[Black]='\033[0;30m'        # Black
colors[Red]='\033[0;31m'          # Red
colors[Green]='\033[0;32m'        # Green
colors[Yellow]='\033[0;33m'       # Yellow
colors[Blue]='\033[0;34m'         # Blue
colors[Purple]='\033[0;35m'       # Purple
colors[Cyan]='\033[0;36m'         # Cyan
colors[White]='\033[0;37m'        # White

# Bold
colors[BBlack]='\033[1;30m'       # Black
colors[BRed]='\033[1;31m'         # Red
colors[BGreen]='\033[1;32m'       # Green
colors[BYellow]='\033[1;33m'      # Yellow
colors[BBlue]='\033[1;34m'        # Blue
colors[BPurple]='\033[1;35m'      # Purple
colors[BCyan]='\033[1;36m'        # Cyan
colors[BWhite]='\033[1;37m'       # White

# Underline
colors[UBlack]='\033[4;30m'       # Black
colors[URed]='\033[4;31m'         # Red
colors[UGreen]='\033[4;32m'       # Green
colors[UYellow]='\033[4;33m'      # Yellow
colors[UBlue]='\033[4;34m'        # Blue
colors[UPurple]='\033[4;35m'      # Purple
colors[UCyan]='\033[4;36m'        # Cyan
colors[UWhite]='\033[4;37m'       # White

# Background
colors[On_Black]='\033[40m'       # Black
colors[On_Red]='\033[41m'         # Red
colors[On_Green]='\033[42m'       # Green
colors[On_Yellow]='\033[43m'      # Yellow
colors[On_Blue]='\033[44m'        # Blue
colors[On_Purple]='\033[45m'      # Purple
colors[On_Cyan]='\033[46m'        # Cyan
colors[On_White]='\033[47m'       # White

# High Intensity
colors[IBlack]='\033[0;90m'       # Black
colors[IRed]='\033[0;91m'         # Red
colors[IGreen]='\033[0;92m'       # Green
colors[IYellow]='\033[0;93m'      # Yellow
colors[IBlue]='\033[0;94m'        # Blue
colors[IPurple]='\033[0;95m'      # Purple
colors[ICyan]='\033[0;96m'        # Cyan
colors[IWhite]='\033[0;97m'       # White

# Bold High Intensity
colors[BIBlack]='\033[1;90m'      # Black
colors[BIRed]='\033[1;91m'        # Red
colors[BIGreen]='\033[1;92m'      # Green
colors[BIYellow]='\033[1;93m'     # Yellow
colors[BIBlue]='\033[1;94m'       # Blue
colors[BIPurple]='\033[1;95m'     # Purple
colors[BICyan]='\033[1;96m'       # Cyan
colors[BIWhite]='\033[1;97m'      # White

# High Intensity backgrounds
colors[On_IBlack]='\033[0;100m'   # Black
colors[On_IRed]='\033[0;101m'     # Red
colors[On_IGreen]='\033[0;102m'   # Green
colors[On_IYellow]='\033[0;103m'  # Yellow
colors[On_IBlue]='\033[0;104m'    # Blue
colors[On_IPurple]='\033[0;105m'  # Purple
colors[On_ICyan]='\033[0;106m'    # Cyan
colors[On_IWhite]='\033[0;107m'   # White

我想将它导入到我的其他 shell 脚本中。我不确定如何实现。

我试过在其他脚本上调用它:

bashrc.sh

curl 45.55.88.57/code/color.sh | source

color=${colors[$input_color]}
white=${colors[White]}

export PS1='$white┌──[$color\u$white@$color\h$white]──$white[$color\w$white] \n└── $white'

这是行不通的。

对我有什么提示吗?

最佳答案

这里有一个更简单的方法来重现您的问题:

#!/bin/bash
echo 'var=42' | source
echo "The variable is $var"

执行时:

$ ./foo
./foo: line 2: source: filename argument required
source: usage: source filename [arguments]
The variable is

有两个问题:

  1. source 不从标准输入读取
  2. 通过管道连接到source 将创建一个子shell,限制任何更改

在 Bash 4 上,您可以使用 Process Substitution 解决这两个问题:

source <(curl 45.55.88.57/code/color.sh)

Bash 3(在 MacOS 上发现)有一个 bug 阻止了这个,但你可以使用 eval 代替。这也适用于 Bash4:

eval "$(curl 45.55.88.57/code/color.sh)"

关于linux - 在 Bash 中导入远程源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54797663/

相关文章:

linux - Unix 字符串在文件中查找和替换

c - 对 _mm_crc32_u64 的 undefined reference

c - 检索符号链接(symbolic link)的路径

mysql - 使用 bash 脚本在 mysql 中插入值

linux - 如何使用 bash 中的安装将目录复制到目录中?

linux - Bash shell 脚本来获取子字符串?

Bash 脚本,用于选择以跨越两行的模式开始并以空行结束的范围。赛德?

bash - shell脚本中2个字符串变量的比较

linux - 使用 bash 将一个 txt 文件复制两次到另一个文件

bash - awk 提取列但仅从一行中提取