Thursday, March 31, 2011

Using custom shell scripts on Linux & OSX

You need to create a custom shell script and would like to run it from anywhere on your system.

1. Create a `bin` directory to store your script.
machine:~ user$ mkdir ~/bin

For Linux users this will create /home/[user]/bin, for OSX you'll have /Users/[user]/bin. You could name this directory whatever you'd like, but bin seems fitting.

2. Modify .bashrc or .bash_profile to put `bin` on your path.

If you're comfortable with vi you can: (Alternatively use any text editor you are comfortable with)

machine:~ user$ vi ~/.bash_profile [OSX]
user@machine:~$ vi ~/.bashrc [Linux]

Either way, add the following line:
PATH=$PATH:$HOME/bin

3. Save and exit.

4. Source your file for it to take effect:

machine:~ user$ . ~/.bash_profile [OSX]
user@machine:~$ . ~/.bashrc [Linux]

5. Now you can drop a shell script into your bin directory and have executable from anywhere on your filesystem (don't forget to make it executable!)

Example:

Last login: Thu Mar 31 11:07:30 on ttys004
machine:~ user$ pwd
/Users/user
machine:~ user$ touch example
machine:~ user$ vi example
...

#!/bin/sh
echo "Somewhere in Russia a little girl is warming up with your max."

...
machine:~ user$ mv example ~/bin/
machine:~ user$ chmod +x ~/bin/example
machine:~ user$ ls -l ~/bin | grep example
-rwxr-xr-x 1 user staff 80 Mar 31 14:25 example
machine:~ user$ cd /tmp/
machine:tmp user$ example
Somewhere in Russia a little girl is warming up with your max.
machine:tmp user$


Fin

Lift weights - Lift spirits

Monday, March 14, 2011

Simple command line encryption

Works on OS X, Linux, anywhere with OpenSSL installed:
(NOTE: With OS X Lion breaking TrueCrypt - this is simple replacement that, in a way, simplifies things.)

To encrypt the file:
openssl des3 -salt -in infile.txt -out encryptedfile.txt
To decrypt the file:
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt
The -a option, stores the encrypted file in base64 instead of binary.

To encrypt the file stored in base64:
openssl des3 -a -salt -in infile.txt -out encryptedfile.txt
To decrypt the file stored in base64:
openssl des3 -d -a -salt -in encryptedfile.txt -out normalfile.txt
To see a list of all available ciphers and other command information:
openssl -h

Other examples(notes to self...):
openssl des3 -salt -in rev27_*****_2010.zip -out rev27_*****_2010.zip.des3
openssl des3 -salt -in pf_*****_2010.zip -out pf_*****_2010.zip.des3

Monday, August 2, 2010

View output in console and write to file using tee - Mac OS X 10.6.4

Use tee to send output to the console and to a file:

machine:dir user$ ./some_program | tee output_file.txt

Executing the above command will display the output of some_program immediately in the console and write the same contents to output_file.txt.

I use this command mostly for diffs against svn, saves a step. Also, though I'm posting this as a Mac OS X terminal option, I first discovered this on Ubuntu 8.04 though I sure the history or this command is much older. I recall from the documentation to think of this in plumbing terms: Send your output down a pipe to a "T" essentially sending the same stream/flow to two different places.

~Fin

Friday, January 15, 2010

Recursive SVN Directory Removal - Ubuntu 9.10 Karmic

From time to time it can be handy to remove subversion references from code that is sitting on the file system. The following command will remove all '.svn' directories from the current directory on down:

user@machine:~/dev/sample1$ rm -rf `find . -type d -name .svn`

(Please note those are grave accent quotes - below the tilde on the key left of the one(1) key.)

This command should work for all flavours of Unix.

~Fin

Tuesday, January 12, 2010

Create a symbolic link - Ubuntu 9.10 Karmic

ln -s /path/to/real/file /path/to/non-existant/file

So you could do like:

user@machine:~/devtools/jdk$ ll
drwxr-xr-x 10 root root 4096 2010-01-12 11:11 jdk-1.6.0
user@machine:~/devtools/jdk$ ln -s jdk-1.6.0/ current
user@machine:~/devtools/jdk$ ll
lrwxrwxrwx 1 brad brad 10 2010-01-12 11:25 current -> jdk-1.6.0/
drwxr-xr-x 10 root root 4096 2010-01-12 11:11 jdk-1.6.0
user@machine:~/devtools/jdk$

~Fin

Wednesday, October 28, 2009

Start, stop, restart MySql - Ubuntu 9.04 Jaunty

user@machine:~$ sudo /etc/init.d/mysql [start, stop, restart]

~Fin

Monday, October 26, 2009

Reset root password for MySql 5.* - Ubuntu 9.04 Jaunty

user@computer:~$ sudo dpkg-reconfigure mysql-server-5.0

This will pop a "dialog" in your terminal and advise you to set a root password. Verify, tab to <OK> and try logging in again:

user@computer:~$ mysql -uroot -p<password_you_just_set>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


~Fin