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.
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:
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:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.
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
This question already has an answer here:
Using a variable containing spaces as a single argument
(1 answer)
Closed 8 years ago.
I am writing a bash script and i have the following:
#!/bin/bash
echo Enter some text
read tweet
t update $tweet
I have sferik t installed to tweet using the t update command and i want to pass multiple words into the $tweet variable however i am reciving this message.
ERROR: "t update" was called with arguments ["sdfs", "sdfsdf"]
Usage: "t update [MESSAGE]"
You need to quote the variable so it's passed as a single argument to t:
t update "$tweet"
In general, always quote your variables unless you know exactly why you should not.