Some more scripting I've been doing
by
, 03-01-2010 at 07:41 AM (446 Views)
There are times in my life when I just have a flood of ideas. I've had one over the last few days. It feels like I've just got this rush of programmer hormones coursing through my body, and I feel inspired to code, and learn and read about technology, during every waking hour. Here is some of what I've been doing.
This script was inspired by a question I saw on unix.com. It deletes lines of a certain range of character lengths from a file, and gives the user the option of writing it back to the file:
At first I made the mistake of not putting quotes around $filename. This caused the script to error when I input files with spaces in their names. I found I had a similar problem with the following script, which I fixed accordingly:Code:#!/bin/bash # deletes lines of a certain range length from a file # Writing the result to the file is optional. write="no"; for opt in $* do case "$opt" in -w ) write="yes"; shift;; -* ) shift;; * ) ;; esac done least=$1; great=$2; # lower and upper limits shift; shift; filname="$*"; if [ $write == "yes" ] then # delete lines with regular expression sed -e "/^.\{$least,$great\}$/d" "$filname" > tempfile.txt; cat tempfile.txt > "$filname"; rm tempfile.txt; else sed -e "/^.\{$least,$great\}$/d" "$filname"; fi unset write least great filname
I actually wrote this a while back, but didn't get it to work properly until now.Code:#!/bin/bash # This shell program prints a tree diagram of # the files in a directory, using recursion. # It is an emulation of the Linux tree command, # which I don't have on my Mac. olddir=$PWD; color="off"; for option in $* do case $option in --color ) color="on"; shift;; -* ) shift;; * ) ;; esac done declare -i stackheight=0; # stackheight determines the directory depth. function listfiles { cd "$1"; for file in * do for((i=0; $i < $stackheight; i++)) do printf "\t"; done # indentation shows the depth of the file if [ -d "$file" ] then if [ $color == "on" ] then printf "\e[34m"; # display directory names in blue fi fi printf "$file\e[0m\n"; if [ -d "$file" ] then stackheight=$stackheight+1; # more indentation listfiles "$file"; # recursive listing of files cd ..; fi done stackheight=$stackheight-1; # less indentation } listfiles "$1"; cd $olddir; unset i color stackheight;
The next shell script displays the content of my database and dumps it (backing it up in a script) at the same time. It uses a sed script that edits the backup script, as I found it was missing a vital line of code.
The shell script:
The sed script:Code:#!/bin/bash olddir=$PWD; gotomysql > /dev/null; # alias case $1 in -black ) color="\e[30m";; -red ) color="\e[31m";; -green ) color="\e[32m";; -yellow ) color="\e[33m";; -blue ) color="\e[34m";; -magenta ) color="\e[35m";; -cyan ) color="\e[36m";; -white ) color="\e[37m";; * );; esac # edit so it reads from standard input for confirmation # before doing the mysqldump, for safety purposes echo "mysqldump:"; if [ -e backups ] then sudo mysqldump Personal > backups/personal.mysql; # Lines 24 through 31 are for editing personal.mysql if [ -e ~/Desktop/Computer_Stuff/sed_scripts/edit_backup.sed ] then sed -f ~/Desktop/Computer_Stuff/sed_scripts/edit_backup.sed backups/personal.mysql > temppersonal.mysql; cat temppersonal.mysql > backups/personal.mysql; rm temppersonal; else echo "Script \"edit_backup\" does not exist. It was probably moved. Edit manually." 1>&2; fi else echo "Directory \"backups\" does not exist. It was probably moved." 1>&2; fi # ^ automatically backs up database echo "7days.mysql:" printf "$color"; sudo mysql < 7days.mysql; printf "\e[0m"; cd $olddir;
My most recent script was one that uses tr to encrypt a file. It uses a cypher I made up that converts alphabetical order to QWERTY order:Code:#!/usr/bin/sed 1{ :cont n /^[-\/]/bcont /^$/bcont i\ USE Personal;\ \ }
As you can see, I'm mostly into scripting now. I've put C aside because I've found that so far I haven't mustered the patience to learn a full-blown programming language. So I'm sticking to scripting languages for now. I'd like to learn C sometime soon thoughCode:#!/bin/bash # Encodes or decodes a file using a QWERTY/Alpha cypher code="none"; for opt in "$@" do # I might add more options sometime. case $opt in -*e* ) code="encode"; shift;; -*d* ) code="decode"; shift;; -* ) shift;; * ) ;; esac done filename="$1" if [ -e "$filename" ] then if [ $code == "decode" ] then # translates alpha to QWERTY cat "$filename" | tr "A-Z" "QWERTYUIOPASDFGHJKLZXCVBNM" | tr "a-z" "qwertyuiopasdfghjklzxcvbnm" | tr "0-9" "9876543210"; elif [ $code == "encode" ] then # translates QWERTY to alpha cat "$filename" | tr "QWERTYUIOPASDFGHJKLZXCVBNM" "A-Z" | tr "qwertyuiopasdfghjklzxcvbnm" "a-z" | tr "9876543210" "0-9"; # do if no options: else printf "\e[31mOptions not set: You must provide either -e to encode or -d to decode.\e[0m\n" 1>&2; fi # do if no filename: else printf "\e[31m$filename: File not found\nNote: This script will only work on files. It will not take bare strings.\e[0m\n" 1>&2; fi unset code filename
At the moment I've found a new interest in system administration, which has nothing to do with C. I'm into retro Unix, so I'm learning the text formatters nroff, troff, eqn, tbl, and pic just for fun. The text formatters are somewhat obsolete actually, as they were apparently made for line printers. I'm studying TCP/IP and MySQL some more. I'm also trying to learn Windows/DOS so I can administer a Windows network, though I have had trouble with that as I rarely have access to a Windows machine.
Coding excessively is a good thing in my opinion, because it exercises a skill that really matters, but at the same time I lose my ability to focus on other areas that matter. I'm sort of out of ideas right now, so I'll probably take a break.










