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

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

commentstrip

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.

#!/bin/bash
# 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
fi
case $1 in
# Copy output to xorg server clipboard
c ) shift
# Check if selection exists
if [ ! -f "$@" ]; then
echo " Selection \""$@"\" does not exist." && exit
fi
# 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
else
grep -vh '^[[:space:]]*\(#\|$\)' "$@" | xclip -selection c
echo " Comments stripped from file and copied to xorg server clipboard"
fi
;;
# Print output to terminal
* ) if [ ! -f "$@" ]; then
echo " Selection \""$@"\" does not exist." && exit
fi
grep -vh '^[[:space:]]*\(#\|$\)' "$@" ;;
esac
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.

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

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

    Reply
  4. Iain

    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.

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

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

    cya.

    Reply

Leave a reply to Iain Cancel reply