我在提交之前搜索了这个,但找不到任何东西。
我有一个使用我的EC2实例运行的bash脚本(ubuntu AMI) 最后一个命令将IP地址保存为变量 - 这似乎已经“突然”(最近读取)停止工作。这就像它完全忽略命令一样。
如果我手动复制粘贴命令,它可以正常工作(但是这会失败)
脚本
ggplot(...) +
scale_color_manual(values = c('1' = 'green', '2' = 'yellow', '3' = 'red'),
labels = c('1' = 'A', '2' = 'B', '3' = 'C'),
name = "Legend Title")
这不会将IP地址保存为变量,就像我说的那样,如果我事后手动将其复制到命令行中就没问题。
我正在AWS上的EC2控制台中运行它。在“高级详细信息”中的“用户数据”部分下。
似乎发生了以下命令:
#!/bin/bash
# update the install on the ubuntu image
sudo apt-get update -y
# install Docker's certificate etc
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
# add dockers official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# set up the stable repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# update again now with added repo
sudo apt-get update -y
# Install Docker CE
sudo apt-get install docker-ce -y
# install docker-compose (in case you need to make a docker-compose file)
sudo apt-get install docker-compose -y
# make docker group if not already created
sudo groupadd docker
# make ubuntu user part of docker group
sudo usermod -aG docker ubuntu
# create results dir
mkdir /home/ubuntu/mount
# change permissions on mount drive so all users can read/write
sudo chmod 775 /home/ubuntu/mount
# change ownership of mount folder so that files can be sent to it
sudo chown -R ubuntu /home/ubuntu/mount/
# install htop
sudo apt install htop
#set ip as env var
IP=$(hostname -i)
我ssh到实例上并调用'echo $ IP'并且什么都没得到。但是,如果我手动运行'IP = $(hostname -i)'命令,它会使用当前IP地址填充变量。
我现在已将脚本移至GitHub:https://raw.githubusercontent.com/ScreamingJoypad/jmeter-armada/master/userdata.sh
单独在实例上运行每个命令都可以正常工作,就像在创建实例后从实例中创建脚本一样
但是,使用IP=$(hostname -i)
然后运行curl -o <filename>.sh <link>
不起作用,并提供以下错误:
答案 0 :(得分:2)
帮助你入门。
在一个shell中创建的变量在另一个shell中不可用。
可以启动一个新shell,通过在启动时使用导出来保持它。
help export
可以使用source在当前shell中运行脚本。
help source
可以在登录时自动运行命令并设置变量。
man bash #look for 'bashrc'.
答案 1 :(得分:1)
将脚本的最后一行更改为
echo "export IP=$(hostname -i)" >> /etc/bash.bashrc
这使得bash shell中的IP
变量可用 - 这似乎是你想要的