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.
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?
This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Compare output rather than command
(3 answers)
Closed 3 years ago.
piped eval in bash for string length comparison
i am trying to check if a certain device with a given id is plugged in and trigger a action based on that
i tried eval / exec
here is what i have so far
#!/bin/bash
KBP='[["lsusb -d 1c11:b04d | wc -c" == "0"]]'
if eval $KBP; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
expected result:
if device is plugged in and string is not 0 it would hop in the false condition
actual result - cant evaluate the piped condition
Guessing fixed expression would look like this:
if [ "$(lsusb -d 1c11:b04d | wc -c)" -eq 0 ]; then
To remember:
Bash is spaces aware. [[ and ]] needs after and behind (well, ; is special here, it separates commands).
To get output of a command use command substitution $( ... )
There is no need for eval here.
This question already has answers here:
How to run script commands from variables?
(3 answers)
Execute command in a variable don't execute the latter part of a pipe
(1 answer)
Running a command that is stored in a variable (including pipes and redirects)
(3 answers)
Closed 5 years ago.
#!/bin/bash
# 1st part
ret=$(ps aux | grep -v grep) # thats OK
echo $ret
# 2nd part
cmd="ps aux | grep -v grep" # a problem with the pipe |
ret=$($cmd)
echo $ret
How can I use a command-string as I have in the 2nd part? Think the pipe is the problem. Tried to escape it but it did not help. Get some snytax error of ps.
Thanks!
You need eval:
ret=$(eval "$cmd")
Using eval is not recommended here. It can lead to unexpected results, especially when variables can be read from untrusted sources (See BashFAQ/048 - Eval command and security issues.
You can solve this in a simple way by defining and calling a function as below
ps_cmd() {
ps aux | grep -v grep
}
and use it in the script as
output="$(ps_cmd)"
echo "$output"
Also a good read would be to see why storing commands in a variable is not a good idea and has a lot of potential pitfalls - BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!
This question already has answers here:
Why should eval be avoided in Bash, and what should I use instead?
(3 answers)
Variables as commands in Bash scripts
(5 answers)
Closed 5 years ago.
Command="tr ' ' '\n' < /location/to/file.xml | sed -e '/\/Name/, $d' | grep Apple | wc -l"
New_command=`$Command`
The error I am getting:
tr: extra operand \'\\n\'' Trytr --help' for more information.
What is wrong with what I am doing? Trying to assign a string variable to 'New_Command' so I can execute it and get the output.