0

I would like to setup a makefile that reads user input with Bash autocompletion, and based on that create a directory where some files would be copied into. Basically something like

$ make initializeProject
Enter destination: Dir<ectory>/Sub<directory>/ProjectDirectory

mkdir: created directory 'Directory/Subdirectory/ProjectDirectory'
'Templates/TemplateFile' -> 'Directory/Subdirectory/ProjectDirectory/TemplateFile'

where < X > stands for autocompleted parts.

As of now my makefile does not autocomplete, and it looks like this:

initProjectLaTeX:
        @read -p "Enter destination: " destination; \
        mkdir -pv $$destination; \
        cp -iv ~/.templates/latex/* $$destination

I have tried this option, but it produces the error /bin/sh: 1: read: Illegal option -e. How can I access Bash autocompletion here?

aahlback
  • 82
  • 1
  • 5

2 Answers2

0

make uses /bin/sh as default shell and on many systems it's not the same as bash (e.g. on Debian is dash). I suspect that the -e option of read is a bash extension, that is why you get the error:

/bin/sh: 1: read: Illegal option -e

setting /bin/bash as the SHELL in the Makefile should work:

Makefile

SHELL := /bin/bash

initProjectLaTeX:
    @read -e -p "Enter destination: " destination; \
    mkdir -pv $$destination; \
    cp -iv ~/tmp/*c $$destination

example

$ make initProjectLaTeX
Enter destination: foo/bar/baz
mkdir: created directory 'foo'
mkdir: created directory 'foo/bar'
mkdir: created directory 'foo/bar/baz'
'/home/marco/tmp/foo.c' -> 'foo/bar/baz/foo.c'
$ tree foo
foo
└── bar
    └── baz
        └── foo.c

2 directories, 1 file
$

completion

$ make initProjectLaTeX
Enter destination: foo
foo/   foo.c
Enter destination: foo
foo/   foo.c
Enter destination: foo
MarcoLucidi
  • 2,007
  • 1
  • 5
  • 8
0

Rather than have make interact with the user, pass the desired destination to make.

Start with a modified recipe:

initProjectLaTeX:
        mkdir -pn $(DESTINATION)
        cp -iv ~/.templates/latex/* $(DESTINATION)

then run

make initProjectLaTeX DESTINATION=Dir<ectory>/Sub<directory>/ProjectDirectory

(bash allows completion for the value of an assignment.)

chepner
  • 497,756
  • 71
  • 530
  • 681