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.

rps


#!/bin/sh
# rps - running program search

ps aux | grep --color=always $@ | grep -v grep | grep -v "$0"

commentstrip

Commentstrip will display the output to the terminal but if you got xclip installed, the ‘c’ flag can be used to copy the output to the clipboard (i.e. ‘commentstrip c file’).


#!/bin/bash
# commentstrip - output file without comments or blanklines

# text color variables
bldred='\e[1;31m' # red - bold
bldblu='\e[1;34m' # blue
undwht='\e[4;37m' # white - underline
txtrst='\e[0m'    # text reset

# display usage if argument isn't given
if [ -z $1 ]; then
  echo " commentstrip <*c> <filename> - output file without comments or blanklines"
  exit 1
fi

# be sure that file exists
if [[ ! -f $@ ]] && [[ ! -f $2 ]]; then
  echo -e " ${bldred}*${txtrst} file doesn't exist"
  exit 2
fi

case $1 in
  # copy output to xorg server clipboard
  c ) shift
      # exit if root (root doesn't have access to user xorg server)
      if [[ `whoami` == root ]]; then
        echo -e " ${bldred}*${txtrst} must be regular user to copy to clipboard"
        exit 3; else
        grep -vh '^[[:space:]]*\(#\|$\)' "$@" | xclip -selection c
        echo -e " ${bldblu}*${txtrst} stripped contents of ${undwht}$@${txtrst} and copied to xorg server clipboard"
      fi ;;
  # else, display output to terminal
  * ) grep -vh '^[[:space:]]*\(#\|$\)' "$@" ;;
esac

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. Enjoy.

About Gen2ly

<3's linux

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

  1. Ciaran McCreesh says:

    What’s wrong with the good old-fashioned ‘ps auxw | grep [d]ns’ ?

  2. Gen2ly says:

    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.

  3. Shae says:

    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?)

  4. Gen2ly says:

    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.

  5. Dan says:

    Also, what about pgrep? :)

  6. Gen2ly says:

    Hold it, I gotta go chase a dog. Yeah, Dan if that what does it for you, lol.

  7. Iain says:

    Hi,

    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!

    cya.

  8. Gen2ly says:

    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.

  9. Iain says:

    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.

    cya.

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 )

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