Use of colon in this shell script

Hi the following lines appear in a bash shell script.  I would like to know what these lines is doing, the colon in particular.

: ${pidfile:=/bin/lcl_scripts/httpd2.pid}
pid=$(<$pidfile)
gmanpertAsked:
Who is Participating?
 
PHeadlandConnect With a Mentor Commented:
The first colon on the line prevents the rest of the line being interpreted as a command. For example:

    ${v1="Hello"}

Gives an error, because it first assigns "Hello" to v1, then tries to execute the value of v1 as a command.

The "${pidfile:= ... }" means that the value of pidfile is only changed if it is not null or already set. So:

    v1="Hello"
    : ${v1="World!"}
    echo $v1

Produces "Hello", not "World!". This means that pidfile may already have been set on entry to the script, but if it isn't, the script uses the default value.

The second line assigns the contents of pidfile to pid. pidfile is the name of a file containing a process id, "<" reads from that file, so pid ends up set to the contents of the file (the process id).
0
 
gmanpertAuthor Commented:
Actually, here is the whole code snippet.

: ${pidfile:=/bin/lcl_scripts/httpd2.pid}
pid=$(<$pidfile)
 sudo kill -TERM $pid
 case $? in
  1)  echo -n "(not running)";;
  0)  # wait until the processes are gone (the parent is the last one)
  echo -n "(waiting for all children to terminate) "
  for ((wait=0; wait<120; wait++)); do
   if test -f $pidfile; then                       # -f test for regular file
      usleep 50000
      continue
   fi
   if ! test -f /proc/$pid/exe; then
       break
   fi
   if test "$(readlink /proc/$pid/exe 2>/dev/null)" = $apache_bin; then
       usleep 50000
   else
       break
   fi

  done
  ;;
 esac

0
All Courses

From novice to tech pro — start learning today.