2

Consider the following script to stop and start tomcat:

#!/bin/bash

# Shutdown tomcat and delete all the logs
echo --- Shutting down Tomcat...
sh /opt/tomcat/bin/shutdown.sh -force &> /dev/null
rm -f /opt/tomcat/logs/* &> /dev/null
echo OK

# Startup tomcat
sh /opt/tomcat/bin/startup.sh &> /dev/null

echo --- Starting up Tomcat...
until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:8080 | grep 'Coyote'`" != "" ];
do
  sleep 10
done

echo OK

What I'd like to be able to do is show the OK on the same line as the message stating what's going on. How can I achieve this?

Mirrana
  • 1,601
  • 6
  • 28
  • 66

1 Answers1

6
echo -n "--- Shutting down Tomcat..."
...
echo OK

The -n suppresses the newline at the end of the echo. Note that if the Tomcat server emits any information, it will start after the triple dot.


As pointed out in the comments, you should consider:

printf "--- Shutting down Tomcat..."
...
printf "OK\n"

(Although you could leave the second as echo OK, it seems better to be consistent.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Also `printf '%s' "--- Shutting down Tomcat... "` (which could be more portable). – gniourf_gniourf May 02 '14 at 16:28
  • True: it is arguably better to use `printf`. The problem with `echo` is the different ways of doing it. System V shells used `echo "--- Shutting down Tomcat...\c"`; 7th Edition Bourne shell (actually, the `echo` binary) used the `-n`. Different versions of `echo` do different amounts and types of interpretation of the strings it is given. To some extent, POSIX standardized the `printf` command to avoid the problems with `echo`. See also [Bloated `echo` command](http://stackoverflow.com/questions/3290683/bloated-echo-command). – Jonathan Leffler May 02 '14 at 16:50
  • Agreed, [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) specifically says that using `echo` with arguments is a journey down a black hole, err, bad idea. Black holes are cool, but you wouldn't want to get eaten by one. – Adrian Frühwirth May 02 '14 at 18:15