?
Solved

busybox bash/ash - bash -x | grep

Posted on 2016-08-29
19
Medium Priority
?
355 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
17 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41774977
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
ID: 41775007
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 2000 total points
ID: 41775117
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41775183
PuTTY with with 20000 lines of history would maybe do the trick as well ;)
0
 

Author Comment

by:Mark Lewis
ID: 41775188
@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
ID: 41775250
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
ID: 41775295
What exactly does not work?
Error messages?
0
 

Author Comment

by:Mark Lewis
ID: 41775330
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41775351
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
ID: 41775562
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
ID: 41775567
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
ID: 41775577
>> 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
ID: 41775583
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
ID: 41775608
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
ID: 41775611
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
ID: 41775620
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
ID: 41775624
You're always welcome, glad I could help!

Thx for the points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

831 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