Week of bash scripts – rps and commentstrip

These two scripts will respectively: find if a program is running, and strip-comments from text files. The first is useful if you need to see if the program is running or if you need to kill the process with it’s id, comment strip is a good tool to use if posting configurations on forums as often developers or advanced users already know what the settings actually do.


aspire ~/.scripts:
rps geany
todd      1827  0.1  0.3 184576 28616 ?        S    May31   1:05 geany
# Running program search
ps aux | grep --color=always -i "$@" | grep -v grep | grep -v "$0"
view raw rps hosted with ❤ by GitHub


Commentstrip will display the output to the terminal. If you got xclip installed the ‘c’ option can be used to copy the output to the clipboard.

# Output file without comments or blanklines
# Display usage if no parameters given
if [ -z $1 ]; then
echo " ${0##*/} <*c> <filename> - print file w/o comments/blanklines - (c)lipboard"
exit 1
case $1 in
# Copy output to xorg server clipboard
c ) shift
# Check if selection exists
if [ ! -f "$@" ]; then
echo " Selection \""$@"\" does not exist." && exit
# Exit if root (root doesn't have access to user xorg server)
if [[ `whoami` == root ]]; then
echo " Copying to clipboard cannot be user root" && exit
grep -vh '^[[:space:]]*\(#\|$\)' "$@" | xclip -selection c
echo " Comments stripped from file and copied to xorg server clipboard"
# Print output to terminal
* ) if [ ! -f "$@" ]; then
echo " Selection \""$@"\" does not exist." && exit
grep -vh '^[[:space:]]*\(#\|$\)' "$@" ;;
view raw cmtstrip hosted with ❤ by GitHub

The final day of week of bash scripts… phew! I’d like to thank everyone that posted comments, and to those that stopped by this week.

9 thoughts on “Week of bash scripts – rps and commentstrip

  1. Gen2ly Post author

    lol, I like that too, huh, never seen that before: bracketing the first letter will get rid of the grep result. ah, I guess I’m lazy.

  2. Shae

    The command line and by extension bash is very powerful in Linux. New users generally freak out about the thought of using them, but they make some very tough tasks a breeze. Thanks for sharing this, I now use this sometimes to look at some files I have that seem to be mostly useless comments (somehow?)

  3. Gen2ly Post author

    Nice, glad you found a need for it. Yeah, I find the commentstrip pretty useful or but for the not long one I use tail.

  4. Iain


    Instead of using
    grep -v “.bin/rps”
    how about
    grep -v “$0”

    This way you can put it anywhere, and bash will remove the name for you.

    Also, you could use #!/bin/sh instead of bash since I don’t see any bash-specific commands in there, and it makes it a touch more portable…

    And finally, copy-and-paste from wordpress seems to put non-breaking spaces instead of normal spaces in the command, so I couldn’t actually run the script until I typed it out by hand!


  5. Gen2ly Post author

    Good tips Iain, thank you. Yeah, I’ve seen $0 used before but doesn’t come to me regularly yet, although now, I think that it will. As for #!/bin/sh I put them in because you know about them but I don’t know enough about bash-specific commands to be able to do so yet. As for the copy-paste thing: it turns out if you use <div style= blah blah> it uses western encoding (or whatever your browser is set to) and not UTF-8. I tested before but the script must have been too basic. Think the problem is mainly with “quotes” but probably other characters in there too. Think the web is just set up this way now mainly, probably for, microsoft systems. Now doing <pre style= blah…> should have it fixed. That’s a big one for me, thanks for the heads up.

  6. Iain

    Actually I only looked at rps, not commentstrip for sh compatibility. I just found out (man bash) that you can start bash with +B to force strict sh compatibility, which would be useful for testing the portability of your scripts.

    I should have said nice post too :) I like to do things myself too (where I can) rather than use pre-done programs/scripts – I learn better that way.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s