Data Mastery
Data Mastery

Reputation: 2085

Using Variable in Shell script as input for another command

#!/bin/bash

sudo docker-compose -f /home/administrator/compose/docker-compose.yml up --build -d
OUTPUT=$(docker ps | grep 'nginx_custom' | awk '{ print $1 }')

echo $OUTPUT

sudo docker $OUTPUT nginx -s reload

This the the ID that get´s printed correctly in the console.

6e3b3aa3fbc4

This command works fine.

 docker exec 6e3b3aa3fbc4 nginx -s reload

However the variable seems not to get passed to the command here:

sudo docker $OUTPUT nginx -s reload

I am quite unfamiliar with the shell :(. How do I pass the variable to a command that is longer than just echo?

Upvotes: 0

Views: 159

Answers (1)

vgersh99
vgersh99

Reputation: 965

add set -x to the script and see what happens: you can probably get rid of grep and incorporate it inside awk

#!/bin/bash
set -x
sudo docker-compose -f /home/administrator/compose/docker-compose.yml up --build -d
OUTPUT=$(docker ps | awk '/ngnix_custom/{ print $1 }')

echo $OUTPUT

sudo docker $OUTPUT nginx -s reload

Upvotes: 2

Related Questions