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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Running program search | |
ps aux | grep --color=always -i "$@" | grep -v grep | grep -v "$0" |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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.
What’s wrong with the good old-fashioned ‘ps auxw | grep [d]ns’ ?
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.
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?)
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.
Also, what about pgrep? :)
Hold it, I gotta go chase a dog. Yeah, Dan if that what does it for you, lol.
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.
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.
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.