Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Christopher Dale

iSQL sucks, so make it work better

Runs the SQL code located in a file you pass as a parameter against the DSN you specify as a second parameter.

What does it need?:
-----------------------------------
* isql (if you're reading this, you should have this)
* A pager (less/more/most/etc, you should have this)
* bash (you can try it in your shell, but no guarantees)
* tr (you should have this)
* sed (you should have this)

The code:
-----------------------------------
#!/bin/bash
# {{{ sql $1 $2 - Run a SQL script
function sql
{
      tr '\n' ' ' < "$1" | tr '\t' ' ' | isql "$2" Username Password | sed '1,8d' | sed 's/^.*SQL> //g' | $PAGER
}
# }}}


Parameters:
-----------------------------------
$1 = The file that contains SQL
$2 = The DSN you want to run the SQL against


What does it do?:
-----------------------------------
It chops off the extraneous "thing" that is spit out whenever you start iSQL, it looks like this:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>


and then it pipes everything into your specified pager ($PAGER) so you can at least scroll around in the results instead of seeing an ASCII art wrapped mess of your results.

Example use:
-----------------------------------
username@host $> sql ~/sqlscripts/getallusers.sql DSNName


Notes:
-----------------------------------
For my purposes I use the same credentials for all my databases (so I don't forget them) which is why they are hardcoded in the function and not passed into the function.

By the way, if anyone has any time on there hands, could you please pick up the slack on the development of this program. It's about the only CLI tool (that I know of) that can work with TSQL on a MSSQL 2000 server. Or, if someone knows of something better, let me know!

Thanks:
-----------------------------------
Thanks everyone, and I hope others find this somewhat useful (although I know it's a far cry from perfect),

Chrelad

Deleting a line from a file matching criteria

I was messing around trying to figure out an easier way to remove entries from my /etc/portage/package.keywords file without having to drop into VI all the time and came up with this. You can put this into a bash script to make things alot easier on yourself if you are constantly altering your mask/use/keywords files in Gentoo.

sed -i '/x11-wm\/compiz/d' /etc/portage/package.keywords


The above example will remove any line that matches the regular expression x11-wm/compiz and edit the file in place "-i".

Compressing a directory with rar on Linux

I've been struggling to get this to work for so long that when I finally got it going I had to throw it up here so I wouldn't lose it.
rar a -m5 -R output.rar /etc/

This will create a max compression (not taking into account dictionary sizes and the like) archive of the entire etc directory.

Attach a screen before chrooting into your linux from scratch minimal system

This snippet is not so much a snippet as a tip. It's goal is to make your Linux from Scratch experience easier by allowing you the freedom of being able to attach and detach to a screen session from your host computer. Of course you would go through the book and change the chroot command to suit your needs, but just bear in mind that the chroot command you will use may be different from the one I use.

By using screen, you can start a lengthy compile and then detach, go about your business and re-attach another time. I use it primarily when I have to start a compilation from one computer, then later from another machine located somewhere else. Combined with SSH, this can be real handy.

desktop ~ # screen -S clfs
desktop ~ # chroot "${CLFS}" /tools/bin/env -i \
>     HOME=/root TERM="${TERM}" PS1='\u:\w\$ ' \
>     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
>     /tools/bin/bash --login +h
root:/#


At any time, you can hit ctrl+a+d to detach. Then if you want to re-attach just type screen -r clfs and whamo, your right back where you left off.

Thanks again to ChrisS67 for helping me with my GCC problem (or me forgetting to install findutils) ;)

SSH dynamic forward (Linux)

This command will create a dynamic forward from an SSH client to an SSH server. Basically what this does is allow you to use any local port (8080 in this example) as a proxy for any TCP application.

Feedback, suggestions and comments are all welcome!

# In the following example, we create a dynamic
# forward from port 8080 (client) to port 22 (host.net).

ssh -D 8080 username@host.net

# Now, we'll check out netstat to see what we
# have done.

netstat

# Active Internet connections (w/o servers)
# ...
# tcp 0 0 host.net:ssh client.com:60565 ESTABLISHED
# ...
#
# Awesome! Now we've got the connection. I'll add
# another post telling how to use this port as a
# socks proxy for any TCP application :)