grep command not reading comamnd output for this adb command - terminal

On executing the following adb command with grep I get the following response in (darwin os) terminal. i count number of error after grep finds a "Error" string in response
adb -s derefedv645xxxxxx shell "am start -n com.samsung.networkui/.usa.EnhancedLteServices" | grep Error | wc -l
Output:
Error type 3
Error: Activity class {com.samsung.networkui/com.samsung.networkui.usa.EnhancedLteServices} does not exist.
0
Meaning there is no "Error" text in the response. why grep is not able to capture the text "Error" ? let me know if more information is required.

You need to redirect stderr to stdout before using next comment. I would also suggest using awk to avoid 2 commands grep + wc -l:
adb -s derefedv645xxxxxx shell "am start -n com.samsung.networkui/.usa.EnhancedLteServices" 2>&1 |
awk '/Error/ {++n} END {print n}'

Related

Unix pipeline command for redirecting output from stdin to stderr

I have a shell script that outputs information about successes to stdout, and also does a grep looking for errors in logs
inner.sh:
# do some things
echo success
# do other things
echo success
grep 'error' logs/*
I have another shell script that calls this one, counts up the successes and compares them to an expected number of successes:
outer.sh:
bash ./inner.sh | grep success | wc -l # I compare this number to the expected number
What I can't figure out how to do is have the output of grep go to stderr, so its not counted by the wc -l in outer.sh, but rather makes it around the wc to the terminal for the operator to see.
So I want a command like stdin_to_stderr that I can pipe the grep to, that would make any results it finds leave inner.sh on its stderr.
Is there already such a thing? Or do I just need to write the tiny script that would do this? Or am I thinking about this wrong?
bash ./inner.sh >&2 output_log_file
grep -c success output_log_file -- Count of success
grep -v -c success output_log_file -- Count of not "success"
Example :
echo -e "success.\nerror.\nsuccess.\nError.\nsuccess.\nerror" | grep -c success
echo -e "success.\nerror.\nsuccess.\nError.\nsuccess." | grep -v -c success
output :
3
2

Bash- Running a command on each grep correspondence without stopping tail -n0 -f

I'm currently monitoring a log file and my ultimate goal is to write a script that uses tail -n0 -f and execute a certain command once grep finds a correspondence. My current code:
tail -n 0 -f $logfile | grep -q $pattern && echo $warning > $anotherlogfile
This works but only once, since grep -q stops when it finds a match. The script must keep searching and running the command, so I can update a status log and run another script to automatically fix the problem. Can you give me a hint?
Thanks
use a while loop
tail -n 0 -f "$logfile" | while read LINE; do
echo "$LINE" | grep -q "$pattern" && echo "$warning" > "$anotherlogfile"
done
awk will let us continue to process lines and take actions when a pattern is found. Something like:
tail -n0 -f "$logfile" | awk -v pattern="$pattern" '$0 ~ pattern {print "WARN" >> "anotherLogFile"}'
If you need to pass in the warning message and path to anotherLogFile you can use more -v flags to awk. Also, you could have awk take the action you want instead. It can run commands via the system() function where you pass the shell command to run

Cannot redirect the ouput of hwclock -r command

I am implementing a shell script and I want to analyse the output shown by hwclock -r (--show) command which displays the RTC time and date.
To do that I tried things like: hwclock -r | grep -v "grep" | grep "error" > /dev/null
to see if an error happened while reading RTC registers.
The problem is that output is only and always forwarded to console. I tried to forward output to a file then analyse its content and I also tried to use tee -a command to direct output to both console and a file, but with no success.
Is there a solution to that or an explanation to what is happening with hwclock -r command.
In advance Thank you.
I just solved it by forwarding error messages to a file then make the analysis.
hwclock -r 2> file.txt; grep -v "grep" | grep "error" > /dev/null will do the job.
You omitted file.txt in the first grep.
If you just want to check for "error", with a not too old bash this will also do, in a shorter way:
hwclock -r |& grep error >/dev/null

Grep output of command and use it in "if" statement, bash

Okay so here's another one about the StarMade server.
Previously I had this script for detecting a crash, it would simply search through the logs:
#!/bin/bash
cd "$(dirname "$0")"
if ( grep "[SERVER] SERVER SHUTDOWN" log.txt.0); then
sleep 7; kill -9 $(ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}')
fi
It would find "[SERVER] SERVER SHUTDOWN" and kill the process after that, however this is not a waterproof method, because with different errors it could be possible that the message doesn't appear, rendering this script useless.
So I have this tool that can send commands to the server, but returns an EOF exception when the server is in a crashed state. I basically want to grab the output of this command, and use it in the if-statement above, instead of the current grep command, in such a way that it would execute the commands below when the grep finds "java.io.EOFException".
I could make it write the output to a file and then grep it from there, but I wonder, isn't there a better/more efficient method to do this?
EDIT: okay, so after a bit of searching I put together the following:
if ( java -jar /home/starmade/StarMade/StarNet.jar xxxxx xxxxx /chat) 2>&1 > /dev/null |grep java.io.EOFException);
Would this be a valid if-statement? I need it to match "java.io.EOFException" in the output of the first command, and if it matches, to execute something with "then" (got that part working).
Not sure to solve your problem, but this line:
ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}'
could be change to
ps -aef | awk '/[S]tarMade.jar/ {print $2}'
The [S] prevents awk from finding itself.
Or just like this to get the pid
pidof StarMade.jar

Bash script giving undesired output

I am facing with the following bash script:
#! /bin/bash
processname=$1
x=1
while [ $x -eq 1 ] ; do
ps -el | grep $processname | awk ' {if($15!="grep") print "The memory consumed by the process " $15 " is = "$9} ' >> out.log
sleep 30
done
and I am running this with :
$ ./myscript.sh Firefox
but when i see the output in the file, apart from the firefox process i am also getting information for /bin/bash process
The memory consumed by the process /Applications/Firefox.app/Contents/MacOS/firefox is = 911328
The memory consumed by the process /bin/bash is = 768
Can some one help me with this so that I only want to get information related to Firefox process and nothing else(/bin.bash etc)
The common trick is to change grep Firefox to grep [F]irefox. In this case, you can achieve it with
ps | grep '['${processname:0:1}']'${processname:1}
This is normal and because the $processname is Firefox. Since your command also monitors it, there is a process that uses it.
For example, try ps -el | grep Firefox and you will get two process lines matching (if you have one instance of Firefox running), one is Firefox, the other is the grep command looking for Firefox.
Piping your output in grep -v /bin/bash' should solve this. Eg:
ps -el | grep $processname | awk ...
becomes:
ps -el | grep $processname | grep -v 'grep' | awk ...
You calling
ps -el | grep $processname | awk ' {if($15!="grep") print "The memory consumed by the process " $15 " is = "$9} '
This means that you run awk and connect it's input with output of grep. Then grep is started and gets output of ps -el as input.
At the moment when bash will start ps you have grep and awk running.
Solution: run ps -el and remember it's output. Then run grep and awk. It should be like this:
ps -el > ps.tmp
grep $processname ps.tmp | awk ' {if($15!="grep") print "The memory consumed by the process " $15 " is = "$9} ' >> out.log
Probably this could be done without using tmp file. Like TMP=$(ps -el). But I don't know how to run grep filter on variable

Resources