This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I've this code in my script:
no_forward="#net.ipv4.ip_forward=1"
forward="net.ipv4.ip_forward=1"
sed -i 's/$no_forward/$forward/' /etc/sysctl.conf
Based on the man page, -i suffix is not neccesary, but this this use I'll only have the modified file instead both of them (the file modified and the "backup", the previous one).
I need to use that vars, because I needing them after that command so It's useful to have them like that. I guess I should be wrong with the pattern string, but right now I can't find why. Or maybe the problem are the var's strings or their symbols?
Could You help me? I accept other solutions non-sed based if they use bash and don't need special commands, since I'll need to use the script in another computer without installing anymore.
Thanks for reading
If you want to be able to use vars in sed, use double quotes, so :
sed -i "s/$no_forward/$forward/" /etc/sysctl.conf
Related
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
I have a command I want to run:
sbt "testOnly com.example.testClass"
which needs to be ran with the quotes. However, what I really want to do is be able to pass the argument in a variable, while keeping the quotes.
This does not work:
TEST_CMD="\"testOnly com.example.testClass\""
sbt $TEST_CMD
This does work:
TEST_CMD="\"testOnly com.example.testClass\""
eval sbt $TEST_CMD
I read http://mywiki.wooledge.org/BashFAQ/050 and now I understand why the first doesn't work, and I've also learned that eval can be insecure and should be avoided (this is just an internal Jenkins job, would it ever be an issue?).
Also in the article, it mentioned adding the command to an array first, so I tried:
args=("\"testOnly com.example.testClass\"")
sbt "${args[#]}"
but that also does not run correctly. What's the best way to do this? Is it really that bad to use eval in my case?
Quote the variable expansion rather than the assignment.
TEST_CMD="testOnly com.example.testClass"
sbt "$TEST_CMD"
This question already has answers here:
What does double-dash do when following a command?
(4 answers)
Closed 3 years ago.
https://docs.docker.com/compose/startup-order/
Could someone point me to some docs or general knowledge of what the "--" means in this docker command?
command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
I've always been weak at bash and unix things and wanted to learn more.
The precise semantics of options depend entirely on the command. In this case, examine the documentation or source code of wait-for-it.sh. But commonly, a double dash is used to signal the end of options. Here, it looks like the arguments before the double dash are parameters for the script itself, and the stuff after the double dash is a command line (which could conceivably contain additional options, which should not be misunderstood as options for wait-for-it.sh itself).
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 5 years ago.
How do I expand a brace expansion that originally come from a string variables ? Note that the string variable is a requirement.
#!/usr/bin/env bash
TEXT_DIRS='opt/*/{doc,gtk-doc}'
My intention is reading a bash source from zsh, or maybe other language as well such as Perl or Python. Get the configuration from /etc/makepkg.conf, as below.
DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})
It is all, for just, learning purpose.
Is that possible, to expand from string ?
The tricky thing here is that once Bash resolves the environment variable, it doesn't make another pass to process its contents again. You'd have to evaluate the content of the variable in another pass of the shell ( eg another shell command).
Here's one way to do that:
bash-4.4# TEXT_DIRS='/usr/*/{bin,src,lib}'
bash-4.4# bash -c ls\ $TEXT_DIRS
ls: /usr/*/src: No such file or directory
/usr/local/bin:
/usr/local/lib:
Here, I'm dynamically generating a shell command that I then evaluate to handle the 2nd expansion. (I took the liberty of changing the paths to something that would match on typical systems, so make sure to change it back if you try to test).
Dynamically generating code is always dangerous, if you can't trust the input. That's essentially how command injection attacks work. But use of eval in your own shell with trusted input is more or less "safe", though I rarely find myself using it unless in a contrived scenario like yours, or some of my own worse ideas.
This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 5 years ago.
I've been trying to write a bash script. A part of it is supposed to replace a part of a string with nothing.
Here's what I'm trying to do
$dbname=$1
$dbNameActual="${$dbname/.sql/}"
date
echo $dbNameActual
I tried a number of suggestions from stack. But got nowhere. I tried adding sed, but that didn't seem to work.
The idea is that I have a script, and it takes in a db import file name, say db250317.sql and outputs db250317 .
I'm running Ubuntu 16.04 LTS.
You don't put $ twice in the expression, and you don't put $ before the variable you're assigning to (this isn't PHP or Perl). It should be:
dbNameActual="${dbname/.sql/}"
Also, if the thing you're trying to delete is always at the end, you can use % to remove it:
dbNameActual="${dbname%.sql}"
Also remember to quote the variable when you use it later, in case the filename contains spaces. You should almost always quote variables, unless you have a specific reason not to.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I've been attempting to write a Bash script that automates everything needed to add a new piece of equipment to our MRTG graphs. Part of that requires me to edit a cfg file which I've read can be done with the sed command. The lines pasted below are where the error occurs when running the script giving me a "unexpected EOF while looking for matching `"' " error. town, tower, equipment, and direction are declared above. Any help in narrowing down what the problem might be would be a huge help!
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s/$pattern/$newpattern/" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
You need to use something other than slashes in the s/// command because there are slashes galore in the replacement text:
newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s%$pattern%$newpattern%" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
I used % symbols instead; you can use any other character that appears neither in $pattern nor $newpattern. If need so be, you can use a control character such as Control-A; that works fine too.