Tuesday, August 5, 2008

Init cowsay

Yes! The script to end all scripts! This is just what you need!

Ok, so it's a script to basically put a command into your ~/.bashrc file. I did it a long, long time ago, because... well, I suppose for no reason really, but it cam in handy, when I was doing a lot of stuff on a lot of different machines, and logging in through ssh to other machines at the same time, and it was useful for the cow to remind me where I was entering.

Right, so it's not very useful. Also it is now overblown, because there's all these options! When I first made the script it was just:
cp ~/.bashrc ~/.bashbuprc && echo 'cowsay -e xx -T \ U -f small.cow $USER@$HOSTNAME' >> ~/.bashrc

and now it is 100 lines of code...

Anyway, it requires cowsay, which I just got going sudo apt-get install cowsay. cowsay is very configurable, so you can make whatever creature you want as your console splash article. You can even specify any command you want with the -c switch!

And then, when you get bored, just use the -r switch to revert.

Important safety notice! This may screw something up with your .bashrc file! It shouldn't, but it might, so don't trust nobody and back it up.

Without further ranting, here's the script:
1  #!/bin/bash
2  #
3  # Init cowsay
4  # Enters cowsay into your local .bashrc file, so that you see 
5  # a dead cow every time you use the temrinal. 
6  # Yes, the very thing you need!
7  # It is actually possible to use this with other commands and files.
8  #
9  # Potential issues:
10 #   Potential serious damage to your ~/.bashrc file is possible!
11 #   Best to keep backup of the file!
12 # Parameters:
13 #   -r|--remove     Remove line instead of adding
14 #   -f|--force      Force insert even if line already exists
15 #   -h|--help       Print usage and quit
16 #   -b|--backup     Backup bashrc file
17 #   -c|--command    Use a custom command instead of cowsay
18 # Requires:
19 #   cowsay (http://www.nog.net/~tony/warez/cowsay.shtml)
20 # Author:
21 #   Konrad Siek 
22 
23 # Initiate arguments
24 insert='true'
25 force='false'
26 backup='false'
27 command='cowsay -e xx -T \ U -f small.cow $USER@$HOSTNAME'
28 bashrc="$HOME/.bashrc"
29 
30 # Use getopt to parse command arguments.
31 options=`getopt    -o b::rc:fh \
32     --long backup::,remove,command,help,force \
33     -n $0 -- "$@"`
34 
35 # Proceed if everything is OK.
36 if [ $? == 0 ] 
37 then
38     # Set the parsed command options.
39     eval set -- "$options"
40 
41     # Setup selected options
42     while true ; do
43         case "$1" in
44             -r|--remove
45                 # Remove line instead of adding
46                 insert='false'
47                 shift 
48             ;;
49             -f|--force
50                 # Force operation even if line already exists
51                 force='true'
52                 shift 
53             ;;
54             -h|--help
55                 # Print usage 
56                 echo "Usage: $0 (options)"
57                 echo "\t-r|--remove\t\tRemove line instead of adding"
58                 echo "\t-f|--force\t\tForce insert even if line already exists"
59                 echo "\t-h|--help\t\tPrint usage"
60                 echo "\t-b|--backup\t\tBackup bashrc file"
61                 echo "\t-c|--command\tUse a custom command instead of cowsay"
62                 exit 0;
63                 shift 
64             ;;
65             -b|--backup)
66                 # Backup bashrc file
67                 backup='true'
68 
69                 # Optionally set backup file name
70                 if [ $2 != '' ] 
71                 then
72                     backup_file=$2
73                 fi
74                 shift 2
75             ;;
76             -c|--command)
77                 # Use a specific command instead of cowsay
78                 command=$2
79                 shift 2
80             ;;
81             --) 
82                 # Stop parsing options
83                 shift
84                 break
85             ;;
86             *) 
87                 # Weird error
88                 echo "Something went horribly, horribly wrong in the interior."
89                 exit 1 
90             ;;
91             esac
92     done
93     
94     # Check if the command is already in there
95     if [ ! -e $bashrc ]
96     then
97         exists='0'
98     else
99         exists=`cat $bashrc | fgrep "$command" | wc -l`
100
101        # Backup on demand
102        if [ $backup == 'true' ]
103        then
104            cp $bashrc $backup
105        fi
106    fi
107
108    if [ $insert == 'true' ] 
109    then    
110        # Check if we should insert it nevertheless 
111        continue=`expr \( $force == 'true' \| $exists == '0' \) `
112        # echo "not $exists and $force is $continue"
113
114        # If possible, add the command in the appropriate place
115        if [ $continue != '0' ]
116        then
117            # Insert command into file
118            echo "$command" >> $bashrc
119        else
120            # Oh noes!
121            echo "$0: Command '$command' already exists in file '$bashrc'."
122            echo "$0: If you know what you're doing you can try the -f option."
123            exit 1;
124        fi
125
126    else
127        if [ $exists != '0' ]
128        then
129            # Remove all lines that match from file
130            cat $bashrc | fgrep -v "$command" > $bashrc
131        else
132            # Nothing to do...
133            echo "$0: Nothing to do - command '$command' not in file '$bashrc'."
134        fi
135    fi
136fi


The code is also available at GitHub as bash/init_cowsay.

No comments: