Solved

busybox bash/ash - bash -x | grep

Posted on 2016-08-29
19
88 Views
Last Modified: 2016-08-29
On busybox, I often use bash -x to debug scripts, watching them as they run but a problem is the information flies by very quickly, easily missed.

I need to run bash -x while watching for certain conditions, kind of like if you'd use grep to see some specific output.
Wondering if anyone might know of a trick, a tip, a way of running bash -x while being able to grep certain lines based on a key word, and not have the rest of the output being displayed.

Probably a long shot uh?
0
Comment
Question by:Mark Lewis
  • 10
  • 6
19 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You mentioned "grep" - did you try it?

 bash -x /path/to/script param1 param2 2>&1 | grep "my desired messages"

If this doesn't work (it should!) you could redirect all output to a file to then scan this file, also using "grep":

bash -x /path/to/script param1 param2 2>&1  > script.log
grep "my desired messages" script.log

"2>&1" is needed to feed stderr (the debug messages) also to grep instead of having it displayed directly on your terminal.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
If it's just for viewing all messages keeping them from scrolling away you could use "more" or "less":

 bash -x /path/to/script param1 param2 2>&1 | more
 bash -x /path/to/script param1 param2 2>&1 | less

If "grep" in my first suggestion above would give too much output you can of course combine it with, let's say, "more":

bash -x /path/to/script param1 param2 2>&1 | grep "my desired messages" | more
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
Well, you wrote "certain conditions" which is plural. "grep" can handle such OR conditions:

bash -x /path/to/script param1 param2 2>&1 | grep -E "condition string1|condition string2|condition string3"
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
PuTTY with with 20000 lines of history would maybe do the trick as well ;)
0
 

Author Comment

by:Mark Lewis
Comment Utility
@Gerwin Jansen

I do that too but better to look for specific entries when there are thousands of lines.
0
 

Author Comment

by:Mark Lewis
Comment Utility
I accepted too quickly. Your example is using bash but my problem is when using ash, which is bash for busybox.
Your examples do not work with ash.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
What exactly does not work?
Error messages?
0
 

Author Comment

by:Mark Lewis
Comment Utility
There are no error messages, the script simply runs but I see it's full output rather than only the keywords I am looking for.

@Gerwin Jansen
If that is possible, it might be best.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
I'm really unsure what you mean with " ash, which is bash for busybox."

As far as I know (and I regularly use busybox under Cygwin) it's just a collection of scaled-down Unix tools (applets), compiled/linked into one single executable named "busybox".
busybox itself runs within a shell (better: is called by a shell) , which is mostly bash.
Applets are started over aliases set inside the calling shell.
Some busybox implementations come with "ash" as an applet, but that's just for making the footprint of some scripts a bit smaller. Such scripts normally use the full versions (if installed) of tools like "more" instead of the applets, or they must use "/bin/busybox more" instead.

So what exactly is your environment? Under which OS do you use busybox? Does this OS provide bash? What do you see with

alias

and with

uname -a
echo $0

?
0
 

Author Comment

by:Mark Lewis
Comment Utility
In this case, I am testing something on openwrt which uses busybox ans ash.
Typically, because busybox uses scaled down commands, they do not have all of the functions that full Linux environments do.
Bash is an alias to ash on openwrt.

"OpenWrt uses busybox's ash shell by default, which is in most parts POSIX conform. Visit shell script for general Information about shell scripts"

https://wiki.openwrt.org/doc/howto/user.beginner.cli
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
I just read a bit about Ash, because it must have been a long time since I last used it (if at all).

Ash (as claimed by POSIX, by the way) supports the "-x" flag, redirection of stdout/stderr (2>&1) and pipes (|)..

So even if you're constrained to exclusive use of Ash the solutions I gave you should work under that shell.

Important: Did you specify "2>&1" as I suggested? Failure doing so will show all debugging info on the screen, regardless of grep - which is exactly the scenario you described!

P.S. The documents  you pointed me to (including this one: http://www.in-ulm.de/~mascheck/various/ash/#dash) do not help in that aspect.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
>> scaled down commands << Yes, but a "grep" which doesn't grep is a bit useless, so I don't think they "scaled it down" that much.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You quoted a document saying that ash is in most parts POSIX conform.

Here is the POSIX specification, which confirms that POSIX requires proper recognition of "-x", "2>&1" and "|"

http://pubs.opengroup.org/onlinepubs/009696699/utilities/xcu_chap02.html#tag_02
0
 

Author Comment

by:Mark Lewis
Comment Utility
This time, I used it exactly like this.

bash -x script.sh param1 param2 2>&1 | grep -E "datetimer"

That worked. I am not sure what param1 and param2 mean.
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
param1/2 (3/4/5...) are just placeholders for command line parameters your script might require. Replace with your own values or omit them if there are none.
0
 

Author Comment

by:Mark Lewis
Comment Utility
ok, that's what I thought but didn't try it.

bash -x script.sh 2>&1 | grep -E "datetimer" works.

Thanks very much, now I can finally get some work done :)
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You're always welcome, glad I could help!

Thx for the points!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now