Makefile multiline command does not work in Docker properly - docker

I am trying to compile HTML from Markdown. My makefile looks like:
MD = pandoc \
--from markdown --standalone
# ...
$(MD_TARGETS):$(TARGET_DIR)/%.html: $(SOURCE_DIR)/%.md
mkdir -p $(#D); \
$(MD) --to html5 $< --output $#; \
sed -i '' -e '/href="./s/\.md/\.html/g' $#
When I run this on local machine everything works.
When I run the same in Docker I get the following error:
mkdir -p /project/docs; \
pandoc --from markdown --standalone --to html5 /project/source/changelog.md --output /project/docs/changelog.html; \
sed -i '' -e '/href="./s/\.md/\.html/g' /project/docs/changelog.html
sed: can't read : No such file or directory
makefile:85: recipe for target '/project/docs/changelog.html' failed
make: *** [/project/docs/changelog.html] Error 2
Consequent call of make gives the same error but with another file:
sed: can't read : No such file or directory
makefile:85: recipe for target '/project/docs/todo.html' failed
Obviously, make somehow tries sed earlier than HTML is done.
But I use multiline syntax ; \ of make so as to avoid using subshell.
I also tried && \ but neither of them works. What should I do?

Unfortunately you have been misled by your "obvious" conclusion Obviously, make somehow tries sed earlier than HTML is done :) That's not at all what the problem is. Review your error message more carefully:
sed: can't read : No such file or directory
Note the can't read :; there's supposed to be a filename there. It should say something like can't read foobar:. So clearly sed is trying to read a file with the name of empty string. Here's the line you're running:
sed -i '' -e '/href="./s/\.md/\.html/g' /project/docs/changelog.html
The clear culprit here is the empty string argument to -i. The sed man page describes the -i option as:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
Note how (a) the SUFFIX is optional, and (b) there's no space between the argument and its option. In your example you added a space, which causes sed to believe that the SUFFIX was not provided and to treat the next argument ('') as the filename to be operated on.
I'm not sure why it worked when you ran it from the command line: I have to assume that your command line version didn't include the empty string or else it didn't include a space between the -i and the ''.
Use one of:
sed -i'' -e '/href="./s/\.md/\.html/g' $#
or
sed --in-place= -e '/href="./s/\.md/\.html/g' $#

Related

Echo dynamic sed to file inside Dockerfile

I am working on a Dockerfile, inside of which I want to dynamically create a sed expression based on the input argument variable, and write this expression to a file.
Here's part of the Dockerfile:
FROM ubuntu
ARG VERSION
RUN echo $VERSION > /usr/local/testfile
RUN echo '#!/bin/sh \n\
sed -i "s/\"version\"/\${VERSION}/g" file' > /usr/local/foo.sh
the image builds fine.
When I start a container from that image, and inspect the files:
# cat /usr/local/testfile
0.0.1
# cat /usr/local/foo.sh
#!/bin/sh
sed -i "s/\"version\"/\${VERSION}/g" file
I notice that the $VERSION was not replaced correctly in the sed command. What am I missing here? I've tried a few different things (e.g. "$VERSION") but none of them worked.
I ended up breaking down the command. I created a variable for the sed command by using string concatenation and then I echoed that to the file separately:
FROM ubuntu
ARG VERSION
ENV command="sed -i s/\"version\"/""$VERSION""/g"
RUN echo '#!/bin/sh' > /usr/local/foo.sh
RUN echo $command >> usr/local/foo.sh
# cat /usr/local/foo.sh
#!/bin/sh
sed -i s/"version"/0.0.1/g

How to Search for a number and replace it with other number

I have a file server.xml with below line-
Server port="8007" shutdown="SHUTDOWN"
I want to search for 8007 and replace it with other number eg.: 8010.
How can I do it?
try this :
sed 's/8007/8010/g' yourfile
To make replacements you can use sed. I suppose that the new "port" is stored in a variable. As a result both solutions bellow works ok with GNU Sed:
$ a=$'Server port="8007" shutdown="SHUTDOWN"';echo "$a"
Server port="8007" shutdown="SHUTDOWN"
$ newport="8010"
$ sed -r "s/([0-9]+)/$newport/g" <<<"$a"
Server port="8010" shutdown="SHUTDOWN"
# Alternative:
$ sed -r 's/(port=")(.*)(" .*)/\1'"$newport"'\3/g' <<<"$a"
sed -r (or sed -E) enables extended regex groups.
If you need to apply replacements in-place (on the file) you need to use also -i switch in sed (sed -r -i.bak "....")

How to use Fred's ImageMagick textcleaner script?

