This question already has answers here:
bash command not found when setting a variable
(2 answers)
Closed 6 months ago.
i have a script that compare heapmemory
#!/bin/bash
$a=$(jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $5}')
$b=1000000K
if [[ $a -ge $b ]]
then
echo "The heapmemory used is greater."
else
echo "The heapmemory used is small."
fi
my question is when I am executing this script using ./testscriptforheap.sh, although the output is correct but why I am getting this command not found in line number 2 and 3 I am not able to figure it out.
> ./testscriptforheap.sh: line 2: =1603644K: command not found
> ./testscriptforheap.sh: line 3: =1000000K: command not found
The heapmemory used is greater.
Assignments in bash (and also in sh) don't use the $ in front of them:
a=$(jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $5}')
b=1000000K
Your variable assignment is incorrect, try this:
#!/bin/bash
a=$(jcmd "$(jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap | cut -f1 -d ' ')" GC.heap_info | awk 'NR==2 {print $5}')
b=1000000K
if [[ $a -ge $b ]]
then
echo "The heapmemory used is greater."
else
echo "The heapmemory used is small."
fi
...also, the bash -gt operator is trying to compare two integers.
From what I can gather:
When you issue the jcmd nnnn GC.heap_info command, it's the sixth field of the second record in the output holds the used heap memory.
It will be of this form nnnnnK, so to be able to compare just the numbers, you could do with piping that through a sed:
Then you could remove K from $b=1000000K.
So, no harm in trying...
#!/bin/bash
a=$( \
jcmd `jps -l | grep com.adobe.coldfusion.bootstrap.Bootstrap | cut -f1 -d ' '` GC.heap_info | awk 'NR==2 {print $6}' | sed -r 's/[^0-9]//g' \
)
b=1000000
if [[ $a -ge $b ]]
then
echo "The heapmemory used is greater."
else
echo "The heapmemory used is small."
fi
Related
I'm extracting two md5sums by using this code:
md5sum test{1,2} | cut -d' ' -f1-2
I'm receiving two md5sums as in example below:
02eace9cb4b99519b49d50b3e44ecebc
d8e8fca2dc0f896fd7cb4cb0031ba249
Afterwards I'm not sure how to compare them. I have tried using the xargs:
md5sum test{1,2} | cut -d' ' -f1-2 | xargs bash -c '$0 == $1'
However, it tries to execute md5sum as a command
Any advice?
Try using a command subsitution instead
#!/bin/bash
echo 1 > file_a
echo 2 > file_b
echo 1 > file_c
file1=file_a
# try doing "file2=file_b" as well
file2=file_c
if [[ $(sha1sum $file1 | cut -d ' ' -f1-2) = $(sha1sum $file2 | cut -d ' ' -f1-2) ]]; then
echo same
else
echo different
fi
I assign a keyword as variable, and need to awk from a file using this variable and loop. The file has millions of lines.
i have tried the code below.
DEVICE="DEV2"
while read -r line
do
echo $line
X_keyword=`echo $line | cut -d ',' -f 2 | grep -w "X" | cut -d '=' -f2`
echo $X_keyword
done <<< "$(grep -w $DEVICE $config)"
log="Dev2_PRT.log"
while read -r file
do
VALUE=`echo $file | cut -d '|' -f 1`
HEADER=`echo $VALUE | cut -c 1-4`
echo $file
if [[ $HEADER = 'PTR:' ]]; then
VALUE=`echo $file | cut -d '|' -f 4`
echo $VALUE
XCOORD+=($VALUE)
((X++))
fi
done <<< "awk /$X_keyword/ $log"
expected result:
the log files content lots of below:
PTR:1|2|3|4|X_keyword
PTR:1|2|3|4|Y_rest .....
Filter the X_keyword and get the field no 4.
Unfortunately your shell script is simply the wrong approach to this problem (see https://unix.stackexchange.com/q/169716/133219 for some of the reasons why) so you should set it aside and start over.
To demonstrate the solution, lets create a sample input file:
$ seq 10 | tee file
1
2
3
4
5
6
7
8
9
10
and a shell variable to hold a regexp that's a character list of the chars 5, 6, or 7:
$ var='[567]'
Now, given the above input, here is the solution for how to g/re/p pattern as variable and count how many results:
$ awk -v re="$var" '$0~re{print; c++} END{print "---" ORS c+0}' file
5
6
7
---
3
If that's not all you need then please edit your question to clarify your requirements and provide concise, testable sample input and expected output.
I'm trying to use this command to do different things based on the version of tmux installed.
But since the version is a float, I can't use normal bash checking, so I'm trying to use bc which takes in arguments in the form of "a
[[ echo `tmux -V | cut -d ' ' -f2` "> 1.6" | bc ]]
But if I do that, I get
-bash: conditional binary operator expected
-bash: syntax error near `-f2`>'
The first part tmux -V | cut -d ' ' -f2\ returns something like 1.6 or 1.8, so I'm trying to concatenate that with "> 1.6" to get an expression like "1.8> 1.6".
So I'm not really sure how to do this.
This is also going in a .tmux.conf file, and so I don't think I'd be able to store the result of the first part in another variable first.
You should not have an \ in the cut command. This works
echo "$(tmux -V | cut -d ' ' -f2) >1.6" | bc
Also, you may try this:
echo "$(tmux -V | awk '{print $2}') >1.6" | bc
And the test should be comparing to 1:
if [[ "$(echo "$(tmux -V | awk '{print $2}') >1.6" | bc)" -eq 1 ]]; then
echo "version above 1.6"
else
echo "please update your version"
fi
Please understand that doing a "floating point" test means that 1.11 is smaller than 1.6.
Not what is intended, I believe.
The correct test will need to split the version string on the dot and comparing both numeric (integer) values:
#!/bin/bash
IFS=' .' read _ one two <<< "$( tmux -V )"
if (( ( one == 1 && two > 6 ) || one > 1 )); then
echo "version above 1.6 present"
else
echo "please update tmux to a version higher than 1.6"
fi
I need some help . I want the result will be
UP:N%:N%
but the current result is
UP:N%
:N%
this is the code.
#!/bin/bash
UP=$(pgrep mysql | wc -l);
if [ "$UP" -ne 1 ];
then
echo -n "DOWN"
else
echo -n "UP:"
fi
df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t && echo -n ":"
top -bn2 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \awk 'END{print 100 - $1"%"}'
You can use command substitution in your first sentence (notice you're creating a subshell in this way):
echo -n $(df -hl | grep 'sda1' | awk ' {percent+=$5;} END{print percent"%"}'| column -t ):
I have the following code
for ip in $(ifconfig | awk -F ":" '/inet addr/{split($2,a," ");print a[1]}')
do
bytesin=0; bytesout=0;
while read line
do
if [[ $(echo ${line} | awk '{print $1}') == ${ip} ]]
then
increment=$(echo ${line} | awk '{print $4}')
bytesout=$((${bytesout} + ${increment}))
else
increment=$(echo ${line} | awk '{print $4}')
bytesin=$((${bytesin} + ${increment}))
fi
done < <(pmacct -s | grep ${ip})
echo "${ip} ${bytesin} ${bytesout}" >> /tmp/bwacct.txt
done
Which I would like to print the incremented values to bwacct.txt, but instead the file is full of zeroes:
91.227.223.66 0 0
91.227.221.126 0 0
127.0.0.1 0 0
My understanding of Bash is that a redirected for loop should preserve variables. What am I doing wrong?
First of all, simplify your script! Usually there are many better ways in bash. Also most of the time you can rely on pure bash solutions instead of running awk or other tools.
Then add some debbuging!
Here is a bit refactored script with debugging
#!/bin/bash
for ip in "$(ifconfig | grep -oP 'inet addr:\K[0-9.]+')"
do
bytesin=0
bytesout=0
while read -r line
do
read -r subIp _ _ increment _ <<< "$line"
if [[ $subIp == "$ip" ]]
then
((bytesout+=increment))
else
((bytesin+=increment))
fi
# some debugging
echo "line: $line"
echo "subIp: $subIp"
echo "bytesin: $bytesin"
echo "bytesout: $bytesout"
done <<< "$(pmacct -s | grep "$ip")"
echo "$ip $bytesin $bytesout" >> /tmp/bwacct.txt
done
Much clearer now, huh? :)