这就是我的 docker-compose.yml 的样子。
nginx:
container_name: 'nginx'
image: 'nginx:1.11'
restart: 'always'
ports:
- '80:80'
- '443:443'
volumes:
- '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
links:
- 'anything'
Run Code Online (Sandbox Code Playgroud)
现在我需要通过 shell 脚本(在 ubuntu 服务器上)添加一些内容。我不太确定是否有可能:
nginx/links
,如果它不存在newthing
如果不存在 newthing-block 则追加块新内容应如下所示:
nginx:
container_name: 'nginx'
image: 'nginx:1.11'
restart: 'always'
ports:
- '80:80'
- '443:443'
volumes:
- '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
- '/etc/letsencrypt:/etc/letsencrypt'
links:
- 'anything'
- 'newthing'
newthing:
container_name: foo
image: 'newthing:1.2.3'
restart: always
hostname: 'example.com'
Run Code Online (Sandbox Code Playgroud) 如何正确使用文件名中的变量?这就是我正在做的,但下划线似乎有一些问题:
domain="example"
sub="foo"
if [ -f /opt/nginx/conf.d/com_$domain_$sub.conf ]
cat <<EOF > /opt/nginx/conf.d/com_$domain_$sub.conf
some multiline
content
EOF
fi
Run Code Online (Sandbox Code Playgroud) 如何在一个命令中创建多个嵌套目录?
mkdir -p /just/one/dir
Run Code Online (Sandbox Code Playgroud)
但我需要创建多个不同的嵌套目录...
我想在 shell 脚本中打印一个复选标记和一个十字标记:
#!/bin/bash
echo -e "\xE2\x9C\x94 existing"
echo -e "\xE2\x9D\x8C missing"
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
我需要/opt/nginx/conf.d/default.conf
通过 shell 脚本创建包含此内容的文件,如果文件不存在则创建该文件:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
}
Run Code Online (Sandbox Code Playgroud)
如何通过 shell 脚本编写多行内容?
我创建了目录
sudo mkdir -p /opt/nginx/conf.d
Run Code Online (Sandbox Code Playgroud)
但我不知道如何写文件。
如果安装了 docker,是否可以通过 shell 脚本进行检查。
我会手动检查这个
sudo docker run --name hello-world-container hello-world
sudo docker rm hello-world-container
Run Code Online (Sandbox Code Playgroud)
但是我想以编程方式进行检查,因此如果此测试失败,则可以启动安装例程。
更新
#!/bin/bash
which docker
if [ $? -eq 0 ]
then
docker --version | grep "Docker version"
if [ $? -eq 0 ]
then
echo "docker existing"
else
echo "install docker"
fi
else
echo "install docker" >&2
fi
Run Code Online (Sandbox Code Playgroud) 我正在创建一个这样的文件EOF
:
cat <<EOF > Dockerfile
RUN apt-get update -y \
&& apt-get install -y \
bsdtar \
git \
locales
EOF
Run Code Online (Sandbox Code Playgroud)
但结果是:
RUN apt-get update -y && apt-get install -y bsdtar git locales
Run Code Online (Sandbox Code Playgroud)
我想保留反斜杠和换行符
我没有得到任何打印结果,但我不明白为什么?
read -e -i "no" -p "Install? " result
if [ '$result' == 'yes' ]; then
declare -a subs=('one' 'two')
for sub in "${subs[@]}"
do
echo "$sub"
done
fi
Run Code Online (Sandbox Code Playgroud) 如果出现这种情况,我如何检查 bash 脚本
sudo docker images -q nginx
Run Code Online (Sandbox Code Playgroud)
给我一个结果字符串,这意味着这个容器是存在的
sudo docker images -q nginx
if [ $? != '' ]
then
echo "existing"
else
echo "missing
Run Code Online (Sandbox Code Playgroud) 我如何获得这种情况的单衬里?
if [ -f ~/.ssh/config ]
then
echo -e "\xE2\x9C\x94 Config file existing"
fi
Run Code Online (Sandbox Code Playgroud)
我的尝试:
if [ ! -f ~/.ssh/config ] || echo -e "\xE2\x9C\x94 Config file existing"
Run Code Online (Sandbox Code Playgroud) 我需要将一些多行文本写入文件:
cat <<EOF > file
server {
listen 80;
server_name $sub.$domain.com www.$sub.$domain.com;
return 301 https://$server_name$request_uri;
}
EOF
Run Code Online (Sandbox Code Playgroud)
对我来说,问题是$server_name$request_uri
应该作为字符串处理,而 domain 和 sub 应该是一个变量。所以我需要以$
某种方式逃避。
如果安装了 kubernetes,我需要通过 bash 脚本检查。如果不是,我开始我的设置程序。我认为最好检查是否kubectl cluster-info
有输出。如何检查失败的命令?
if command kubectl cluster-info > /dev/null; then
# sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
# ...
fi
Run Code Online (Sandbox Code Playgroud) 我曾经gpg2
在 Ubuntu 16.04 服务器上生成一些密钥。现在我必须移动机器。我需要将所有生成的密钥传输到 Mac。我想我只需要复制~/.gnupg
文件。
但是我必须将其存储在哪里才能让他们通过 via 调用gpg --list-secret-keys --keyid-format LONG mr@robot.sh
?一样的地方?
如何gpg2
在我的 Mac 上安装?homebrew gpg2
不存在。