I want to do OCR on some of my images, but images are not quite very impressive. So, for cleaning it I wanted to use Fred's ImageMagick Textcleaner script. Command that I gave:-
sh textcleaner.sh input_file output_file -g -e stretch -f 25 -o 20 -t 30 -u -s 1 -T -p 20
This is the arguments which Fred has given on website itself. I am also doing for same sample image. But I don't think so any of my options are working everything is by default. And I keep getting this error also
textcleaner.sh: line 177: type: textcleaner.sh: not found
usage: dirname path
usage: basename string [suffix]
basename [-a] [-s suffix] string [...]
And At last I had to keep the files in same folder where my textcleaner script is. How can I make it global and give the absolute path to it rather than putting the files wherever textcleaner is.
It's a bash script - it says so in the first line - yet you are trying to run it in sh - which is not bash. You need to make the script executable, by running
chmod +x textcleaner
then you can run it properly using:
./textcleaner ... arguments ...
That should make the error message go away. Then try showing us a sample image so we can try and see what the problem is.
In my ImageMagick scripts, the syntax is script name ...arguments... input output. So your command should be
bash textcleaner.sh -g -e stretch -f 25 -o 20 -t 30 -u -s 1 -T -p 20 input_file output_file
See my Pointers For Use (for further configuration) at my home page: http://www.fmwconcepts.com/imagemagick/index.php

Xcode Pre-Action Scripts not working?

Problem
I've attempted to add pre-action shell scripts that would switch on/off certain definitions in my .pch file depending on what I was building for.
However, when running a build, nothing happens. I'm not a fluent shell scripter, so the solution may be my incorrect syntax, but Xcode won't tell me anything.
Details
Here's some code:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i 's/source/working/' $prefix
sed -i 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD' $prefix
sed -i 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED' $prefix
sed -i 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD' $prefix
I added the first line to test if it would even remove a basic word I know is in the .pch file. It didn't. This leads me to believe that my path is invalid.
I've tried several different variations of the .pch file's path and have failed with all of them, though they all could have been wrong.
Thank you for your help
Your sed lines seem not to be correct. Try:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i -e 's/source/working/' $prefix
sed -i -e 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD/' $prefix
sed -i -e 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED/' $prefix
sed -i -e 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD/' $prefix

msys path conversion (or cygpath for msys?)

