Finding text from file and assigning to a variable - bash

I am trying to find a text from file and assign it to a variable. I used grep with regex option to find a text. I need the output to get assigned to a variable so that I can use the output wherever I need. Filename: wgettesting.html
cat wgettesting.html
productvale:productvalues,productinfo:productinformation,product_group_type,value:product_genderledger,productcategories:smallcategories
Command I used in a script:
gl=grep -o --perl-regexp "(?<=product_group_type,value:product_)[a-zA-Z0-9_]+" wgettesting.html
echo $gl
When I run the script, I got: -o: command not found for the above command.
I also tried like:
gl=cat wgettesting.html|grep -o --perl-regexp ""(?<=product_group_type,value:product_)[a-zA-Z0-9_]+"
echo $gl
For the above, I received: wgettesting.html: command not found
My intended output has to be:
genderledger
Someone guide me on the issue.

You failed to put the grep command within $(),
$ gl=$(grep -o --perl-regexp "(?<=product_group_type,value:product_)[a-zA-Z0-9_]+" wgettesting.html)
$ echo $gl
genderledger
Commands should be enclosed within $() while assigning it's output to a variable.

Related

echo does not print command output as expected

This following command prints nothing on my machine
echo `python3.8 -c 'print ("*"*10)'`
whereas
python3.8 -c 'print ("*"*10)'
does. Why?
The first example does command substitution for the argument to echo and is equivalent to the command echo **********. This happens to output a list of directory contents, since ********** seems to be equivalent to *1 and is expanded by the shell.
If you want to prevent the shell to expand **********, you need to quote it:
echo "`python3.8 -c 'print ("*"*10)'`"
which is equivalent to echo "**********".
1don't quote me on that (pun intended)
`expr` means to evaluate the contents between ' as a command and replace it with the result.
`print(*)` is equal to ` * ` which is evaluated as all the files in the current directory
The output really depends on the machine used

Bash: Assign command to a variable

Below I have an example which confuses me a bit, any help would be appreciated.
I bind a normal command line command (ls) to a new variable. If I echo it it the output is just the command (ls) but if I just use the variable without echo i get the result of the command but why?? Is it because $listdir gets translated to ls so I just get the output? And if I use the echo command it will be interpreted as a string?
router#test:~/scripting$ listdir=ls
router#test:~/scripting$ echo "$listdir"
ls
----- VS ----
router#test:~/scripting$ $listdir
basicLoop.sh fileflood.sh .......
Thank you for any help!
By doing listdir=ls you literally assign a string "ls" to the $listdir variable. So if you run echo $listdir now it will just expand into echo ls, which (as you may have guessed) will just print "ls" onto a screen. If you want to store a result of a command into a variable you can wrap the command in `` or $() (eg. listdir=$(ls) or listdir=`ls`).
jarmusz#emacs~$ listdir=`ls`
jarmusz#emacs~$ echo "$listdir"
dls
docs
music
...
If you want to store just a name of the command and run it later you can do it like this:
jarmusz#emacs~$ listdir=ls
<some other commands...>
jarmusz#emacs~$ echo `$listdir`
dls docs music ...
In this example, echo `$listdir` will expand into echo `ls` and then into echo dls docs music...
When bash is interpreting the commands you feed to it, the first thing it will do is expand any expansions it is given. So when you give it $listdir by the time bash starts to execute the value it is given, all it knows is that it was given the value ls. It does not care where the value came from, only what the value is.
Lets look at the trace given after running set -x, which instructs bash to prints to stderr after expansion and before execution:
$> echo $listdir
+ echo ls
ls
$> $listdir
+ ls
file_0 file_1
As you can see, in the second line, bash will attempt to run the command ls just as if you have explicity called ls or even /usr/bin/ls
Edit
Expansion isn't the first step in in shell evaluation, see #Gordon Davisson's comment for details

Bash scripting - escape characters in unix

I am trying to execute a word search in my directory where I look into all text files, and try to find words that have a length of 14.
$ ls *.txt | & grep -o -w '\w\{14,14\}'
This works as intended when I run it in a command line.
Now I want to run this same exact command but in a .sh file
In my file, I have this:
eval $("ls *.txt | & grep -o -w '\w\{14,14\}'")
I then get this error:
test.sh: line 1: ls *.txt | & grep -o -w '\w{14,14}': command not found
Is the problem that I have escape characters in my text? How can I get it so that when I run my .sh file, I get the same output as if I ran it on the command line?
You don't need to eval something to put it in a shell script. eval is evil, and should be avoided like the plague (as in, only use if you have invented the antidote yourself). Your statement could be changed to simply grep -o -w '\w\{14,14\}' *.txt and chucked verbatim into a script file.
An excellent web site to learn both the fundamentals and the subtleties of Bash scripting is Greg's Wiki.

How to pass a shell script argument as a variable to be used when executing grep command

I have a file called fruit.txt which contains a list of fruit names (apple, banana.orange,kiwi etc). I want to create a script that allows me to pass an argument when calling the script i.e. script.sh orange which will then search the file fruit.txt for the variable (orange) using grep. I have the following script...
script name and argument as follows:
script.sh orange
script snippet as follows:
#!/bin/bash
nameFind=$1
echo `cat` "fruit.txt"|`grep` | $nameFind
But I get the grep info usage command and it seems that the script is awaiting some additional command etc. Advice greatly appreciated.
The piping syntax is incorrect there. You are piping the output of grep as input to the variable named nameFind. So when the grep command tries to execute it is only getting the contents of fruit.txt. Do this instead:
#!/bin/bash
nameFind=$1
grep "$nameFind" fruit.txt
Something like this should work:
#!/bin/bash
name="$1"
grep "$name" fruit.txt
There's no need to use cat and grep together; you can simply pass the name of the file as the third argument, after the pattern to be matched. If you want to match fixed strings (i.e. no regular expressions), you can also use the -F modifier:
grep -F "$name" fruit.txt

Passing a variable into awk within a shell script

I have a shell script that I'm writing to search for a process by name and return output if that process is over a given value.
I'm working on finding the named process first. The script currently looks like this:
#!/bin/bash
findProcessName=$1
findCpuMax=$2
#echo "parameter 1: $findProcessName, parameter2: $findCpuMax"
tempFile=`mktemp /tmp/processsearch.XXXXXX`
#echo "tempDir: $tempFile"
processSnapshot=`ps aux > $tempFile`
findProcess=`awk -v pname="$findProcessName" '/pname/' $tempFile`
echo "process line: "$findProcess
`rm $tempFile`
The error is occuring when I try to pass the variable into the awk command. I checked my version of awk and it definitely does support the -v flag.
If I replace the '/pname/' portion of the findProcess variable assignment the script works.
I checked my syntax and it looks right. Could anyone point out where I'm going wrong?
The processSnapshot will always be empty: the ps output is going to the file
when you pass the pattern as a variable, use the pattern match operator:
findProcess=$( awk -v pname="$findProcessName" '$0 ~ pname' $tempFile )
only use backticks when you need the output of a command. This
`rm $tempFile`
executes the rm command, returns the output back to the shell and, it the output is non-empty, the shell attempts to execute that output as a command.
$ `echo foo`
bash: foo: command not found
$ `echo whoami`
jackman
Remove the backticks.
Of course, you don't need the temp file at all:
pgrep -fl $findProcessName

Resources