This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
How do i store the output of a bash command in a variable? [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to get the number of lines that was printed from the ps command, and save to a variable.
COUNT_PS= ps -C $NAME | wc -l)
the line above prints me 2, but COUNT_PS unfortunately still equals 0 (at the start of the script I've assigned COUNT_PS as 0).
It actually prints the value - but doesn't save it to COUNT_PS.
What am I doing wrong?
Related
This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Closed 5 months ago.
I have asked way too many dumb questions here, but here we go.
Code:
#!/bin/bash
for i in {0..$#}; do
ascii ${!i} | grep "\`" | tail -c 3 | head -c 1;
done
echo
(This is to translate long binary input, like from those horror games that have binary at the end)
Error message:
./asciit: line 4: {0..2}: invalid variable name
Command run:
./asciit 01101000 01101001
Why?
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:
How do I set a variable to the output of a command in Bash?
(15 answers)
Bash script store command output into variable
(2 answers)
Save output in variable? [duplicate]
(2 answers)
Store output of sed into a variable [duplicate]
(4 answers)
Assigning the output of a command to a variable [duplicate]
(3 answers)
Closed 4 years ago.
suppose in a file if I have " Route : 8888" , I want to get the string after the pattern "Route :"
I have used sed command, but it is printing 8888 but I want to store it into a variable and use for further processing
So store sed's output in a variable:
ROUTE_VALUE=$(sed ... <file.txt)
Where '...' is your sed command.
$ cat rt.sh
ROUTE=`sed -n 's/.*Route = \(.*\),.*/\1/p' data.txt`
echo $ROUTE
$ bash rt.sh
8888
$ source rt.sh
8888
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:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
I want to find out the number of directories and files in home directory and store that in a shell variable. I am using the following set of commands.
command="ls -l | grep -c \"rahul.*patle\""
eval $command
I want to store the result in a variable. How can I do this?
The syntax to store the command output into a variable is var=$(command).
So you can directly do:
result=$(ls -l | grep -c "rahul.*patle")
And the variable $result will contain the number of matches.