Bash Scripts


bash

From time to time, my bash scripts turn out to be useful to more than just me.
It doesn’t happen very often, but on the chance that it does, I’m archiving them here.

DeMoodle

At Lancaster University, we use Moodle for our Virtual Learning Environment, which is fine most of the time, but a bit of a pain for marking.
This is doubly so,  in the case of code submissions, where Moodle will quite happily mangle file names breaking any linkage used in the code.

To reverse Moodle’s mangling, I wrote this quick bash script, which not only creates a folder tree in the form /student/submission-id/…files… but will also unzip any submitted archives in to the correct folder in said tree.

If there are any other Moodle sufferers users out there who find this useful and extend it in any way, I will gladly merge their edits for the greater good!

#!/bin/bash

for filename in ./*
do
echo $filename
metafile=$(basename "$filename")
extension="${metafile##*.}"
basename="${metafile%.*}"

IFS=‘_’ read -a parts <<< "$filename"

user=${parts[0]}
index=${parts[1]}
submission=${parts[@]:2}

mkdir -p "$user$index"

if [ "$extension" = "zip" ]; then
unzip "$filename" -d "$user$index/"
rm "$filename"
else
mv "$filename" "$user$index/$submission"
fi

done;

Download link: demoodle.sh

Random Radio Station

Pull a random radio station from my stream list (used in my radio projects) and play it in console-mode vlc. Try net radio channel hopping, and a new station is only a ctrl+c, up, enter away!

cvlc $(curl -s http://johnvidler.co.uk/radio/data/streams.js | jshon -e $(shuf -i 0-`curl -s http://johnvidler.co.uk/radio/data/streams.js | tr -cd ‘{‘ | wc -c` -n 1) -e stream -u)

Using Tree To Find The Minimum Power Of Two Larger The File Will Fit To

Used as part of a study in determining the average file size found on the modern Linux/UNIX/BSD system.

If you use this script, I would love to have your filesizes.csv output for /home (or equivalent) so I can use it in my research! No personal data is saved by this script, it just uses ‘tree’ to walk the file system and report sizes.

Because of the length of this file (it’s moderately long) the script can be viewed here: filesize.sh

Generate a Word List from Google’s nGram Lists

This code is terrible, don’t use it (especially if you don’t have much RAM!) – I only put this up for the craziness of it all.

lynx -dump -listonly http://storage.googleapis.com/books/ngrams/books/datasetsv2.html | cut –delim=‘ ‘ -f2 | grep gz | xargs -n 1 curl -0 | gunzip | grep 2008 | cut -f 1 | sed ‘s/\s/\n/g’ | sort -u > uniquewords.txt

Generate a Quoted, Comma Separated String From a Word List

Note, this assumes that output.txt is blank or non-existent.

cat input.txt | xargs -I var -n 1 echo -n \"var\", >> output.txt

PulseAudio Control Wrapper

Save in an executable file as “/usr/local/bin/pavol”, then call “pavol up/down/mute” to control PulseAudio from the terminal – I’m using this for my media key hooks

#/bin/bash

VOLUME_STEP=10

case "$1" in
        "up")
                /usr/bin/pactl set-sink-volume 0 +$VOLUME_STEP%
                ;;
        "down")
                /usr/bin/pactl set-sink-volume 0 –$VOLUME_STEP10%
                ;;
        "mute")
                /usr/bin/pactl set-sink-mute 0 toggle
                ;;
esac
MUTED=$(pactl list sinks | grep Mute: | cut -d: -f2)
LEVEL=$(pactl list sinks | grep ‘^[[:space:]]Volume:’ | head -n $(( $SINK + 1 )) | tail -n 1 | sed -e ‘s,.* \([0-9][0-9]*\)%.*,\1,’)
if [ "$MUTED" == " yes" ]; then
        notify-send -t 500 -a pavol -i sound "Volume Control" "Muted"
else
        notify-send -t 500 -a pavol -i sound "Volume Control" "Volume: $LEVEL%"
fi

This Post Has Been Viewed 371 Times