I need to pass /DEF:c:\filepath\myLib.def" command line option from a bash script to MS compiler/linker. The path is generated as part of build process by a bash script. Basically, the argument that my script passes is:
-DEF:/c/filepath/myLib.def
MSYS path conversion can't handle it properly because it doesn't understand /DEF: part. It works if I do
-DEF=/c/filepath/myLib.def
but then ms tools don't understand this parameter. In short, what's the proper way to write that parameter in MSYS bash so that it converts it to proper argument?
On cygwin I could use cygpath, but there is no equivalent, because somebody from msys thinks that it's not needed (even if there are scripts for cygwin that uses cygpath).
Update (Aug-2016):
This question is no longer relevant, as msys2 now comes with cygpath in its installation.
...
I'll summarize my research here.
The cygpath equivalent in MSYS is to use this command:
{ cd /c/some/path && pwd -W; } | sed 's|/|\\|g'
The problem with this approach is that it requires existing path, e.g. the c:\some\path has to be an existing directory; however, real cygpath supports paths that do not exist.
So, if you need to get path to a directory that doesn't exist, then you can fallback to sed conversion of the path:
{ cd 2>/dev/null /c/some/path && pwd -W ||
echo /c/some/path | sed 's|^/\([a-z,A-Z]\)/|\1:/|'; } | sed 's|/|\\|g'
The mouthful of slashes is there to satisfy quoting rules of sed. So, if c:\some\path doesn't exist on your PC, it will try to convert forward to back slashes and replace /c/ with c:\ (or any other drive letter). The only drawback for this is that it won't work correctly non-existing paths that contain a mounted component, such as /bin/does-not-exist or /usr/bin/does-not-exist.
One more approach is to use cygpath from cygwin in MSYS. It seems that cygwin sets global environment variable CYGPATH, that is, you can use it from regular cmd.exe:
%CYGPATH% -w /c/some/path
C:\some\path
or from MSYS:
$CYGPATH -w /c/some/path
C:\some\path
as long as you set to point /c to /cygdrive/c in cygwin.
But this approach will print you /usr located in cygwin installation, not in MSYS.
In short, I think msys should really include real cygpath in the default set of tools just for some cases that aren't handled automatically by msys command line argument conversion logic
use pwd -W
or
download cygpath for msys from here http://mingw.5.n7.nabble.com/enhanced-version-of-cygpath-td28556.html
and use cygpath -wa
Similar to dmitri-rubinstein# above, I've cleaned up the code a bit and added the reverse conversion as well.
winpath() {
if [ ${#} -eq 0 ]; then
: skip
elif [ -f "$1" ]; then
local dirname=$(dirname "$1")
local basename=$(basename "$1")
echo "$(cd "$dirname" && pwd -W)/$basename" \
| sed \
-e 's|/|\\|g';
elif [ -d "$1" ]; then
echo "$(cd "$1" && pwd -W)" \
| sed \
-e 's|/|\\|g';
else
echo "$1" \
| sed \
-e 's|^/\(.\)/|\1:\\|g' \
-e 's|/|\\|g'
fi
}
unixpath() {
echo "$1" \
| sed -r \
-e 's/\\/\//g' \
-e 's/^([^:]+):/\/\1/'
}
I am using this with msysgit:
winpath() {
if [ -z "$1" ]; then
echo "$#"
else
if [ -f "$1" ]; then
local dir=$(dirname "$1")
local fn=$(basename "$1")
echo "$(cd "$dir"; echo "$(pwd -W)/$fn")" | sed 's|/|\\|g';
else
if [ -d "$1" ]; then
echo "$(cd "$1"; pwd -W)" | sed 's|/|\\|g';
else
echo "$1" | sed 's|^/\(.\)/|\1:\\|g; s|/|\\|g';
fi
fi
fi
}
My bash foo is weak and I couldn't get regexes working in bash 3.1 so I hacked out a perl script for it:
#!/bin/env perl
use strict;
my #r;
foreach my $e (#ARGV) {
$e=~s/\//\\/g;
$e=~s/^\\([A-Za-z])\\/\1:\\/;
push #r, $e;
}
print join(" ", #r);
MSYS cygpath
Program
This program convert a DOS path to a UNIX path and vice versa
#!/bin/env perl
# DOS to UNIX path conversion
# © John S. Peterson. License GNU GPL 3.
use strict;
use Getopt::Std;
# usage
if ($#ARGV == -1) {
print 'Usage: cygpath (-w) NAME...
Convert Unix and Windows format paths
Output type options:
-w, --windows print Windows form of NAMEs (C:\WINNT)
';
exit 0;
}
# option
my %opt;
getopts('w', \%opt);
# convert path
my #r;
foreach my $e (#ARGV) {
if ($opt{w}) {
# add drive letter suffix
$e =~ s,^\/([A-Za-z])\/,\1:\/,;
$e =~ s,\/,\\,g;
} else {
$e =~ s,\\,\/,g;
# add leading slash
$e = "/$e";
# remove drive letter suffix
$e =~ s,:,,;
}
push #r, $e;
}
print join("\n", #r);
Compared to Cygwin cygpath
The output from this program is better than the output from Cygwin cygpath in MSYS because
Cygwin cygpath remove the Cygwin home from a converted path, f.e.
cygpath "$CYGWIN/usr/local/bin"
/usr/local/bin
which is a problem because
it's sometimes useful to convert a DOS Cygwin path to a UNIX path for the purpose of copying files from Cygwin to MSYS
This program doesn't remove the Cygwin home
cygpath "$CYGWIN/usr/local/bin"
/c/file/program/cygwin/usr/local/bin
Compared to automatic MSYS path conversion
Manual path conversion has a use in MSYS because
the automatic path conversion is inadequate
for f.e.
devkitPro make
How about this one ?
cmd //c echo <your path>
It may not work always but it is the shortest I found
This works for me
df "$1" | tac >k
read b <k
rm k
set "$1" $b
echo ${1/$7/$2/}
ref
nearly pure GNU bash solution (which is what you commonly run in MSYS) (interestingly not working with MSYS2, leave a comment if you know why):
#!/bin/bash
# check if we have cygpath (cygwin, newer MSYS2), then just use that
which cygpath 1>/dev/null 2>&1
[[ $? = 0 ]] && (cygpath -wa "$1"; exit $?)
# check if it looks like a Windows path, in which case we directly convert and exit
[[ ${1} =~ ^[a-zA-Z]: ]] && \
echo "${1}" | sed -e 's|/|\\|g' -e 's/\(.\)/\u\1/' && exit 0
# split first path entry (if any) with trailing slash and filename
[[ ${1} =~ ^\([/a-zA-Z0-9_.-]\\w*/\)?\(.*\)$ ]]
chk_root="${BASH_REMATCH[1]}"
chk_rest="${BASH_REMATCH[2]}"
# check if the root path exists and more important: let pwd binary resolve the translation according to the mount
chk_winroot="$(cd "${chk_root}." 2>/dev/null && pwd -W)"
[[ "${chk_winroot}" == "" ]] && echo "${chk_root}: No such file or directory" && exit 1
# using substition to replace all / by \ and uppercasing the first character
# pure bash solution; sadly: the first part needs a newer bash than old MSYS have ...
# chk_drv="${chk_winroot:0:1}"
# chk_all="${chk_winroot:1}/${chk_rest}"
# echo "${chk_drv^^}${chk_all//\//\\}"
# ... so fallback to GNU sed
echo "${chk_winroot}/${chk_rest}" | sed -e 's|/|\\|g' -e 's/\(.\)/\u\1/'
There's still an issue with it: if MinGW's fstab contains an entry like /mnt/c the pwd -W of /mnt/. done in this script won't work.
To fix it: replace pwd -W by inspecting $ cat /etc/fstab | cut -d'#' -f1 | grep -v "^\s*$" entries manually and replace the first match - while this will never work for cygwin or msys2 which use a different format this is covered by using cygpath there.

Resources