Linux.org User-created Helpful Shell Scripts

Yes I tested it but replaced the destruction command with another command and it works as expected. You can remove it, it was just for the laughs.
 


I kinda want to remove that script - but you DID warn 'em.

Note: New users, do not run the script in this post:


Also, I wonder if @f33dm3bits tested the script before sharing it?
I wish there was a forum specifically for posting viruses f33's, comes pretty close! Not for financial/black-hat reasons but for creating visible havock on virtual machines or empty computers, i've seen a couple of youtube videos which contain that...
 
Look up 'fork bomb', if you want some good old fashioned terminal fun.
 
Well... I do have some practical scripts that could be used every day. One in particular, a window manager. However, it's not for the timid. It requires ksh, wmctrl, xwininfo and xdotool. (It'll let you know if they're not installed). Also, you'll need to set up keyboard shortcuts. It has seventeen options of which some can be added together.

It will figure out (pretty closely) the size of the top and bottom panels, then calculate the screen size without the panels. It works fairly good, but may need tweaking. I need to work in it a bit more. I've been using it for over 10 years. Since I was the only one using it, I didn't bother to flush it out. Now that there's some place someone may use it. I think it's time to finish it up.

The first several installations of it were on Red Hat based Linux. I installed it on my current machine running Manjaro about 5 years ago. It didn't need any tweaking other than installing the afore mentioned tools, so I'm hoping other distros will play nicely with it as well... Hopefully, we'll see.

To give an idea of what it does, Here's a couple of the functions.
# A brief explanation.
usage(){
echo
echo "Options:"
echo " -8 80x25(roughly) Window size."
echo " -a Manipulate the active window. (Default)"
echo " -b When resizing a window's height specify that it is to"
echo " use the bottom half of the screen, top is the default."
echo " -c Centers the window."
echo " -d Resizes window to full width."
echo " -f Resizes window to full height."
echo " -l Move window to the left of the screen."
echo " -r Move window to the right of the screen. (Working, kind of.)"
echo " -s Forces the user to select a window to manipulate."
echo " -t Resize a window to half the height of the current desktop."
echo " -w Resize a window to half the width of the current desktop."
echo " -u Places window at the top of the destkop."
echo " -h This help."
echo
echo
echo "Some Common options."
echo "Half screen window right: wm_ctrl.sh -warf"
echo "Half screen window left: wm_ctrl.sh -walf"
echo "Move window to the top: wm_ctrl.sh -u"
echo "Move window to the bottom: wm_ctrl.sh -b"
echo "Move window to the right: wm_ctrl.sh -r"
echo "Move window to the left: wm_ctrl.sh -l"
echo "Resize window to 1/3rd width: wm_ctrl.sh -1"
echo "Resize window to 2/3rd width: wm_ctrl.sh -2"
echo "Resize window to half width: wm_ctrl.sh -wa"
echo "Resize window to half height: wm_ctrl.sh -t"
echo "Center window in both directions: wm_ctrl.sh -C"
echo "Center window horizontally: wm_ctrl.sh -c"
exit 0
}
###########################################################################
# Main
###########################################################################
while getopts 1238abCcdflrstuwh opt
do
case "$opt" in
1) wd="$w13";;
2) wd="$w23";;
3) wd="$fw";;
# Approx 80x25 sizing.
8) wd=`echo "scale=5 ; $wm_wd * .427604167" | bc -l | xargs printf "%1.0f\n" ` ;
wh=`echo "scale=5 ; $wm_hi * .513238289" | bc -l | xargs printf "%1.0f\n"`;;
a) wtype=":ACTIVE:";;
b) hpos="$(($hh+$offset))";;
C) hpos=`echo "scale=0; ($wm_hi/2)-($win_hi/2)+$offset" | bc -l`
vpos=`echo "scale=0; ($wm_wd/2)-($win_wd/2)" | bc -l`;;
c) vpos=`echo "scale=0; ($wm_wd/2)-($win_wd/2)" | bc -l`;;
d) wd="$wm_wd";;
f) wh="$wm_hi";;
l) vpos="0";;
r) vpos=`echo "$wm_wd-$win_wd" | bc -l`;;
s) wtype=":SELECT:";;
t) wh="$(($hh-22))";;
u) hpos="0";;
w) wd="$hw";;
h|*) usage;;
esac
done
###########################################################################
# Do the work! ;)
###########################################################################
wmctrl -r "$wtype" -e 0,"$vpos","$hpos","$wd","$wh"
 
I also tested the script with the desctruct command in a vm and it works as expected there as well, causing my vm's installation to be destroyed.

It's good to be thorough!
 
Here's an alias for taking files that are being created/used by software on your computer, and putting those files in a more convenient place for personal access:

Code:
alias organize='find <top-folder> -maxdepth <number> -mindepth <number> -type d -exec cp -r {} <destination> \;'

find here is configured to take folders from an exact depth, and then those folders are given to cp and put into a more favorable place (use the same number for maxdepth and mindepth to specify an exact depth). I'm so happy that i actually figured out how to use the -exec option today!
 
alias organize='find <top-folder> -maxdepth <number> -mindepth <number> -type d -exec cp -r {} <destination> \;'
Nice script. Try using rsync. It will preserve the date and time stamps and much much more. That, and if your into learning, it's a worm hole all it's own. It has over 100 options and can be used to transfer/copy to/from folder to folder and to and from other hosts. It could be used to replicate important data / folders etc.. from your machine to a backup computer for safe keeping.

Something like this.
Code:
find /util -maxdepth 0 -mindepth 0 -type d -exec rsync -avn {} /tmp/output/ \;

The n in avn is for a dry run. You can test your code and never write a byte. Remove the n to run it. The a = archive v = verbose (tells you what files are being copied & more)

Just a thought.
 
Nice script. Try using rsync. It will preserve the date and time stamps and much much more. That, and if your into learning, it's a worm hole all it's own. It has over 100 options and can be used to transfer/copy to/from folder to folder and to and from other hosts. It could be used to replicate important data / folders etc.. from your machine to a backup computer for safe keeping.

Something like this.
Code:
find /util -maxdepth 0 -mindepth 0 -type d -exec rsync -avn {} /tmp/output/ \;

The n in avn is for a dry run. You can test your code and never write a byte. Remove the n to run it. The a = archive v = verbose (tells you what files are being copied & more)

Just a thought.
Yeah rsync is a powrrful tool but i'm generally confused about practical ways to use it, the normal copy command is very easy to understand but i do want to have a better grasp of rsync.
 
Look up 'fork bomb', if you want some good old fashioned terminal fun.
Fork bomb is a pretty great system-destroyer...i tested it on an ubuntu VM today (live without install), and it took about 30 minutes before it made the system run so badly that i had to power off the machine. I think that's neat because not only could i keep entering {icode]free[/icode] to check on how much virtual RAM was getting used up, but it could also theoretically be used for "evil" purposes since it's kinda slow:

Code:
:(){ :|:& };:

here's where i cound this particular fork bomb:

 
A word of caution... Do not run the above command unless you know what to expect. It's an amusing academic exercise that won't really break anything, but it will force a reboot.

That said, I wonder what made it take 30 minutes. It's usually much faster.

There are some great obfuscated fork bombs out there in like Perl.
 
A word of caution... Do not run the above command unless you know what to expect. It's an amusing academic exercise that won't really break anything, but it will force a reboot.

That said, I wonder what made it take 30 minutes. It's usually much faster.

There are some great obfuscated fork bombs out there in like Perl.
It might have been 15-20 minutes (never over-estimate my conception of time...) but i was happy that it was slow enough that i could keep running the "free" command to see the progress it was making. RIght off the bat, it was using less than 4GB of 8GB total RAM...it slowly made it's way up to 5GB, and then slowly made its way to giving me just a black screen that i couldn't click back into existence.

I should also noted that the output from the "free" command was not entirely linear, sometimes it was using less RAM than before, but it's clearly a virus-like type of command (that doesn't threaten your data unless you've got some outdated hardware...don't run with a bare metal machine with an HDD!) that slowly takes over and forces you to power-off your system.
 
Oh, it shouldn't actually harm your data - not that one I don't think, even on real hardware. I wonder if the VM has something to do with it taking that long. The last time I ran it on a real computer it was that same command and it killed it pretty quickly. It wouldn't surprise me too much if it behaved differently on virtualized hardware. The VM's software may even try to prevent things like that.

Anyhow, we probably shouldn't veer too far from the topic in this thread, so I'll stop gabbing like an old lady across the fence.
 
Oh, it shouldn't actually harm your data - not that one I don't think, even on real hardware.
It could, theoretically, if you force the computer and that causes the needle to scratch the platter on an HDD...but other than that it shouldn't hard the integrity of files.
 
In the spirit of practical become "practical joke", here is something i made today that could be used on computer systems at work or some sort of family computer network. WIth crontab, this behaves like a loop so you don't have to do anything else. It's very light-hearted and g-rated :)

Code:
#!/bin/bash

FNAME=<insert-absolute-directory-path-to-new-filename-in-desktop>

if [ -e $FNAME ]; then

    echo -e "\n\nOkay I know i wasn't very specific, but I'm in your bathroom! Explain yourself!" >>  $FNAME

else

    echo -e "Dear Desktop User,\n I know about some things you did a very long time ago, and i also know about some other stuff too.\nGive me a million dollar by 5pm eastern standard time  or else there's gonna be some problems dude!" >> $FNAME

fi
 
Here's the best possible version of my grep script for looking through your working directory and finding things that match text:
Code:
#!/bin/bash
#this will look through every file in a working directory and
#find the text files that contain a string
echo -e "Enter string you want to find in your files. This script only searches files"
read -p "in your working directory: " term

echo -e "\nUN-HIDDEN FILES\n"

FILES="$(grep -lIs "$term" *)"
#options mean show files containing text of file, omit binary files,
#omit error messages.

#file takes grep output to display their type,
#then sed removes error messages when files have spaces
file $FILES | sed '/No such file or directory/d'

FILESH="$(grep -lIs "$term" .*)"

echo -e "\nHIDDEN FILES\n"

file $FILESH | sed '/No such file or directory/d'  

echo -e "\nFind and grep can't process these files because they have spaces in them:\n"

#sed removes directories and their listings
ls *' '* | sed -n '/:$/q;p'

EDIT: changed to show the status of every non-directory file.
 
Last edited by a moderator:
Oh, it shouldn't actually harm your data - not that one I don't think, even on real hardware. I wonder if the VM has something to do with it taking that long. The last time I ran it on a real computer it was that same command and it killed it pretty quickly. It wouldn't surprise me too much if it behaved differently on virtualized hardware. The VM's software may even try to prevent things like that.

Anyhow, we probably shouldn't veer too far from the topic in this thread, so I'll stop gabbing like an old lady across the fence.
31 minutes ago, i decided to try the forkbomb just on my desktop, and it's actually progressing a lot slower than it did on the virtual machine. This is the RAM readout right after i started it:

Code:
total        used        free      shared  buff/cache   available
Mem:        15655348     8727412     4671728       20020     2256208     6586500
Swap:        2097148           0     2097148

This is the current RAM readout:

Code:
total        used        free      shared  buff/cache   available
Mem:        15655348     9170860     4127876       57216     2356612     6096892
Swap:        2097148           0     2097148

Luckily, fork bomb is not having any effect on my temperatures, even though i'm going to reboot after i post this just because i know what to expect and it's not interesting anymore. The terminal that's running the forkbomb is frozen/dysfunctional though! It isn't effecting anything else since i still have a remaining 4GB left and i'm not playing video games or anything...
 
31 minutes ago, i decided to try the forkbomb just on my desktop, and it's actually progressing a lot slower than it did on the virtual machine.

I wonder if there's now some in-built defenses against it?

Maybe try one of the many other fork bombs out there, if you're curious.
 
I wonder if there's now some in-built defenses against it?

Maybe try one of the many other fork bombs out there, if you're curious.
Yeah, i was thinking it was either because Ubuntu has more built in defenses, or maybe it once again has something to do with hardware...funny how that happens a lot with computers.

This article below has the forkbomb in other programming languages:


And this one has forensics and a way to limit the size of your fork bomb so that your system doesn't crash:

 
Here's a sed script that takes large numbers (from the thousands to anything less than ten trillion) and puts
commas where they should be:

Code:
#trillions
s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4,\5/g

#hundred billions
s/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g

#ten billions
s/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g

#billions
s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3,\4/g

#hundred millions
s/\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g

#ten millions
s/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g

#millions
s/\([0-9]\)\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2,\3/g

#hundred thousands
s/\([0-9]\{3\}\)\([0-9]\{3\}\)/\1,\2/g

#ten thousands
s/\([0-9]\{2\}\)\([0-9]\{3\}\)/\1,\2/g

#one thousands
s/\([0-9]\)\([0-9]\{3\}\)/\1,\2/g

I got the idea to do something like this because in a couple of the books i've read, the authors mention ways to put commas between large numbers, but i haven't seen anything that would apply that to anything that comes from the command line output:


Code:
xarathustra@xarathustra:~$ df | sed -f easy-numbers.sed
Filesystem      1K-blocks      Used  Available Use% Mounted on
tmpfs             1,565,544      3,292    1,562,252   1% /run
/dev/nvme0n1p2  805,437,640 421,062,300  343,387,900  56% /
tmpfs             7,827,708     23,812    7,803,896   1% /dev/shm
tmpfs                5,120         4       5,116   1% /run/lock
/dev/nvme0n1p1     523,248      5,364     517,884   2% /boot/efi
tmpfs             1,565,540       148    1,565,392   1% /run/user/1,000
/dev/nvme0n1p4 1,093,885,756   5,293,096 1,032,952,596   1% /media/xarathustra/4f763,174-e291-489a-9b8d-e418f7b70fa1
/dev/nvme0n1p3   19,982,160  15,252,016    3,689,760  81% /media/xarathustra/9d485,139-229e-4f36-ade2-d

Of course, it jumbles the columns from the df program, so you would need some code from awk to make this look neater...but my script does work to accomplish its intention.
 

Members online


Latest posts

Top