Grep command giving different result for different users for same command


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command giving different result for different users for same command
# 1  
Grep command giving different result for different users for same command

Hello,

I am running below command as root user
Code:
#nodetool cfstats tests | grep  "Memtable switch count"
Memtable switch count: 12

Where as when I try to run same command as another user it gives different result.
Code:
#su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch count""
Memtable cell count: 0
Memtable data size: 0
Memtable off heap memory used: 0
Memtable switch count: 12

I am not sure why grep giving different result here.

Please help.

Thanks,
Pushpraj

Last edited by Pushpraj; 06-18-2015 at 12:44 PM.. Reason: Typo
# 2  
Hello Pushpraj,

Using only grep will match even a single string too which you are trying to search for, could you please try following
and let us know if this helps you.
1st:
Code:
 #su -l zabbix -s /bin/bash -c "nodetool cfstats tests" | grep -i "Memtable switch count"

2nd:
Code:
 #su -l zabbix -s /bin/bash -c "nodetool cfstats tests" |awk -F":" -vvar="Memtable switch count" '{if($1 == var){print}}'

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-18-2015 at 11:57 AM..
# 3  
Escape the double quotes around the search pattern.

BTW, I'd be surprised if the result of the first command were "12" only...

Last edited by RudiC; 06-18-2015 at 12:04 PM..
# 4  
Thanks RudiC , You are correct. That was typing mistake. I have updated post.
# 5  
Soooo, problem solved?
# 6  
Hi RudiC , I just tried it now. If I run the command as zabbix user by escaping double quotes it ran correctly.
Code:
su -l zabbix -s /bin/bash -c "nodetool cfstats cassdb.tests | grep \"Memtable switch count\""
Memtable data size: 12

But problem is this is not working from bash script. Below is the script I am trying.
Code:
#!/bin/bash
KEYSPACE="$1"
ATTRIBUTE="$2"
nodetool cfstats cassdb.${KEYSPACE} | /bin/grep "$ATTRIBUTE" | awk -F: '{print $2}' | sed -e 's/^[ \t]*//'

When I run above script as root user it works perfectly.
Code:
#/opt/zabbix/cassandra.sh tests "Memtable switch count"
12

Where as when executed as zabbix user it dosent works.
Code:
su -l zabbix -s /bin/bash -c "/opt/zabbix/cassandra.sh tests "Memtable switch count""
109
0
105
12

Here 12 is printed as bottom along-with other attributes values which has "Memtable" string in it.

I tried escaping double quotes from bash script but somehow not able to figure out exact syntax.

Thanks RavinderSingh, I tried both the options which you provided but they are not working. No output for the command.

Thanks,
Pushpraj

Last edited by Pushpraj; 06-18-2015 at 03:39 PM..
# 7  
Why don't you do it exactly as above?
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #156
Difficulty: Easy
The first two-network TCP/IP communications test was performed between Stanford and University College London, in 1975.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

3. Shell Programming and Scripting

Grep result from dd command

Hi, I am running following command in a bash script for testing IO and use grep to get throughput number, but it did not work, it displayed everything: dd if=/dev/zero of=/dev/null bs=1G count=1 oflag=dsync | grep bytes | awk '{print $7}' 1+0 records in 1+0 records out 536870912 bytes... (2 Replies)
Discussion started by: hce
2 Replies

4. Shell Programming and Scripting

If the grep command returns any result set

my intension is to use a grep command inside the shell script and if any row is returned or not.. depending on the resultset i have to code on further. how to check this i mean.. could anyone help me out with the if condition how to use it here !! (4 Replies)
Discussion started by: gotam
4 Replies

5. Shell Programming and Scripting

Use grep result to execute next command

Hi I am trying to run 2 servers using a script one after the other. I start the first one: run.sh -c servername >> jboss_log.txt & Then I have to wait until I see Started message in the log file before I launch the other server. I can't use sleep because I am not sure how long it'll... (5 Replies)
Discussion started by: iririr
5 Replies

6. UNIX for Dummies Questions & Answers

Last Command not giving year

Hi All, i want the last login details along with year. i tried below command but not giving me the year of last login. last <$Userid> | head -1 i heard that if it is current year then it wont display the year else it will display the year. is it so? if yes then , is there any way of... (1 Reply)
Discussion started by: ani_rvce
1 Replies

7. Shell Programming and Scripting

Last Command not giving year

Hi All, i want the last login details along with year. i tried below command but not giving me the year of last login. last <$Userid> | head -1 i heard that if it is current year then it wont display the year else it will display the year. is it so? if yes then , is there any way of... (1 Reply)
Discussion started by: ani_rvce
1 Replies

8. Linux

Terminal Execution By Giving a Command

Hi Guys, I am using Red Hat Linux 5 and GNOME Terminal is available there in the Accessories menu of Applications. But I don't see any run command option which can be used to type the name of the terminal and execute it directly as I used to do it under Mandrake Linux wherein I would type... (2 Replies)
Discussion started by: indiansoil
2 Replies

9. Solaris

^p not giving command line history.

I am used to using "set -o emacs" and then using "CNTL-P" for getting previous commands in solaris but on one host it does not work and instead just makes a DONG!! # ksh # set -o emacs # ls # ^p Can anyone offer guidance as to why this is? Thank you my friends. akbar (0 Replies)
Discussion started by: akbar
0 Replies

10. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

Featured Tech Videos