Simple Basic UNIX – more advanced vi tips

[problem]

Continuing along with more vi tips, these can help you out of a jam or just save you time – going back to command line, etc.

Saving a file, which is read-only? Running java, Perl, PHP from within vi.

[/problem]

[solution]

Use chmod.

[/solution]

[example]

Have you ever edited a file, made your changes only to find the file is write only? Then in zsh at least, it won’t save it.

Here is a simple work around:

 :!chmod u+w % 

Percent is a shortcut in vi to the current file.

How about shell programming or PHP, Perl or even Java. You want to run the code, just like this:

 !./% !javac %;java `echo % | sed 's/.java//'` !php -q % 

[/example]

[tags]UNIX, PHP, Perl, Java[/tags]

Creating favicon.ico

[problem]

You want to create a favicon.ico from Unix.

[/problem]

[solution]

  1. Open your image with GIMP
  2. If your image is not a square then resize the canvas with GIMP
  3. Scale the image to 16×16 pixel (Image, Scale image)
  4. Save as favicon.pnm or favicon.ppm with raw encoding
  5. Close GIMP

[/solution]

[example]

Then to convert favicon.pnm or favicon.ppm using the command line utility ppmtowinicon:

 $ ppmtowinicon -output favicon.ico favicon.pnm 

Or

 $ ppmtowinicon -output favicon.ico favicon.ppm 

[/example]

[reference]

Kewl tip on creating a favicon.ico with ppmtowinico – thanks to linuxproblem.org. 🙂

http://www.linuxproblem.org/art_19.html

[/reference]

Blogging Tip – do your quotes turned to dots when pasting

[problem]

One annoying thing I recently discovered, when copying code from my blogs (yes I use my own tips too! 🙂 ), quotes seem to turn into dots. Both for single and double quotes.

[/problem]

[solution]

The answer to this was explained in a blogging book, that apparently the quotes on a keyboard refer to feet and inches – not speech! 😉

The solution is simply to use ASCII notation.

[/solution]

[example]

Therefore a double quote becomes " and a single '

[/example]

[reference]

[/reference]

Simple Basic MySql – beginning mysql mysqlshow

[problem]

You want to perform command line display of mysql dbs with mysqlshow.

[/problem]

[solution]

Is this demo, I show the options for mysqlshow.

mysqlshow is a real easy way to quickly view your dbs, tables and rows.

[/solution]

[example]

Show Databases

 $ mysqlshow -i -u'root' -p'xxxxxx' +---------------+ |   Databases   | +---------------+ | demo          | | mysql         | | test          | +---------------+ 

Show number of tables under each db

 $ mysqlshow -v -i -u'root' -p'xxxxx' +---------------+--------+ |   Databases   | Tables | +---------------+--------+ | demo          |      1 | | mysql         |     15 | | test          |      0 | +---------------+--------+ 5 rows in set. 

Show total number of rows for each db

 [marcus@bagend ~]$ mysqlshow -v -v -i -u'root' -p'xxxxx' +---------------+--------+--------------+ |   Databases   | Tables |  Total Rows  | +---------------+--------+--------------+ | demo          |      1 |            7 | | mysql         |     15 |         1383 | | test          |      0 |            0 | +---------------+--------+--------------+ 5 rows in set. 

[/example]

Simple Basic MySql – beginning mysql db management

[problem]

You want to know how to do the following, through mysql command line interface:

    Connect to the mysql daemon
    Show the databases your user has access
    Show tables defined under that db
    Show schema for given table

[/problem]

[example]

 mysql -u'username' -p'password' -h'mysql_host' 

mysql_host is optional if the daemon is running on the same host.

Show databases

 show databases; 

Show tables

 show tables; 

Describe tables

 use demo; describe demo; 

For example:

 mysql> show databases; +----------+ | Database | +----------+ | demo     | | test     | +----------+ 2 rows in set (0.00 sec)  mysql> use demo;  Database changed mysql> show tables; +----------------+ | Tables_in_demo | +----------------+ | demo_table     | +----------------+ 1 row in set (0.00 sec)  mysql> desc demo_table; +-------+--------------+------+-----+---------+-------+ | Field | Type         | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id    | mediumint(9) |      | PRI | 0       |       | | day   | varchar(20)  |      |     |         |       | | val   | tinyint(4)   |      |     | 0       |       | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.10 sec) 

[/example]

Simple Basic Java Software – analysing arguments from command line

[problem]

You want a simple bit of java code that takes arguments from the command line and displays them.

[/problem]

[solution]

Good demo on how to obtain and handle java arguments.

  public class Arguments {    public static void main(String argv[]) {      if(argv.length > 0) {        System.out.println("args exec w " + argv.length + " args");          for(int i=0;i... 

[/solution]

[example]

Here is a demo:

 # java Arguments -a -x -y args exec w 3 args argv[0] = -a argv[1] = -x argv[2] = -y 

[/example]

Useful UNIX tip – generate 512 bytes of random data with dd

[problem]

You want to generate 512 bytes of random data

[/problem]

[solution]

Use UNIX dd (disk to disk) command with /dev/random blocks for truly random data, whereas /dev/urandom just pumps out whatever is available.

[/solution]

[example]

You need to escape the control characters, or else it will trash your screen. cat -ve escapes these control characters for you.

dd bs=1 count=512 if=/dev/urandom | cat -ve

dd is a very powerful command and can be used to read files, data, tapes, even disks, etc – varying the number of blocks with count and block size with bs.

if is the input device and of can be used to designate an output device.

dd bs=1 count=512 if=/dev/urandom | openssl base64

Also it can convert to upper and lower case, with conv=ucase, etc.

If you have an extremely large file, it can open at an offset – via the skip option, for example to skip the first 1k of the file and read 100 bytes:

dd if=access_log.techieblogs bs=1 skip=1024 count=1000  - - [04/Jun/2006:00:13:10 +0800] "GET /mysqldemo/run_q.php?database=mysql HTTP/1.1" 200 1364

[/example]