Alpine image in gitlab CI: find does not replace {} - find

I have the following script setup in my gitlab ci:
find . -iname "*.tf" -exec envsubst < {} ;
However, I get an error message "/bin/sh: eval: line 119: can't open {}: no such file".
I also tried putting single quotes around {}, but got the same error. Maybe I did not understand find correctly? Does the shell syntax in Alpine ash differ from standard sh or bash and it does not work because of that?

the redirection < applies to find and not to envsubst as you're expecting

Related

How do I create my own custom command line script in zsh?

I want to be able to run simple one line commands on my terminal, but instead of them being complicated I want them to be very simple, writing my own commands for these complex command lines that I have to write out so frequently. I am using the zsh command line and for example I would want to do this.
% npx tailwindcss -i ./static/styles.css -o ./static/output.css --watch
into something much easier to read such as
% build tailwindcss
Another problem/example is where if I would like to use http-server, but I want to turn off the caching, I don't think you can change the default settings for http-server module to turn off the caching, I have to do this:
% http-server -c-1
I know it is not too much of a big deal but I would like to at least pretend that the zero caching is the default. So I could do something like this with the command line:
% run server
And that would just run the server.
Also as a side note if this is not the best command line tool to do stuff like this in like maybe bash would be better I would be open to using a different command line tool as well.
Using aliases
If you run the exact same command every time you could alias it like this:
alias http-server="http-server -c-1"
alias <alias-name>="npx tailwindcss -i ./static/styles.css -o ./static/output.css --watch"
Now http-server expands to http-server -c-1 and <alias-name> expands to npx tailwindcss -i ./static/styles.css -o ./static/output.css --watch
Using functions
If you want to choose the input file each time you could write a function instead:
build(){
npx tailwindcss -i $1 -o ./static/output.css --watch
}
Then you can build with build file.css
Add the alias or function to ~/.zshrc if you want it available every time you start zsh
Using bck-i-search
Use Ctrl+r to search history. Pressing Ctrl+r and writing "tailwind" will likely show you the full command. You can even comment the function with some arbitrary words to make them easier to find in history (e.g. "aaa")
npx tailwindcss -i ./static/styles.css -o ./static/output.css --watch #aaa

How can I get zsh to inherit the complete autocompletion?

I have an little shell script (named "run") which redirects all output of a program to /dev/null:
#!/bin/bash
$# &> /dev/null &
disown +
How can I say zsh that the whole autocompletion shall work for this?
I mean
$ run git com<TAB>
autocomplete to
$ run git commit
I was able to make that work by adding:
compdef _command run
to my .zshrc file.
I've based my answer on this bash question. It was worth giving it a try with compdef - surprisingly it worked.
As I'm still zsh/autocompletion newbie I cannot explain the inner workings and you should probably go through the documentation or other sources to find more on the topic.

Multiple mongo commands in kubernetes not working

