Solved

busybox bash/ash - bash -x | grep

Posted on 2016-08-29
19
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
19 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 500 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

726 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