Xcode Pre-Action Scripts not working? - ios

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

Related

Dockerfile ENV not replacing in sed

I have set up dockerfile with making changes to config files with sed.
The image is a simple rtmp restreamer.
The lines in dockerfile go like:
RUN sed -i 's/ytkey/${YOUTUBE_KEY}/g' /etc/nginx/nginx.conf &&\
sed -i 's/fbkey/${FACEBOOK_KEY}/g' /etc/nginx/nginx.conf
i set up ENV at the beginning
ENV YOUTUBE_KEY=default FACEBOOK_KEY=default
but after building the image it doesn't replace ytkey or fbkey as default but inputs a string ${YOUTUBE_KEY}.
I have tried running
RUN sed -i 's/ytkey/\${YOUTUBE_KEY}/g' /etc/nginx/nginx.conf &&\
sed -i 's/fbkey/\${FACEBOOK_KEY}/g' /etc/nginx/nginx.conf
But had the same result. Is it a problem with sed (having the env in '') or something else? Is there an alternative to sed that works with dockerfile?
Or maybe i'm doing things completely the wrong way?
You can use double quotation marks around environment variables like this:
RUN sed -i 's/user/'"${YOUTUBE_KEY}"'/g' /etc/nginx/nginx.conf &&\
sed -i 's/pid/'"${FACEBOOK_KEY}"'/g' /etc/nginx/nginx.conf
I realise the question is specifically about sed, but you might also consider using envsubst to do this, especially if you start to use more variables.
It processes stdin to stdout, so in your example you would need to use a temp file strategy, but personally I prefer that approach:
envsubst < /etc/nginx/nginx.conf > /etc/nginx/nginx.conf.tmp
mv /etc/nginx/nginx.conf.tmp /etc/nginx/nginx.conf
For more complex configs, your templates would often be in a different directory anyway, so this wouldn't be necessary.

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

Makefile multiline command does not work in Docker properly

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' $#

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 "....")

sed returning empty line on second pass

I have a configuraton file that I am editing and on the first pass the is correctly changed but on the next two lines sed returns the lines blank.
The lines to be edited are
word.word.word.database=dbase
word.word.word.username=someone
word.word.word.password=someone
The sed commands I am using are
cat config.file | \sed -e "s/database/$dbname/" > config.file.1
cat config.file.1 | \sed -e "s/username/$dbuser/" > config.file.2
cat config.file.2 | \sed -e "s/database/$password/" > config.file.3
cp config.file.3 config.file
The end result is
word.word.word.database=dbname
word.word.word.username=
word.word.word.password=
Can't figure out what is going wrong with this. Any help would be great.
Thanks!
$dbname , $dbuser and $password are begin treated like shell variables, hence the leading dollar signs. If you're trying to incorporate shell vars, try:
sed -i -e "s/database/$dbname/" -e "s/username/$dbuser/" -e "s/password/$password/" config.file
Notice that I've added the -i flag to the above command. It enables in-place editing using GNU sed. Other types of sed require an extension to be set, like: -i.bak and this creates a back up file; in your case this would be: config.bak.
If you're just looking to get the full list of results, either drop the dollarsigns or use single quotes. For example:
sed -i -e "s/database/dbname/" -e "s/username/dbuser/" -e "s/password/password/" config.file
or
sed -i -e 's/database/$dbname/' -e 's/username/$dbuser/' -e 's/password/$password/' config.file
EDIT1:
If I've completely mis-understood your question, try this:
sed -i -e "s/\(.*database=\).*/\1dbname/" -e "s/\(.*username=\).*/\1dbuser/" -e "s/\(.*password=\).*/\1password/" config.file
Results:
word.word.word.database=dbname
word.word.word.username=dbuser
word.word.word.password=password
EDIT2:
If you have shell vars labelled $dbname , $dbuser and $password:
sed -i -e "s/\(.*database=\).*/\1$dbname/" -e "s/\(.*username=\).*/\1$dbuser/" -e "s/\(.*password=\).*/\1$password/" config.file

Resources