How to get Makefile to only produce a file name without extension? - bash

I'm trying to make a Makefile that exports a markdown file to a pdf file that uses the same filename as the original markdown file. I used "basename" command but it produces "inputfile.md.pdf" instead of "inputfile.pdf".
Please see my code below (I adapted a code I found on the Internet. Thank you!):
.PHONY: pdf docx apa format
FILES := $(wildcard ./*.md)
pdf:
for file in $(FILES); do \
pandoc $$file \
--bibliography mypath \
--csl mypath \
--filter pandoc-citeproc \
--template eisvogel \
-o $(basename $$file).pdf; \
open $(basename $$file).pdf; \
done
Anyone who can help me? I'm a novice in Makefile (and programming in general) so any detailed help would be very much appreciated.
I also tried these codes below, but they generated an error message:
-o $(basename -s ".md" $$file).pdf; \
-o $(basename -s .md $$file).pdf; \

The way you write $(basename …) you get the basename make function. This would normally the right thing, but you try to reference a shell variable file in its argument, which is unavailable at the make layer.
In this case, it is probably easiest to call the basename shell utility, at the shell level. Therefore, you need to escape the $ to get shell substitution, like this:
-o "$$(basename -s .md $$file)".pdf; \
open "$$(basename -s .md $$file)".pdf; \
Alternatively, you could try to move the loop to the make layer, perhaps using foreach.

Related

openapi-generator: command not found for bash script

I have a generate.sh file with below lines of code,
#!/bin/bash
openapi-generator generate -g aspnetcore \
--template-dir ${PWD}/openapi-generator-aspnetcore3-templates/ \
--additional-properties aspnetCoreVersion=3.1 \
--additional-properties classModifier=abstract \
--additional-properties operationModifier=abstract \
--additional-properties packageName=RedHat.TodoList \
--additional-properties packageTitle=TodoList \
-i todo_openapi.yaml \
-o ${PWD}
When I am trying to run it Git Bash tool it's throwing error ./generate.sh: line 3: openapi-generator: command not found, I can see openapi-generator-cli commands working fine.
In generate.sh, replace openapi-generator with openapi-generator-cli instead.
(openapi-generator is usually installed via brew on Mac)
Looks like PATH problem.
Use which openapi-generator in terminal or Git Bash, whichever works for you, to find openapi-generator path first.
$ which openapi-generator
/usr/local/bin/openapi-generator
Then add the path to PATH variable in your generate.sh, e.g. put the following line before you call openapi-generator.
PATH+=:/usr/local/bin

Looping through files defined by `*` in a variable in BASH

I want to be able to specify a directory with fastq files with a script that will loop through all the files and do some stuff. Here is my attempt:
threads=24
current_path=`pwd`
input_file=${current_path}/raw/
files=${current_path}/raw/*
for file in ${files}; do
output_file=${current_path}/${file}_out/
mkdir -m gu=wrx,o=rx ${output_file}
spades.py \
--s1 ${input_file}${file} \
-t ${threads} \
--plasmid \
--careful \
-o ${output_file}
done
So in this script I get an error: cannot make directory, directory does not exist The script generates a /home folder. I don't know if I am specifying the files incorrectly or if I am using the for loop incorrectly.
Thank you!
you concatenate full path to file with folder in line
output_file=${current_path}/${file}_out/
it should be
output_file=${file}_out/

How does "make dist" command work? I'm stuck on a script which I need to update to add new file and folder

I've to add a file and a folder to the zip that is created when I run make dist command. This is an open-source project.
After research, I understood I've to modify the Makefile.am but examples online don't work or match with my current Makefile.am
Makefile.am
SUBDIRS = bin data po src extensions docs
DISTCLEANFILES = \
intltool-extract \
intltool-merge \
intltool-update
EXTRA_DIST = \
$(bin_SCRIPTS) \
intltool-merge.in \
intltool-update.in \
intltool-extract.in
DISTCHECK_CONFIGURE_FLAGS = --disable-update-mimedb
check-po:
#for i in $(top_srcdir)/po/*.po ; do \
if ! grep -q ^`basename $$i | \
sed 's,.po,,'`$$ $(top_srcdir)/po/LINGUAS ; then \
echo '***' `basename $$i | \
sed 's,.po,,'` missing from po/LINGUAS '***' ; \
exit 1; \
fi; \
done;
lint:
flake8 --ignore E402 $(top_srcdir)/src $(top_srcdir)/extensions
test: lint check-po
PYTHONPATH=$(pkgdatadir)/extensions:$(PYTHONPATH) \
python -m sugar3.test.discover $(top_srcdir)/tests
configure.ac
AC_INIT([Sugar],[0.114],[],[sugar])
AC_PREREQ([2.59])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([configure.ac])
SUCROSE_VERSION="0.114"
AC_SUBST(SUCROSE_VERSION)
AM_INIT_AUTOMAKE([1.9 foreign dist-xz no-dist-gzip])
AM_MAINTAINER_MODE
PYTHON=python2
AM_PATH_PYTHON
AC_PATH_PROG([EMPY], [empy])
if test -z "$EMPY"; then
AC_MSG_ERROR([python-empy is required])
fi
PKG_CHECK_MODULES(SHELL, gtk+-3.0)
IT_PROG_INTLTOOL([0.35.0])
GETTEXT_PACKAGE=sugar
AC_SUBST([GETTEXT_PACKAGE])
AM_GLIB_GNU_GETTEXT
AC_ARG_ENABLE(update-mimedb,
AC_HELP_STRING([--disable-update-mimedb],
[disable the update-mime-database after install [default=no]]),,
enable_update_mimedb=yes)
AM_CONDITIONAL(ENABLE_UPDATE_MIMEDB, test x$enable_update_mimedb = xyes)
GLIB_GSETTINGS
AC_CONFIG_FILES([
bin/Makefile
bin/sugar
data/icons/Makefile
data/Makefile
extensions/cpsection/aboutcomputer/Makefile
extensions/cpsection/aboutme/Makefile
extensions/cpsection/background/Makefile
extensions/cpsection/backup/Makefile
extensions/cpsection/backup/backends/Makefile
extensions/cpsection/datetime/Makefile
extensions/cpsection/frame/Makefile
extensions/cpsection/keyboard/Makefile
extensions/cpsection/language/Makefile
extensions/cpsection/modemconfiguration/Makefile
extensions/cpsection/Makefile
extensions/cpsection/network/Makefile
extensions/cpsection/power/Makefile
extensions/cpsection/updater/Makefile
extensions/cpsection/webaccount/services/Makefile
extensions/cpsection/webaccount/Makefile
extensions/deviceicon/Makefile
extensions/globalkey/Makefile
extensions/webservice/Makefile
extensions/Makefile
Makefile
po/Makefile.in
src/jarabe/config.py
src/jarabe/controlpanel/Makefile
src/jarabe/desktop/Makefile
src/jarabe/frame/Makefile
src/jarabe/intro/Makefile
src/jarabe/journal/Makefile
src/jarabe/Makefile
src/jarabe/model/Makefile
src/jarabe/model/update/Makefile
src/jarabe/util/Makefile
src/jarabe/util/telepathy/Makefile
src/jarabe/view/Makefile
src/jarabe/webservice/Makefile
src/Makefile
])
AC_OUTPUT
When I run the command make dist, the output zip doesn't include a file and folder which I now need to add. I'm not able to understand where in the code(Makefile.am or configure.ac) should I make changes.
I've to add a file and a folder to the zip that is created when I run make dist command.
I take it that these are not already included in the distribution. If you're not sure, then check -- Automake-based build systems such as yours identify a lot of files automatically for inclusion in distribution packages.
Supposing that these files are not already included, there are several ways to cause them to be. Easiest would be to add them to the EXTRA_DIST variable, yielding
EXTRA_DIST = \
$(bin_SCRIPTS) \
intltool-merge.in \
intltool-update.in \
intltool-extract.in \
a_directory \
some_file.ext
Don't forget the trailing backslashes if you continue with the multiline form (which I like, as I find it much easier to read). You can specify a path to the file, the directory, or both. Do note that in the case of the directory, it will be not just the directory itself but all its contents, recursively, that are included in the distribution. This is all documented in the manual.
If you need finer control, then there is also an extension point for managing the contents of the distribution in the form of the "dist hook". This comprises a make target named dist-hook. Like any other literal make rule in your Makefile.am, any rule you provide for building that target is copied to the final generated Makefile, and if such a rule is present then its recipe is run as part of building the distribution, after the distribution directory is otherwise populated but before the archive file is built from that. You can write more or less arbitrary shell code in that target's recipe to tweak the distribution. Follow the above link to the documentation for the gory details.
EXTRA_DIST variable sounds like the thing.

Unescape the ampersand (&) via XMLStarlet - Bugging &

This a quite annoying but rather a much simpler task. According to this guide, I wrote this:
#!/bin/bash
content=$(wget "https://example.com/" -O -)
ampersand=$(echo '\&')
xmllint --html --xpath '//*[#id="table"]/tbody' - <<<"$content" 2>/dev/null |
xmlstarlet sel -t \
-m "/tbody/tr/td" \
-o "https://example.com" \
-v "a//#href" \
-o "/?A=1" \
-o "$ampersand" \
-o "B=2" -n \
I successfully extract each link from the table and everything gets concatenated correctly, however, instead of reproducing the ampersand as & I receive this at the end of each link:
https://example.com/hello-world/?A=1\&B=2
But actually, I was looking for something like:
https://example.com/hello-world/?A=1&B=2
The idea is to escape the character using a backslash \& so that it gets ignored. Initially, I tried placing it directly into -o "\&" \ instead of -o "$ampersand" \ and removing ampersand=$(echo '\&') in this case scenario. Still the same result.
Essentially, by removing the backslash it still outputs:
https://example.com/hello-world/?A=1&B=2
Only that the \ behind the & is removed.
Why?
I'm sure it is something basic that is missing.
& is the correct way to print & in an XML document, but since you just want a plain URL your output should not be XML. Therefore you need to switch to text mode, by passing --text or -T to the sel command.
Your example input doesn't quite work because example.com doesn't have any table elements, but here is a working example building links from p elements instead.
content=$(wget 'https://example.com/' -O -)
xmlstarlet fo --html <<<"$content" |
xmlstarlet sel -T -t \
-m '//p[a]' \
--if 'not(starts-with(a//#href,"http"))' \
-o 'https://example.com/' \
--break \
-v 'a//#href' \
-o '/?A=1' \
-o '&' \
-o 'B=2' -n
The output is
http://www.iana.org/domains/example/?A=1&B=2
As you have already seen, backslash-escaping isn't the solution here. I can think of two possible options:
Extract the hrefs (probably don't need to be using both xmllint and xmlstarlet to do this), then just use a standard text processing tool such as sed to add the start and the end:
sed 's,^,https://example.com/,; s,$,/?A=1\&B=2,'
Alternatively, pipe the output of what you've currently got to xmlstarlet unesc, which will change & into &.
Sorry I can't reproduce your result but why don't make substitutions? Just filter your results through
sed 's/\\&/\&/g'
add it to your pipe. It should replace all & to &.

*** missing separator. Stop. Make file

i'm getting this error:
make:24: *** missing separator. Stop.
Although i changed all space character with tab in line 24.
Line24:arm_v5t_le-gcc $FILES $INCLUDES $LIBS -o $TARGET
Here is the code:
#DM_serial2_make
export PATH="$PATH:/opt/mv_pro_5.0/montavista/pro/devkit/arm/v5t_le/bin:/opt/mv_pro_5.0/montavista/pro/bin:/opt/mv_pro_5.0/montavista/common/bin"
TARGET="/home/cilem/Desktop/06.05.2012/DM_serial2"
INCLUDES=" -I /home/cilem/Desktop/06.05.2012/libxml2 \
-I /home/cilem/Desktop/06.05.2012/gstreamer-0.10 \
-I /home/cilem/Desktop/06.05.2012/gstreamer-0.10/gst/interfaces \
-I /home/cilem/Desktop/06.05.2012/glib-2.0 \
-I /home/cilem/Desktop/06.05.2012/glib-2.0/include"
LIBS=" -L /home/cilem/Desktop/06.05.2012/lib/ -l:libgstreamer-0.10.so.0 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libgstinterfaces-0.10.so.0 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libgobject-2.0.so.0 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libgmodule-2.0.so.0 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libxml2.so.2 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libgthread-2.0.so.0 \
-L /home/cilem/Desktop/06.05.2012/lib/ -l:libglib-2.0.so.0"
FILES="DM_serial2.c"
arm_v5t_le-gcc $FILES $INCLUDES $LIBS -o $TARGET
That looks like a shell script. Shell scripts are not makefiles, and vice versa. You need to find a good tutorial on make, or read the GNU make manual.
For example, you should not have any quoting in your variable values.
Second, variable expansions in make require the variables to be surrounded by parens or curly braces: $(FILES) or ${FILES}.
Third, as piokuc says, that line is not a valid make rule. A make rule has the form:
<target> : <dependencies...>
<commands...>
where the indentation of the commands... must be TAB characters. This rule says "you can build target whenever it's older than any of dependencies... by running commands...". The target and dependencies must (usually) be files, so you definitely don't want to use $(INCLUDES) or $(LIBS) in that list as those are compiler flags.
You probably want something like this, although it could be improved:
$(TARGET): $(FILES)
arm_v5t_le-gcc $(FILES) $(INCLUDES) $(LIBS) -o $(TARGET)
You've got other weird things here. You don't need to provide the same directory over and over with the -L flag. Once is enough. Also I'm not familiar with the -l:libfoo.a construct; usually it's just -lfoo.
I think the last line should be replaced with something like:
$TARGET: $FILES $INCLUDES $LIBS
arm_v5t_le-gcc $FILES $INCLUDES $LIBS -o $TARGET
The above line (the one starting with arm_v5t_le-gcc) should start with a tab, not spaces.

Resources