Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash Here Documents

farenda 2015-06-11 0

Problem:

How to create Here/inline Documents in Bash?

Bash Here documents can be really helpful templating mechanism when used in Bash scripts. They can generate almost anything, even scripts to compile and run sample Java code!

Solution:

The syntax of Bash here documents is simple:

# "<<" or "<<-"
<<[-]word
    here-document content
delimiter

How it works: When Bash sees “<<WORD” the it knows that it is a Here Document and reads everything until it finds “WORD” on a single line. If the operator is “<<-WORD” then it removes all leading TAB chars from the input.

For some commands you can just pass read input, but very often its useful to redirect the input to some file using “>>” operator, like in the example below.

Here’s an example of a shell script that takes a package and class name and generates package with sample Java class content:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 package class"
    echo
    echo "Creates a Java class within specified package."
    echo "Example: $0 com.farenda.solved JavaExampleClass"
    exit 1
fi

PACKAGE="${1}"
PACKAGE_DIR="${PACKAGE//.//}"
MAIN_CLASS="${2}"

echo "Creating Java package dirs: ${PACKAGE_DIR}"
mkdir -p "${PACKAGE_DIR}"

# read until EOF and redirect to the new Java class:
cat <<EOF >> "${PACKAGE_DIR}/${MAIN_CLASS}.java"
package ${PACKAGE};

public class ${MAIN_CLASS} {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
EOF

PACKAGE_DIR var is created using Bash variable substitution. The expression ${PACKAGE//.//} means: take value of PACKAGE variable and substitute all dots . with /, to turn Java package name into a path on a filesystem.

You can use any word you like as a delimiter, but EOF is commonly used.

If you want, you can extend the shell script with generation of Java compilation script, like so:

COMPILER=compile.sh
echo "Creating compilation script: ${COMPILER}"
cat <<EOF >> ${COMPILER}
#!/bin/bash
echo "Compiling: javac ${PACKAGE_DIR}/*.java"
javac ${PACKAGE_DIR}/*.java
EOF
chmod u+x ${COMPILER}

echo
echo "To compile code execute: ./${COMPILER}"

You can use Bash variable substitution in Here Documents and automate generation of many, many things.

Lets run it to see how it all work:

$> ./here-documents.sh com.farenda.solved SampleClass
Creating Java package dirs: com/farenda/solved
Creating compilation script: compile.sh

To compile code execute: ./compile.sh
$> ./compile.sh
Compiling: javac com/farenda/solved/*.java
$> java -cp . com.farenda.solved.SampleClass
Hello, world!

Isn’t that cool? :-)

Share with the World!
Categories Bash Tags bash
Previous: EmbedMongo error libssl libcrypto
Next: Spock Framework Given When Then Where

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored