Page 1 of 1

Script Fun

Posted: Thu Apr 09, 2015 11:56 pm
by DrDominodog51
The title says all. This thread is a place where you can share your scripts to make others' lives easier. For TR's and our computer's sakes there are a few rules:
1) Include what programming language (and version if applicable) the script is in.
2) Include a brief description of what your script does.
3) Please include (many) comments showing how your script works.
4) No scripts that needlessly consume system or network resources, or perform malicious/illegal acts should be posted.
5) Infinite loops are allowed in monitoring scripts, provided there are steps taken to prevent CPU hogging (e.g. sleep for some number of seconds after each iteration), and the procedure to shut it down is clearly stated (e.g. "Hit ^C to terminate the script")
6) Scripts are allowed to modify (Config) files, but it must clearly say in the post and/or code which file is being modified and what the modifications are.
7) If your script takes a lot of cpu power , takes a lot of time to run, or other things like that, please add an additional note stating so.
8) You run these scripts at your own risk.
9) These rules may be changed.
Example of the formatting conventions (The parenthesis are labeling what the phrase is stating for the example):

This script converts user input into Base64 (what the script does). It is written in PHP (programming language) and should be ran from CLI (additional notes). Once there is a return you can type your input.

<?php
$data = ‘Hello’;
// I actually don't know why this line above must be there. I believe it makes $data a string not a variable.
$data = fgets(STDIN);
// This takes the user input and assigns it to (string) $data
echo base64_encode ($data);
// This changes $data into base64 and prints it.
echo “\n”;
// This returns a line,
?>


Comments do not need to appear after every line. Just explain what you are doing. Feel free to discuss others' scripts here.

Re: Script Fun

Posted: Sun Apr 12, 2015 4:28 pm
by just brew it!
Device names for removable devices are assigned dynamically in Linux. This can be confusing if you are trying to work directly with the underlying block device on removable media, or are using multiple hot-plugged USB-based serial port devices. This script, if run in a CLI window, polls the device entries in /dev once a second, and reports the names of removable storage and serial devices as they are inserted and removed. It runs continuously until stopped with a SIGINT (^C from the keyboard) or SIGTERM signal (sent via the kill command).
#!/bin/bash
# devmon - script to report on stdout whenever removable media or USB serial
# devices are inserted or removed
#
# Usage: devmon
#
#   Runs continuously until stopped with ^C

# Set up some temporary file names, based on PID of this shell
CURRENT=/tmp/devmon-cur.$$
PREVIOUS=/tmp/devmon-prv.$$
CHANGED=/tmp/devmon-chg.$$

# Make sure we clean up after ourselves when we are killed
trap "rm -f $CURRENT $PREVIOUS $CHANGED; exit" SIGINT SIGTERM
echo "^C to exit"

# Loop forever until killed
while true; do
    # Get list of current disk and serial TTY devices
    ls /dev/sd?* /dev/ttyUSB* 2>/dev/null >$CURRENT
    if [[ -f $PREVIOUS ]]; then
        # Compare with previous list
        diff $PREVIOUS $CURRENT >$CHANGED
        if [[ `wc -l <$CHANGED` -ne 0 ]]; then
            # List changed - display the differences
            grep '> ' $CHANGED | sed -e 's/> //g;s/$/ inserted/g'
            grep '< ' $CHANGED | sed -e 's/< //g;s/$/ removed/g'
            # Replace saved device list
            mv $CURRENT $PREVIOUS
        fi
    else
        # First time - just save the list to compare against next time
        mv $CURRENT $PREVIOUS
    fi
    # Sleep for a second before checking again to avoid hogging the CPU
    sleep 1
done

Re: Script Fun

Posted: Sun Apr 12, 2015 8:58 pm
by DrDominodog51
Since creating this thread, I started to clean up my hard drive. Here is what I found (even though 1 of these 2 are useless. Try and guess which one.):

This opens up Dunnet. This is a bourne shell script and requires Emacs to be ran.
#!/bin/sh

#  Dunnet

clear && echo "Dunnet By Ron Schnell." && emacs -batch -l dunnet


This is a Perl 5 script that calculates Pi. This will take a rather long time to run as there is a loop that runs 20000 times. It compare 3 different formulas. The comments are a bit sparse because I wrote this a long time ago and its 90% math functions. It may flo
# Note this cannot calculate any further due to language limitations (or at least my knowledge of Perl).

use Math::Trig;
use Math::BigFloat;
# BigFloat = Long Float
print "==== Machin's formula ====\n";
# This formula doesn't use double float because it is so inaccurate. The formula is Pi/4 = (4 * arcyan(1/5)) -  (arctan(1/239)).
$one_fifth = 1 / 5;
$one_239  = 1 / 239;
$quarter_pi = 4 * ( atanh($one_fifth) ) -  atanh($one_239);
print "Pi: " . $quarter_pi * 4 . "\n";

print "==== Gregory and Leibniz formula ====\n";
#This formula is to complex for me to type it in here without being able to use special characters, so there will be a link below this to the wikipedia page.
$current_pi = 0;
my $current_pi = Math::BigFloat->new('0.0');
my $current_compute = Math::BigFloat->new('0.0');
my $series_element = Math::BigFloat->new('0.0');
#This is making all of these variables into Floats
for ($k=1;$k<20000;$k++) {
  $series_element = ( (-1)**($k+1) ) / ( (2 * $k) - 1);
  $current_compute = $current_compute + $series_element;
  $current_pi = 4 * $current_compute ;
#This is the loop that runs twenty thousand times.
}
  print "Series Element: " . $series_element . "\n";
  print "Pi: " . $current_pi . "\n";

print "==== Bailey-Borwein-Plouffe Formula ====\n";
#Again; I will include the formula below.
my $BBP_current_pi = Math::BigFloat->new('0.0');
my $BBP_current_compute = Math::BigFloat->new('0.0');
my $BBP_series_element = Math::BigFloat->new('0.0');
#More variables being made into floats
for ($n=0;$n<280;$n++) {
  $BBP_series_element = ( (4 / (8 * $n + 1)) - (2 / (8 * $n +4)) - (1 / (8 * $n + 5)) - (1 / (8 * $n +6) ) ) * (( 1 / 16 ) ** $n);
  $BBP_current_compute = $BBP_current_compute + $BBP_series_element;
  $BBP_current_pi = $BBP_current_compute ;
 
if (($n % 10) == 0) {
    print "Index (" . $n . ") Pi: " . $BBP_current_pi . "\n";
  }
}
#This if loop prints out the results after every ten runs of the formulas.

For the second formula: http://en.wikipedia.org/wiki/Leibniz_formula_for_π
For the third formula: http://en.wikipedia.org/wiki/Bailey–Borwein–Plouffe_formula
The url tags weren't working at the time of this post.