# Demonstrate 2-tag system can be implemented with `find` and `mkdir` only,
# using an example from Wikipedia.
# en.wikipedia.org/wiki/Tag_system#Example:_A_simple_2-tag_illustration

# _'s are used as a separtor between states.
mkdir _

# Production rules for an M-tag system given as constants.
M=2
PROD_A="c/c/b/a/H"
PROD_B="c/c/a"
PROD_C="c/c"

# Initial state (surrounded by _'s).
mkdir -p _/b/a/a/_

# Alphabets (constant size)
S="(/[abcH])"

# Keep appending the next state to the end of the state until seeing the halting symbol. e.g.
# _/b/a/a/_ -> _/b/a/a/_/a/c/c/a/_ -> _/b/a/a/_/a/c/c/a/_/c/a/c/c/b/a/H/_ -> ... -> .../_/H/c/c/c/c/c/c/a/_ (halt)
#
# Line 2:    Halting condition
# Line 3-6:  Copy the previous state removing M characters from the start, utilizing back-references.
# Line 7-29: Apply the production rule for a, b, c.
find _ -regextype posix-extended -empty \( \
    -regex ".*_/H.*_|.*_(/[^/]?){,$M}_" -prune -o \
    -regex ".*_$S{$M}($S*)/a$S*/_\2" \( -execdir mkdir a/a b/a c/a H/a _/a \; -o -true \) -o \
    -regex ".*_$S{$M}($S*)/b$S*/_\2" \( -execdir mkdir a/b b/b c/b H/b _/b \; -o -true \) -o \
    -regex ".*_$S{$M}($S*)/c$S*/_\2" \( -execdir mkdir a/c b/c c/c H/c _/c \; -o -true \) -o \
    -regex ".*_$S{$M}($S*)/H$S*/_\2" \( -execdir mkdir a/H b/H c/H H/H _/H \; -o -true \) -o \
    \( \
        -regex ".*_/a$S*/_$S*" \( \
            -execdir find a \; -execdir mkdir -p a/$PROD_A/_ \; -o \
            -execdir find b \; -execdir mkdir -p b/$PROD_A/_ \; -o \
            -execdir find c \; -execdir mkdir -p c/$PROD_A/_ \; -o \
            -execdir find H \; -execdir mkdir -p H/$PROD_A/_ \; -o \
            -execdir find _ \; -execdir mkdir -p _/$PROD_A/_ \; \
        \) -o \
        -regex ".*_/b$S*/_$S*" \( \
            -execdir find a \; -execdir mkdir -p a/$PROD_B/_ \; -o \
            -execdir find b \; -execdir mkdir -p b/$PROD_B/_ \; -o \
            -execdir find c \; -execdir mkdir -p c/$PROD_B/_ \; -o \
            -execdir find H \; -execdir mkdir -p H/$PROD_B/_ \; -o \
            -execdir find _ \; -execdir mkdir -p _/$PROD_B/_ \; \
        \) -o \
        -regex ".*_/c$S*/_$S*" \( \
            -execdir find a \; -execdir mkdir -p a/$PROD_C/_ \; -o \
            -execdir find b \; -execdir mkdir -p b/$PROD_C/_ \; -o \
            -execdir find c \; -execdir mkdir -p c/$PROD_C/_ \; -o \
            -execdir find H \; -execdir mkdir -p H/$PROD_C/_ \; -o \
            -execdir find _ \; -execdir mkdir -p _/$PROD_C/_ \; \
        \) \
    \) \
\) &> /dev/null

# Output the result. (_/H/c/c/c/c/c/c/a/_)
find _ -depth ! -empty -name _ -execdir find _ -empty \; -quit
 

Online Bash Shell

Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.

About Bash

Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.

Features

  • Open source GNU project
  • Read and execute the commands from a Shell Script
  • Can be invoked by either single-character command line options (-a, -b, -c, -i, -l, -r, etc. ) or by multi-character command line options also like --help, --debugger,--login, etc.
  • Consists of Key bindings
  • Available in restricted mode for the environment security
  • Contains one-dimensional arrays to manipulate the lists of data.

Syntax help

Variables

name="Foo"
echo $name
echo "$name"
echo "${name}"

Conditional Statements

If

if [ conditional-expression ]  
then  
statements  
fi  

If-else

if [ conditional-expression ]  
then  
   statements  
else  
  statements
fi  

Else-If

if [ conditional-expression ]  
then  
   statements  
elif [ conditional-expression ]  
then  
 statements  
else  
  statements
fi