redirect output of command to a variable in bash script - bash

I am trying to redirect the output of the following command to a variable $bei
awk '/Total number of/ && /multidriven/' ../reports/synthesis /hdl.check_design.rpt | grep -o '[0-9]' | awk '{s+=$1} END {print s}'
this command is working correctly if execute directly, and I am trying to redirect it into a variable to be used later.
I've tried 2 ways to do it:
bei= `awk '/Total number of/ && /multidriven/' ../reports/synthesis/hdl.check_design.rpt | grep -o '[0-9]' | awk '{s+=$1} END {print s}'`
bei= $(awk '/Total number of/ && /multidriven/' ../reports/synthesis/hdl.check_design.rpt | grep -o '[0-9]' | awk '{s+=$1} END {print s}')
But they both don't work and I receive "command not found" error. Does anyone have any idea about this and could you please help me? Thanks a lot

Reason for failure seem to be extra space between bei and $(awk.....) .
Example :
sh-4.1$ x =$(date) #With space
sh: x: command not found
sh-4.1$ x=$(date) #Without space
sh-4.1$ echo $x
Thu Sep 8 06:41:07 EDT 2016
sh-4.1$

Related

awk command variable NF not working on NULL input

I run my safe shell script to make sure a binary is running
to check a binary is running I do following command
pidof prog.bin | awk '{print NF}'
is some system it gives me 0 when binary not running
and
in some systems it gives me NULL(nothing)
I can check the NULL using -z option but why awk command acting this way ??
Instead of pidof you can use:
pgrep -qf prog.bin
And check its exit status.
As per man pgrep:
-f Match against full argument lists. The default is to match against process names.
-q Do not write anything to standard output.
You can use this,
if [ `pidof 'NetworkManager'` ]; then
echo "Running"
else
echo "Not Running"
fi
One way to handle this sort of thing (undefined variables) in awk is like this:
echo hi | awk '{print a}'
compared with:
echo hi | awk '{print a || 0}'
0
One Liner for If else
[[ $(pidof 'NetworkManager') ]] && echo "Running" || echo "Not Running"
Try this:
pidof prog.bin | awk '{ if (NF!=0) print NF }'
Here's some tests with awk and NF:
$ # regular line of input
$ echo foo | awk '{print NF}'
1
$ # empty line
$ echo | awk '{print NF}'
0
$ # a word on input with no newline
$ printf "%s" nonewline | awk '{print NF}'
1
$ # no input, not even a newline
$ printf %s | awk '{print NF}'
# no output from awk
I suspect the pidof case is the last: not even a newline. To force a newline:
echo $(pidof prog) | ...
printf "%s\n" "$(pidof prog)" | ...

shell commands won't run in bash script

I'm trying to pass a parameter to a bash script:
"words.sh surf"
#!/bin/bash
#words.sh
#purpose:
#
# to return a list of words
# sorted by the length of each word
#
i=$1
cat ~/wordlist | grep $i | awk '{ print length(),$0 | "sort -n" }'
and I'm getting this message:
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]].
awk: syntax error at source line 1
context is.
{ print length(), >>> -/bin/bash <<< | "sort -n" }
awk: illegal statement at source line 1
if I run the statement as a command :
cat ~/wordlist | grep surf | awk '{ print length(),$0 | "sort -n" }'
I get the correct output:
...
...
12 hypersurface
12 surfboarding
12 undersurface
12 unsurfeiting
13 subtersurface
What am I doing wrong here?
You said it's working for you, but trust me it's got issues. Re-write it as:
awk -v i="$1" '$0 ~ i{print length(), $0 }' | sort -n

What's the Error?

I'm unable to detect the cause of error.
Please help point me out and it's corrective action. I'm a beginner and so this will be a great help to me.
mapping.txt:
test1 hello
test2 world
My Sh file:
parameter 1 = hello
a= cat mapping.txt | grep $1 | awk '{print$1}'
echo $a
## Extracting Dump name
b=$(ls -ltr /home/oracle/$a/$1*.dmp | awk '{print $9}' | tail -1)
I'm getting test1 as echo, but i'm unable to substitute in /home/oracle/$a/$1*.dmp
script
output: ls: /home/oracle//hello*.dmp: No such file or directory
What change should i do to let it substitute as: /home/oracle/test1/hello*.dmp
From the error, you can see the variable a is not set:
Change your first line from:
a= cat mapping.txt | grep $1 | awk '{print$1}'
to:
a=$(cat mapping.txt | grep $1 | awk '{print$1}')

redirection of awk print to a file

I can get the cpu Mhz of a solaris machine by following command.
% /usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print $6}'
1200
when I run the following command, awk output is not getting redirected.
% csh -cf "/usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print $6}' > myoutput"
% cat myoutput
The sparcv9 processor operates at 1200 MHz,
how to get following result
% cat myoutput
1200
Your problem is that $6 is being evaluated by your existing shell before being passed on to csh (and used in the awk command).
Escaping the $ should fix the problem:
csh -cf "/usr/sbin/psrinfo -v | grep operate |head -1 | awk '{print \$6}' > myoutput"
Or, more succinctly:
csh -cf "/usr/sbin/psrinfo -v | awk '/operate/{print \$6; exit}' > myoutput"

Why I can't split the string?

I want to read a file by shell script, and process it line by line. I would like to extract 2 fields from each line. Here is my code:
#!/bin/bsh
mlist=`ls *.log.2011-11-1* | grep -v error`
for log in $mlist
do
while read line
do
echo ${line} | awk -F"/" '{print $4}' #This produce nothing
echo ${line} #This work and print each line
done < $log | grep "java.lang.Exception"
done
This is a sample line from the input file:
<ERROR> LimitFilter.WebContainer : 4 11-14-2011 21:56:55 - java.lang.Exception: File - /AAA/BBB/CCC/DDDDDDDD.PDF does not exist
If I don't use bsh, I can use ksh, and the result is the same. We have no bash here.
It's because you are passing the output of your while loop through grep "java.lang.Exception".
The output of echo $line | awk -F"/" '{print $4}' is CCC. When this is piped through grep, nothing is printed because CCC does not match the search pattern.
Try removing | grep "java.lang.Exception" and you will see the output of your loop come out correctly.
An alternative approach to take might be to remove the while loop and instead just use:
grep "java.lang.Exception" $log | awk -F"/" '{print $4}'

Resources