This question already has answers here:
extracting a string between two quotes in bash
(3 answers)
Closed 6 years ago.
I have a string that looks like:
result='SNMP OK - "-63.1" |' # output should be -63.1
result='SNMP OK - "63.1" |' # output should be 63.1
I need the to output everything between the quotes -- which should always be numeric.
var='SNMP OK - "-63.1"';
newvar=$(echo "$var" | sed -r 's/.*"(.*)".*/\1/')
echo "$newvar"
-63.1
Related
This question already has answers here:
Forcing bash to expand variables in a string loaded from a file
(13 answers)
Closed 3 years ago.
I have a string like ${REPOSITORY}/company/api:${API_VERSION}. $REPOSITORY and $API_VERSION are shell variables.
$ echo ${DATA_API_VERSION}
latest
$ echo ${REPOSITORY}
com.company.repo
I want to get the interpolated string that shows the values of these variables and assign it to another variable.
This is what I get:
$ echo "$image"
${REPOSITORY}/company/api:${API_VERSION}
I want this:
com.company.repo/company/api:latest
You could use sed to search and replace the two variables.
#!/bin/bash
DATA_API_VERSION="latest"
REPOSITORY="com.company.repo"
image='${REPOSITORY}/company/api:${DATA_API_VERSION}'
sed -e "
s/\${REPOSITORY}/$REPOSITORY/g
s/\${DATA_API_VERSION}/$DATA_API_VERSION/g
" <<< "$image"
This question already has answers here:
How do I use variables in single quoted strings?
(8 answers)
Closed 4 years ago.
The below expression
var=$(git log --since='$start_date' --until='$end_date' --author='$commit_author' | grep -i 'merge request\|pull request' | wc -l)
echo $var
prints var as zero
but if I echo the above expression
git log --since='2018-04-1' --until='2018-06-30' --author='so.sila#xes.com' | grep -i 'merge request\|pull request' | wc -l
and copy it and execute via terminal works
why is that var not storing the value and returns zero
The reason is that '$start_date', '$end_date' etc do not expand to their values as they are in single quotes. Try changing it to double quotes and you may have some luck.
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I am trying to write a shell script that uses wget to download files in bulk from urls that follow a certain numeric pattern.
Understandably, the url from the user input must contain the variable $i.
dl.sh http://some/url/$i/some/url 1 9
This yields repeated result from the final loop because $i will be expanded before passing down into the loop.
http://some/url/9/some/url
http://some/url/9/some/url
...
http://some/url/9/some/url
Is there a workaround to get this shell script working?
Source Code:
#!/bin/bash
# dl.sh url | index_from | index_to
for i in $(seq $2 $3)
do
echo ${1} # replace with wget for actual download.
done
Expected Result:
http://some/url/1/some/url
http://some/url/2/some/url
http://some/url/3/some/url
...
http://some/url/9/some/url
This question already has answers here:
Get string after character [duplicate]
(5 answers)
Closed 6 years ago.
I have variable $OUTPUT=abc PHYSIN=lalala ghi
How can I extract the value of PHYSIN into another new variable called VETH_NAME,
in other words, I'd like VETH_NAME to be lalala
How can I do so using bash commands?
Thanks
Assuming this is what you are saying :
OUTPUT="abc"
PHYSIN="lalala ghi"
VETH_NAME=$(echo "$PHYSIN" | cut -d" " -f1)
finally :
echo $VETH_NAME
lalala
Use parameter expansion with the %% operator, which drops the longest matching suffix from the expansion.
OUTPUT=abc
PHYSIN="lalala ghi"
VETH_NAME=${PHYSIN%% *}
This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop