Skip to main content
added 34 characters in body
Source Link
anubhava
  • 790.3k
  • 67
  • 603
  • 671

You can use:

app="rabbitmq" if docker ps | awk -v app="app"app="$app" 'NR>1{'NR > ($(NF)1 && $NF == app{ret=1; )exit} END{exit !ret}'; then docker stop "$app" && docker rm -f "$app" fi 
  • awk command gets a command line var app from BASH's variable $app
  • "$app". NR>1 skips first header row from docker ps command.
  • ($(NF) == app ) Compare last column NAMESNAMES is equal to app variable or not

You can use:

app="rabbitmq" if docker ps | awk -v app="app" 'NR>1{ ($(NF) == app ) }'; then docker stop "$app" && docker rm -f "$app" fi 
  • awk command gets a command line var app from BASH's variable
  • "$app". NR>1 skips first header row from docker ps command.
  • ($(NF) == app ) last column NAMES is equal to app variable

You can use:

app="rabbitmq" if docker ps | awk -v app="$app" 'NR > 1 && $NF == app{ret=1; exit} END{exit !ret}'; then docker stop "$app" && docker rm -f "$app" fi 
  • awk command gets a command line var app from BASH's variable $app
  • NR>1 skips first header row from docker ps command.
  • $(NF) == app Compare last column NAMES is equal to app variable or not
for container to exit it needs to be the last column variable i.e. NF. Also added bullet points to break down awk commands used to help with readability.
Source Link

You can use:

app="rabbitmq" if docker ps | awk -v app="$app"app="app" 'NR>1{exit  ($2$(NF) !~== app ) }'; then   docker stop "$app" && docker rm -f "$app" fi 

awk command gets a command line var app from BASH's variable "$app". NR>1 skips first header row from docker ps command. $2 !~ app means if content of $app don't match with column 2 which is IMAGE column and exit returns appropriate exit status i.e. 0 if match succeeds and 1 if if it doesn't due !~ operator.

  • awk command gets a command line var app from BASH's variable
  • "$app". NR>1 skips first header row from docker ps command.
  • ($(NF) == app ) last column NAMES is equal to app variable

You can use:

app="rabbitmq" if docker ps | awk -v app="$app" 'NR>1{exit ($2 !~ app)}'; then   docker stop "$app" && docker rm -f "$app" fi 

awk command gets a command line var app from BASH's variable "$app". NR>1 skips first header row from docker ps command. $2 !~ app means if content of $app don't match with column 2 which is IMAGE column and exit returns appropriate exit status i.e. 0 if match succeeds and 1 if if it doesn't due !~ operator.

You can use:

app="rabbitmq" if docker ps | awk -v app="app" 'NR>1{  ($(NF) == app ) }'; then docker stop "$app" && docker rm -f "$app" fi 
  • awk command gets a command line var app from BASH's variable
  • "$app". NR>1 skips first header row from docker ps command.
  • ($(NF) == app ) last column NAMES is equal to app variable
added 337 characters in body
Source Link
anubhava
  • 790.3k
  • 67
  • 603
  • 671

You can use:

app="rabbitmq" if docker ps | awk -v app="$app" 'NR>1{exit ($2 !~ app)}'; then docker stop "$app" && docker rm -f "$app" fi 

awk command gets a command line var app from BASH's variable "$app". NR>1 skips first header row from docker ps command. $2 !~ app means if content of $app don't match with column 2 which is IMAGE column and exit returns appropriate exit status i.e. 0 if match succeeds and 1 if if it doesn't due !~ operator.

You can use:

app="rabbitmq" if docker ps | awk -v app="$app" 'NR>1{exit ($2 !~ app)}'; then docker stop "$app" && docker rm -f "$app" fi 

You can use:

app="rabbitmq" if docker ps | awk -v app="$app" 'NR>1{exit ($2 !~ app)}'; then docker stop "$app" && docker rm -f "$app" fi 

awk command gets a command line var app from BASH's variable "$app". NR>1 skips first header row from docker ps command. $2 !~ app means if content of $app don't match with column 2 which is IMAGE column and exit returns appropriate exit status i.e. 0 if match succeeds and 1 if if it doesn't due !~ operator.

deleted 8 characters in body
Source Link
anubhava
  • 790.3k
  • 67
  • 603
  • 671
Loading
Source Link
anubhava
  • 790.3k
  • 67
  • 603
  • 671
Loading