I am attempting to automate the following series of commands which work correctly into a BASH script:
kubectl exec -it mongo-pod -- bash
mongo DBNAME
db.auth("theUser", "thePw")
db.theCollection.find()
The script I am using is as follows:
#!/bin/bash
kubectl exec -it mongo-pod -- bash -c "mongo DBNAME && /
db.auth("theUser", "thePw") && /
db.theCollection.find()"
I have tried the following:
Executing multiple commands( or from a shell script) in a kubernetes pod
but any commands that are added after the first using & or && are not executed. For example just using "mongo DBNAME" correctly opens the prompt and sets it to the correct db, but adding any other command with && causes all commands to fail with the following:
bash: -c line 0: syntax error near unexpected token 'theUser'
All of the comments are spot on, but at least two things I have the highest confidence I have the answer to:
First, you have the line continuation character wrong; it should be \ and not /. It actually wouldn't even be required if you switched bash into "exit on error" mode, with
kubectl exec -it mongo-pod -- bash -ec "mongo DBNAME
echo 'this command only runs if mongo exits a-ok'
exit 1
and this never will run
"
However, the other mistake is around the quoting characters used: if you have bash -c " then you must either use the single-quote for the interior string literals, or escape them with \". You can actually see what I'm talking about by looking at the syntax highlighting of the shell snippet in your question. Observe that the string literal is red, but then the text theUser as well as thePw are both black -- that's because they are outside the string literal since the string stopped at the first " it encountered -- the one present in db.auth("
It is almost always the case that you'll want to use single quotes when invoking bash remotely like that, for several reasons but the most relevant is that you can then use db.auth("something") without having to unnecessarily escape the double quotes.
Since mongo (like many interpreters such a node and python) wants you to either type in it interactively, provide the input on its "standard input", or give it a local file containing commands, you will want to change the invocation to one of those strategies depending on your needs.
A very convenient way of redirecting standard input without having to use echo or printf and its associated quoting hell is to use what are called "here documents" (abbreviated "heredocs") in bash:
kubectl exec -it mongo-pod -- bash -ec 'mongo DBNAME<<"FOO"
db.auth("theUser", "thePw")
printjson(db.theCollection.find())
FOO
'
That causes bash to transmit almost all characters between the two "heredoc delimiters" to the standard input of the command. If you quote the delimiter, as I have with the [arbitrary] word FOO, then the contents are not subject to variable expansion, command interpolation, etc, which can be one more mechanism to avoid backtick and dollarsign weirdness.

Why Parameter Expansion is not working? [duplicate]

#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
This bash script gives me Bad substitution error on ubuntu. Any help will be highly appreciated.
The default shell (/bin/sh) under Ubuntu points to dash, not bash.
me#pc:~$ readlink -f $(which sh)
/bin/dash
So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh, or if you run it with bash your_script_file.sh, it should work fine.
Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash, which does not support that string substitution syntax.
I had the same problem. Make sure your script didnt have
#!/bin/sh
at the top of your script. Instead, you should add
#!/bin/bash
For others that arrive here, this exact message will also appear when using the env variable syntax for commands, for example ${which sh} instead of the correct $(which sh)
Your script syntax is valid bash and good.
Possible causes for the failure:
Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution. Because your script looks fine and works with bash.
Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.
If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash). Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.
Try running the script explicitly using bash command rather than just executing it as executable.
Also, make sure you don't have an empty string for the first line of your script.
i.e. make sure #!/bin/bash is the very first line of your script.
Not relevant to your example, but you can also get the Bad substitution error in Bash for any substitution syntax that Bash does not recognize. This could be:
Stray whitespace. E.g. bash -c '${x }'
A typo. E.g. bash -c '${x;-}'
A feature that was added in a later Bash version. E.g. bash -c '${x#Q}' before Bash 4.4.
If you have multiple substitutions in the same expression, Bash may not be very helpful in pinpointing the problematic expression. E.g.:
$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution
Both - bash or dash - work, but the syntax needs to be:
FILENAME=/my/complex/path/name.ext
NEWNAME=${FILENAME%ext}new
I was adding a dollar sign twice in an expression with curly braces in bash:
cp -r $PROJECT_NAME ${$PROJECT_NAME}2
instead of
cp -r $PROJECT_NAME ${PROJECT_NAME}2
I have found that this issue is either caused by the marked answer or you have a line or space before the bash declaration
Looks like "+x" causes problems:
root#raspi1:~# cat > /tmp/btest
#!/bin/bash
jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
root#raspi1:~# chmod +x /tmp/btest
root#raspi1:~# /tmp/btest
root#raspi1:~# sh -x /tmp/btest
+ jobname=job_201312161447_0003
/tmp/btest: 4: /tmp/btest: Bad substitution
in my case (under ubuntu 18.04), I have mixed $( ${} ) that works fine:
BACKUPED_NB=$(ls ${HOST_BACKUP_DIR}*${CONTAINER_NAME}.backup.sql.gz | wc --lines)
full example here.
I used #!bin/bash as well tried all approaches like no line before or after #!bin/bash.
Then also tried using +x but still didn't work.
Finally i tried running the script ./script.sh it worked fine.
#!/bin/bash
jobname="job_201312161447_0003"
jobname_post=${jobname:17}
root#ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts# sh jaru.sh
jaru.sh: 3: jaru.sh: Bad substitution
root#ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts# ./jaru.sh
root#ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts#

Bash: rewriting emacs command in bash_profile

I'm trying to write a command in my bash_profile to replace the default emacs command that will emacs a file if it exists, and if it doesn't exist, will copy a template to the new file and then emacs that.
What I attempted was:
function emacs()
{
if [ ! -f ${1} ]; then \
cp /FILEPATH/template.sh ${1}; fi;
builtin emacs ${1}
}
but the error I'm getting is "-bash: builtin: emacs: not a shell builtin"
How do I create a new function to replace the emacs command and then call the original emacs command within that function if emacs is not a builtin command?
You want command emacs, not builtin emacs. See help command.
As an aside, doesn't emacs have some sort of internal support for new-file templates? You might want to take a look at this: http://www.emacswiki.org/emacs/TemplatesMode. (Disclaimer: I don't use emacs; this was just one of the first pages I found while searching for "emacs new file template.)
(edit: sorry, command emacs in the other answer is better, use that.)
builtin only works for actual shell builtins like test, read, ...
Use the explicit path to emacs instead:
function emacs()
{
if [ ! -f ${1} ]; then \
cp /FILEPATH/template.sh ${1}; fi;
/usr/bin/emacs ${1}
}