How to pass string variable in cut shell command [duplicate] - bash

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
Assume that I have :
.sh file having these commands :
#!/bin/bash
GIT_BRANCH="origin/release/2.4.0"
echo $GIT_BRANCH
then, I want to compute new varible from the GIT_BRANCH (operation of substring) :
So,
RELEASE_VERSION=$( $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION
But this does return message error : bad substitution
I tried many possibilities in the RELEASE_VERSION, but no result.
like
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
RELEASE_VERSION=$("$GIT_BRANCH" | cut -d "/" -f3) and this return empty results

You are definitelly missing an echo statement. Following code works for me just fine.
GIT_BRANCH="origin/release/2.4.0"
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION

Related

working with "unclear" declared variables [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I am trying to save the specific output from a piped command to a variable.
value= ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"
This saves the average ping to the variable "value".
However when I try to further process the variable e.g. in an echo line like:
echo "The Average ping is: $variable"
The output is
The Average ping is: $variable
Even when i try to pass the value to another Variable like:
value2= $value
the result is the same.
I read that variables in bash need to be declared in a certain way, may this be the problem in this specific case?
sh or bash:
value="`ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"`"
bash:
value="$(ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r")"

Bad Substitution when I try to print a specific position of array [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 10 months ago.
I'm getting started with bash programming, and I want to print a specific position of array, but when I try I get this error: Bad substitution
#!/bin/sh
user=`cut -d ";" -f1 $ultimocsv | sort -d | uniq -c`
arr=$(echo $user | tr " " "\n")
a=5
echo "${arr[$a]}" #Error:bad substitution
why?
You are using "sh" which does not support arrays. Even if you would use the "bash" you get the same error, because the "arr" will not be an array. I am not sure, if the "-c" at "uniq" is what you wanted.
I assume, this is what you are looking for:
#!/bin/bash
mapfile -t arr < <( cut -d ";" -f1 $ultimocsv | sort -d | uniq )
a=5
echo "${arr[$a]}"
This will not give the error, even if your file has less than 5 unique lines, because bash will return an empty string for a defined but empty array.
It works even with "uniq -c", because it puts complete lines in the array.

Assigning a command output to a shell script variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`

How to capture the output of curl to variable in bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
So, lets say I have the following command:
curl -I http://google.com | head -n 1| cut -d $' ' -f2
This will capture the http status code??
Now I want to assign this to variable.. in bash script
like output = "curl -I http://localhost:8088/tracks?key=9 | head -n 1| cut -d $' ' -f2"
or something like that..
How do I assign the response of above command to a variable called output in bash?
Thanks
You have two options (see this StackOverflow answer here):
Preferred: Surround the invocation in $()
Surround the invocation in back ticks
NOTE: back ticks are legacy, the former method is preferred.
output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";
output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";

Shell script cut command [duplicate]

This question already has an answer here:
Shell script, saving the command value to a variable
(1 answer)
Closed 9 years ago.
I am trying to cut ABCservice and DEFService dfrom the array and print them.What am I missing here?
urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
for url in "${urlArray[#]}"
do
service=echo $url|cut -f4 -d/
echo $service
done
Expected Output:
ABCService
DEFService
Current Output:
./test1.sh: line 6: http://server:port/ABCservice/services/ABCservice?wsdl: No such file or directory
./test1.sh: line 6: http://server:port/DEFservice/services/DEFservice?wsdl: No such file or directory
What abut this?
service=$(echo $url | cut -d"/" -f4)
echo $service
or directly
echo $(echo $url | cut -d"/" -f4)
The problems in your code:
service=echo $url|cut -f4 -d\
to save a command output in a variable, we do it like this: service=$(command).
your cut had \ as delimiter instead of /. Also it is good to wrap it with brackets: -d "/"
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Using bash string function:
for i in "${!urlArray[#]}"; do
urlArray[i]="${urlArray[i]%\?*}"
urlArray[i]="${urlArray[i]##*/}"
echo ${urlArray[i]}
done
$ urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
$ for i in "${!urlArray[#]}"; do urlArray[i]="${urlArray[i]%\?*}"; urlArray[i]="${urlArray[i]##*/}"; echo ${urlArray[i]} ; done
ABCservice
DEFservice
Your delimiter should be a forward slash
echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
The \ is an escape character. Try \\ instead.

Resources