Jump to content


Check out our Community Blogs

DarkLordoftheMonkeys

Member Since 22 Oct 2009
Offline Last Active Jul 15 2010 11:26 AM
-----

#551110 scanf problem

Posted by DarkLordoftheMonkeys on 09 April 2010 - 04:02 PM

It's %d, not &d.
  • -1


#546195 Neat things you can do with Unix

Posted by DarkLordoftheMonkeys on 09 March 2010 - 09:54 AM

Some of these are useful hacks, some are just amusing productivity killers. Most of them work on all shells, though if I'm not sure, I'll say so...

Hack #1: Change the prompt

The environment variable PS1 controls the prompt the shell gives you when it asks for a command. You can set it just like any other variable:

export PS1="\s-\v\$ "
The \s, \v, and \$ are escape sequences that represent strings to use for the prompt. This prompt would display the shell name followed by the version number, followed by a dollar sign.

Here are some of the escape sequences for the prompt:

\A - time of day (24-hour format)
\H - host name
\h - host name up to the first dot
\s - shell
\u - username
\v - version
\w - working directory
\W - trailing element of the working directory
\! - current command history number
\@ - time of day (12-hour format)
\$ - dollar sign

Note: The Z shell might use a different variable.


Hack #2: Reverse the text of a file

Typing "rev filename" will output the contents of a file, with each line reversed. You can create a backwards file using I/O redirection. This works on both Mac OS X and Linux. I don't know about other systems.

Hack #3: banner

A command created just for fun, present in Unix since the very early days (I learned about it from the 7th Edition Unix Manual).

banner -width "text"
Here's the banner I created by typing "banner -50 '/usr/bin/banner'":

            # 
            ####
               ####
                  ### 
                     ####
                        #### 
                           ### 
                              ####
                                 #### 
                                    ####
                                       ####
                                           ### 
                                             # 
                               # 
                ################ 
             ################### 
            #################### 
            #### 
            ###
            ###
             ##
               #               # 
            #################### 
            #################### 
            #################### 
            # 
            #####        ###
            #####      #######
             ##      ########## 
            ###      ######   ##
            ##      #####      # 
            #       #####      # 
            #      #####      ## 
            ##     #####     ###
             ##   ######   ##### 
             ##########    ##### 
               ###### 
            #                  # 
            #################### 
            #################### 
            #################### 
            #             ###
                            ## 
                             ## 
                             ###
                         ####### 
                         ####### 
                         #######
                              
            # 
            ####
               ####
                  ### 
                     ####
                        #### 
                           ### 
                              ####
                                 #### 
                                    ####
                                       ####
                                           ### 
                                             # 
            #                                # 
            ################################## 
            ################################## 
            ################################## 
               #            ##
             ##               ##
            ##                ##
            ##                ## 
            ###              ###
             #####        ######
              ################ 
                ############
                           
            #                  # 
            ####################      ### 
            ####################     ##### 
            ####################     #####
            # 
            #                  # 
            #################### 
            #################### 
            #################### 
            #               ##
                             ## 
                              ##
                              ## 
            #               #### 
            ####################
            ################### 
            ################
            # 
            # 
            ####
               ####
                  ### 
                     ####
                        #### 
                           ### 
                              ####
                                 #### 
                                    ####
                                       ####
                                           ### 
                                             # 
            #                                # 
            ################################## 
            ################################## 
            ################################## 
               #            ##
             ##               ##
            ##                ##
            ##                ## 
            ###              ###
             #####        ######
              ################ 
                ############
                           
                ### 
              ########
             ##########   ##### 
            ####    ####  ######
            ##        ##      ## 
            ##        ##       # 
             ##      ##       ## 
              ##################
            ###################
            ##################
            ## 
            #
            #                  # 
            #################### 
            #################### 
            #################### 
            #               ##
                             ## 
                              ##
                              ## 
            #               #### 
            ####################
            ################### 
            ################
            # 
            #                  # 
            #################### 
            #################### 
            #################### 
            #               ##
                             ## 
                              ##
                              ## 
            #               #### 
            ####################
            ################### 
            ################
            # 
                   ###### 
                ############ 
              ################ 
             #####   ##   ##### 
            ###      ##       ##
            #        ##        # 
            #        ##        # 
            #        ##       ##
             #       ##     ####
             ##      ##########
               ##    ########
                     #### 
            #                  # 
            #################### 
            #################### 
            #################### 
            #             ###
                            ## 
                             ## 
                             ###
                         ####### 
                         ####### 
                         #######
Hack #4: screen

screen is a Unix program that lets you have multiple windows open in the same console. When you enter screen by typing "screen", it brings up a window that gives you information about screen. screen has several keystrokes that you can use, all beginning with Ctrl+A. To add a new window, type Ctrl+A Ctrl+C, or if that doesn't work, Ctrl+A c (type those two keystrokes in succession). You can navigate between windows using keystrokes like Ctrl+A Ctrl+N (go to the next window) and Ctrl+A followed by a number (go to the numbered window). I'm still learning to use this, so I don't know much about it, but here are some of the commands:

Ctrl+A Ctrl+A - go to the last window open
Ctrl+A Ctrl+C - create a new window
Ctrl+A Ctrl+N - go to the next window
Ctrl+A N - go to the Nth window
Ctrl+A k - kill the current window
Ctrl+A " - prompt for which window to go to


Hack #5: Emacs games

Emacs is a great programming environment (the second best after Vim). It's also a great gaming environment. When in Emacs, you can type M-x to get to the games. On some systems (like my Mac), the Meta key is the Escape key. After you type this, you will get a prompt at the bottom of the screen. Enter the title of the game to play. Games include tetris, pong, blackbox, solitaire, dunnet, life, decipher, mpuz, 5x5, hanoi, lm, morse-region, and studlify-region.

Hack #6: Dumb terminal nostalgia

Ever wished you could go back to the days of teletypes and monochrome screens, with the PDP-11 and other ancient computers? Well now you can (sort of), by setting the environment variable TERM to "dumb". Enter this in the command line:

export TERM=dumb
Now the terminal is monochromatic, does not run full-screen apps correctly, and does not recognize escape sequences, just like an old dumb terminal. Kind of a stupid thing to do, but I thought it was kind of neat. To change back, just type "TERM=xterm-color" or whatever your default terminal is.

Hack #7: Scheduling tasks with crontab

crontab is used to edit the crontab file that configures cron, a daemon that runs tasks at scheduled times. Make sure you have EDITOR set to vi or emacs or whatever you use, as crontab will open the file in that editor. Here is most of what you need to know:

crontab -u myusername -e
The syntax for the crontab file is:

minute (0-59)     hour (0-23)     dayofmonth     month     dayofweek    command
To schedule something for all days, or all hours, you would have a * in that field.

Commands involving terminal output will not work.


Hack #8: Editor keybindings

You can use the keybindings of your favorite text editor by setting them with a very simple command.

set -o editor
If editor is vi, then when you press the escape key, you will go into vi command mode, where you can type 0 to go to the beginning of the line, dd to delete, etc.


Hack #9: hexdump

This is a command you can use to look inside a binary file and see its raw code, in hexadecimal. The command is:

hexdump -C filename
Hack #10: xeyes

An X-Window program that demonstrates cursor movement detection. A pair of eyes watches what you do and reports to the boss. Completely useless, but amusing.
  • -1


#535681 Utilizing the Unicode character set

Posted by DarkLordoftheMonkeys on 11 January 2010 - 09:04 AM

Unicode is a 16-bit character set that includes characters for Asian, Arabic, Greek, and other languages as well as several special symbols. It is backwards compatible with ASCII.

The code name for Unicode is utf-8, sometimes utf-16. To include Unicode characters in an HTML document, use the tag:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

To include Unicode characters in an XML document, set the encoding to Unicode, like this:

<?xml version="1.0" encoding="utf-8" ?>

Several programming languages, such as Javascript and C99, allow you to print Unicode characters using the escape sequence \uXXXX, where XXXX is the hexidecimal code for the character.

Example:

printf( "\u0250\n ");

prints an upside-down a. I made a Javascript that flips inputted text upside down using this piece of information:

<html>
<head>
<title>lksag</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function fliptext(){
	var inputText = document.getElementById('textin').value;
	var textArray = new Array();
	var flipped = document.getElementById('flip');
	for( i = 0; i < inputText.length; i++ ){
		textArray[i] = inputText.charAt(i);
	}
	var reverseText = textArray.reverse();
	flipped.innerHTML = "";
	for( i = 0; i < reverseText.length; i++ ){
		flipped.innerHTML += upsideDown(reverseText[i]);
	}
}
function upsideDown( ch ){
	switch (ch){
		case 'a': return "\u0250";
		case 'A': return "\u0250";
		case 'b': return "q";
		case 'B': return "q";
		case 'c': return "\u0254";
		case 'C': return "\u0254";
		case 'd': return "p";
		case 'D': return "p";
		case 'e': return "\u01dd";
		case 'E': return "\u01dd";
		case 'f': return "\u025f";
		case 'F': return "\u025f";
		case 'g': return "\u0183";
		case 'G': return "\u0183";
		case 'h': return "\u0265";
		case 'H': return "\u0265";
		case 'i': return "\u0131";
		case 'I': return "\u0131";
		case 'j': return "\u027e";
		case 'J': return "\u027e";
		case 'k': return "\u029e";
		case 'K': return "\u029e";
		case 'l': return "l";
		case 'L': return "l";
		case 'm': return "\u026f";
		case 'M': return "\u026f";
		case 'n': return "u";
		case 'N': return "u";
		case 'o': return "o";
		case 'O': return "o";
		case 'p': return "d";
		case 'P': return "d";
		case 'q': return "b";
		case 'Q': return "b";
		case 'r': return "\u0279";
		case 'R': return "\u0279";
		case 's': return "s";
		case 'S': return "s";
		case 't': return "\u0287";
		case 'T': return "\u0287";
		case 'u': return "n";
		case 'U': return "n";
		case 'v': return "\u028c";
		case 'V': return "\u028c";
		case 'w': return "\u028d";
		case 'W': return "\u028d";
		case 'x': return "x";
		case 'X': return "x";
		case 'y': return "\u028e";
		case 'Y': return "\u028e";
		case 'z': return "z";
		case 'Z': return "z";
		case '0': return "0";
		case '1': return "\u21c2";
		case '2': return "\u1105";
		case '3': return "\u1110";
		case '4': return "\u3123";
		case '5': return "S";
		case '6': return "9";
		case '7': return "L";
		case '8': return "8";
		case '9': return "6";
		case ' ': return " ";
		case '\n': return "<br />";
		case '.': return "\u02d9";
		case ',': return "\'";
		case '\'': return ",";
		case '\"': return ",,";
		case '!': return "ยก";
		case '?': return "\u00bf";
		case '@': return "@";
		case '#': return "#";
		case '$': return "$";
		case '%': return "%";
		case '^': return "v";
		case '/': return "/";
		case '\\': return "\\";
		case '<': return ">";
		case '>': return "<";
		case '(': return ")";
		case ')': return "(";
		case '[': return "]";
		case ']': return "[";
		case '{': return "}";
		case '}': return "{";
		case ':': return ":";
		case '*': return "*";
		case '-': return "-";
		case '+': return "+";
		case '=': return "=";
		case '&': return "+";
		default: return "";
	}
}
</script>
<style type="text/css">
div{
	font-family: Courier;
	width: 400px;
	text-align: right;
}
</style>
</head>
<body>
<form>
<textarea id="textin" cols="35" rows="5" onkeypress="fliptext();" onkeyup="fliptext();">
</textarea>
<br />
<div id="flip"></span>
</form>

</body>
</html>

There are a few ways to find the hexidecimal code for a Unicode character. The method that I use is the copy the character into Vim, then type ga in command mode when the cursor is over the character. This gives the decimal, octal, and hexidecimal values.
  • 1


#534510 Shell script that emulates the tree command in Linux

Posted by DarkLordoftheMonkeys on 02 January 2010 - 05:14 PM


#!/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

    let stackheight=$stackheight-1;

    # less indentation

}

listfiles "$1";

cd $olddir;

unset i color stackheight;


  • 1


#532867 Vim hacks

Posted by DarkLordoftheMonkeys on 19 December 2009 - 09:46 AM

Hack #1: The Vim startup file

The Vim startup file is a script, written in Vimscript, that you put in your home directory (~). The script is executed every time Vim starts. It is usually used to customize Vim by specifying the default settings. The startup file must have the filename _vimrc. Here is the startup script that I use:

" My Vim setup script
set number
set autoindent
set nowrap
set ruler
set noignorecase
set encoding=utf-8
syntax on
" Use :TOhtml
let html_use_css = 1


Hack #2: Incrementing and decrementing numbers

There are two keystrokes for changing the value of the number under the cursor. Typing Ctrl+A increments the number. Typing Ctrl+X decrements the number. When you do this, the entire number is changed, not just the digit under the cursor.


Hack #3: Bookmarking parts of a document

Bookmarking helps you to move around a document more quickly by marking a place to return to later. Marks are given letters that you can type to return to them. Typing m followed by a letter marks the character under the cursor. Here are the keystrokes:

mx - mark the character under the cursor with mark x
'x - return to the line marked by x
`x - return to the character marked by x
'' - return to the line the cursor was on before the last jump
`` - return to the character that was under the cursor before the last jump
<start bracket>' - go back one mark
<end bracket>' - go forward one mark


Hack #4: Creating macros

Macros are sequences of keystrokes that can be mapped to one key. When that key is pressed, all of the keystrokes mapped to that key are executed. It is generally best to use keys that are not used elsewhere for mapping, such as k, K, and =.

:map = sequence - map a sequence to =
:unmap = - remove mapping for =
:map! = sequence or :imap = sequence - map a sequence to = for insert mode
:unmap! = or :iunmap = - remove mapping for = in insert mode
:map - show mappings


Hack #5: Command history

Vim has a command history that you can search to avoid having to type long ex commands multiple times. It even saves commands from previous sessions. The command history can be accessed using the keystroke q: (q followed by a colon). This will bring up a list of commands at the bottom of the screen. You can scroll through and edit commands in the command history just like you would in the document. When you find the command you want, simply hit enter to enter the command.


Hack #6: Syntax highlighting

Vim color codes, or highlights code, making it easier to spot mistakes. Syntax highlighting can be turned on by typing :syntax on, and turned off by typing :syntax off. Vim provides syntax highlighting for several languages, including:

C
C++
HTML
CSS
Javascript
PHP
SQL
BASIC
DOS Batch
Shell scripts
Perl
Ruby
Python
Java
Fortran
Pascal
Ada
Lisp
Vimscript
sed
AWK
Assembly language
Objective-C
XML
DTD
XSLT

And probably many more.


Hack #7: Save syntax highlighting to HTML

The ex command :TOhtml creates an HTML file that contains the highlighted text of a document. The default is to use font tags to color code the text, but you can set it to use CSS instead by typing :let html_use_css = 1 first.


Hack #8: Get the ASCII or Unicode value of a character

There are two ways to get the code value of the character under the cursor. To get the ASCII value, type :ascii at the ex prompt. There is a keystroke you can use to get the Unicode value. When the character is under the cursor, type ga. Make sure that encoding is set to utf-8 first.


Hack #9: Tools for multiwindow editing

The keystroke Ctrl+W is always used for multiwindow editing commands. There are many commands you can use, but here are a few of them:

^Ws - split the window horizontally
^Wv - split the window vertically
^W^W - go to the next window
^W+ - increase the current window's height by the preceding number of lines
^W- - decrease the current window's height by the preceding number of lines
^W> - increase the current window's width by the preceding number of columns
^W< - decrease the current window's width by the preceding number of columns
^W_ - set the current window's height to the preceding number of lines
^W| - set the current window's width to the preceding number of columns
^Wr - rotate windows


Hack #10: Toggle the case of a character

To toggle the case of a character, type ~ when the cursor is over that character. Vim will change the case and then move to the next character. This also allows you to change the case of a group of characters very quickly by holding down the tilde key.


Hack #11: Show marks and buffers

:display - show all character buffers and their contents.

:marks - show all markers and a section of the text surrounding the characters they mark


Hack #12: Explore, Sexplore, and Vexplore

Explore, Sexplore, and Vexplore (standing for Split-explore and Vertical-explore, respectively), are part of a plugin that allows you to explore and manipulate the filesystem from within Vim. Typing :Explore shows the contents of the current directory in the current window. Vexplore and Sexplore split the window and show the filesystem in the new window. From this new window you can navigate through the filesystem and also create and delete files and directories.
  • 1


#529955 bash hacks

Posted by DarkLordoftheMonkeys on 02 December 2009 - 04:17 PM

Here are 12 useful tricks for more effective bash shell use and programming:

Hack #1: Generating a random number:

The environment variable $RANDOM produces a random integer between 0 and (2^16)-1. You can use it in a shell script to randomly select a list of instructions to execute.


arr=([0]="Random string #1" [1]="Random string #2" [2]="Random string #3");
declare -i randomString=$RANDOM%3;
echo $randomString


Hack #2: Toggling between directories:

Use cd - to toggle between the current directory and the last one. The directory you switch to is printed to the terminal.


Hack #3: pushd, popd, and dirs:

Unix has a useful object called the directory stack, which provides a path to take through your filesystem.

pushd directory adds directory to the top of the directory stack. When you pop the stack, bash brings you to the directory at the top of the stack.

popd pops the directory stack, then displays the current directory plus the directory stack.

dirs displays the directory stack as if you had just popped it.


Hack #4: Using aliases:

An alias is a shortcut, an abbreviated command. Aliases are very useful for executing repetitive commands very quickly. It is often useful to store aliases in a script, like this:

#!/bin/bash
# This script creates aliases for annoyingly long and tedious cd commands.
alias gotoscripts="cd ~/Desktop/SourceCodes/Scripts";
alias gotoworkfiles="cd ~/Desktop/WorkFiles/Current";
alias gotoruby="cd ~/Desktop/SourceCodes/Ruby";

Typing the name of an alias executes whatever command its value is set to.

You can delete an alias by typing unalias "myalias".


Hack #5: CDPATH:

$CDPATH is an environment variable that sets the default file path for directory changes. If you enter cd and then the name of a directory in $CDPATH, bash automatically brings you to that directory. Here's the syntax:

export CDPATH=/usr/local

If you have a directory named bin in usr/local, typing cd bin will always bring you to /usr/local/bin, even if you are outside the containing directory.


Hack #6: sort

The command outputs the lines of a file sorted alphabetically. To sort a file, type sort filename at the prompt. You can then use I/O redirection to send the sorted output to another file.


Hack #7: cal

cal is a Unix program that prints a calendar of dates. The default is to display the current month, like this:

bash-2.3$ cal
   December 2009
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

There are several options for displaying the calendar in different formats. Here are two of them:

cal -m Jul - display the calendar for July of this year.

cal -y 2007 - display the calendar for all months in 2007.


Hack #8: Path to a command:

The type program shows the path to the binary for a command if it is a regular command, and prints "---- is a shell builtin" if the command is a builtin. This is useful for determining what to put in the she-bang line of a script:

bash-3.2$ type perl
/usr/bin/perl

The she-bang for perl scripts is then #!/usr/bin/perl.


Hack #9: Formatting the date:

There are several specifiers for formatting the date. These are used by typing a + and then a string in quotes with the specifiers in it. The shell will replace the format specifiers with the parts of the date.

Example:
bash-3.2$ date +"%m/%d/%y"
12/02/09

Options:
%m - month
%d - day
%y - abbreviated year
%D - month-day-year
%Y - full year
%a - abbrevated day name
%A - full day name
%b - abbrevated month name
%B - full month name


Hack #10: Word count:

wc counts the words, characters, or bytes in the given file. It's options are:

-c - number of bytes
-l - number of lines
-m - number of characters (including multibyte characters)
-w - number of words


Hack #11: Command history:

There are three useful hacks for using the command history of the terminal:

history - this command prints the command history.

HISTTIMEFORMAT is an environment variable that specifies the format for displaying the command history. There are a few formatting options for this variable.

export HISTTIMEFORMAT "%F %T"

This displays the date and time for each command in the history.

The command history can be searched by typing Ctrl+R and then typing a keyword.


Hack #12: Listing open files:

lsof lists all open files with information about each of them. This information includes process name, process ID, user, file descriptor, type (DIR or REG), device number, size, node number, and name of the file.


Summary of environment variables:

$CDPATH - the default file path
$COLUMNS - the width of the terminal window, in characters
$HISTTIMEFORMAT - the format for displaying the command history
$LINES - the height of the terminal window, in characters
$OLDPWD - previous directory
$PATH - a list of paths to the currently running program
$PWD - current directory
$RANDOM - random number
$SHELL - the path to the shell
$TERM - the name of the terminal
  • 3


#528097 Making a GUI in a Shell Script

Posted by DarkLordoftheMonkeys on 27 November 2009 - 07:24 AM

Check it out. Space background:

#!/bin/bash
# This script creates a neat looking space background for the terminal.

for((i=0; i < 1000; i++))
do
	printf "\033[40m";
	declare -i spaces=$RANDOM/1024;
	for((j=0; j < $spaces; j++))
	do
		printf " ";
	done
	colors=([0]="\033[31m" [1]="\033[32m" [2]="\033[33m" [3]="\033[34m" [4]="\033[35m" [5]="\033[36m" [6]="\033[37m");
	declare -i color=$RANDOM%7;
	declare -i plusordot=$RANDOM%4;
	if [ $plusordot -ne 0 ]
	then
		printf "${colors[$color]}.\033[0m";
	else
		printf "${colors[$color]}+\033[0m";
	fi
done
echo "";
unset i j spaces colors color plusordot;

Thanks for the idea.
  • 1


#527939 Vim commands cheatsheet

Posted by DarkLordoftheMonkeys on 26 November 2009 - 10:37 AM

The following is a nearly exhaustive list of all of the Vim keystrokes, plus the all of the most useful ex commands and options. There are over 300 commands and options in Vim, nearly twice as many as the original vi. Brackets around a part of an ex command mean that that part is optional. "Preceding" and "following" mean you type something else either before or after typing the command. The former is usually a movement command (in the case of the vi keystrokes), or an address (in the case of ex commands, without spaces). An ex command and whatever precedes or follows it are all typed in the same command, before hitting enter, like this:


:<preceding>command <following>


Here's a comprehensive list. It doesn't go into much detail for each command, so tell me if any of this is unclear and I'll add a note to explain it:

vi keystrokes:

a - enter insert mode, append after cursor
A - enter insert mode, append at the end of the line
b - move the cursor to the beginning of the previous word
B - move the cursor to the beginning of the previous word, ignoring punctuation
cc - change line
c - change the following
C - change line
dd - delete line
d - delete the following
D - delete line
e - move the cursor to the end of the current word, or next word, if it is already at the end
E - move the cursor to the end of the current or next word, ignoring punctuation
f - move the cursor to the next occurrence of the following character on the current line
F - move the cursor to the last occurrence of the following character on the current line
ga - print the ASCII value of the character under the cursor; if encoding is set to a different character set, show the value in that character set
gd - go to the first occurrence of the word under the cursor in the function
gD - go the the first occurrence of the word under the cursor in the file (same as gd except in curly bracket language source files)
ge - move the cursor to the end of the previous word
gE - move the cursor to the end of the previous word, ignoring punctuation
gf - edit the file whose name is under the cursor (if in an Explore, Sexplore, or Vexplore window)
gh - enter select mode
gm - move the cursor to the middle column on the screen
go - move the cursor to the preceding byte in the file buffer
gR- enter vreplace mode
G - move the cursor to the last line of the document, or to the line of the preceding number
h - move the cursor left one character
H - move to home position
i - enter insert mode, insert before the cursor
I - enter insert mode, insert at the beginning of the line
j - move the cursor down one line
J - join the current line with the next
k - move the cursor up one line
K - unused
l - move the cursor right one character
L - move the cursor to the last line on the screen
m - mark the current position with the following character
M - move the cursor to the middle line on the screen
n - repeat last search, go to the next occurrence of the pattern
N - repeat last search, go to the previous occurrence of the pattern
o - open a new line below the current one and enter insert mode
O - open a new line above the current one and enter insert mode
p - put the contents of the specified buffer after the cursor
P - put the contents of the specified buffer before the cursor
q - record keystrokes into the following buffer to be executed with @
q: - show ex command history
Q - exit vi mode and enter ex mode
r - replace a single character
R - enter replace mode, overwrite text
s - substitute a single character
S - substitute the current line
t - move the cursor forward to one character before the following character
T - move the cursor backward to one character after the following character
u - undo last change
U - undo all unwritten changes to the current line
v - enter visual mode 
V - enter visual line mode
w - move the cursor to the beginning of the next word
W - move the cursor to the beginning of the next word, ignoring punctuation
x - delete the character under the cursor
X - delete the character to the left of the cursor
yy - yank the current line
y - yank the following
Y - yank the current line
z<enter> - reposition the current line to the top of the screen
z. - reposition the current line to the middle of the screen
z- - reposition the current line to the bottom of the screen
ZQ - exit the current file unconditionally
ZZ - write any unsaved changes and quit
^A - increment the number under the cursor
^B - move up one screen
^C - kill vi
^D - move down half a screen
^E - move the screen down one line
^F - move down one screen
^G - display document information at the bottom of the screen
^H - move the cursor back one character
^I - unused
^J - move the cursor down one line
^K - unused
^L - redraw screen
^M - carriage return
^N - move the cursor down one line
^O - go back to the previous session
^P - move the cursor up one line
^Q - unused
^R - redo last change
^S - unused
^T - go back in the tag stack
^U - move up half a screen
^V - enter visual block mode
^Wb - go to bottom window
^Wf - split the current window and edit the file under the cursor (works in an Explore window)
^Wr - rotate windows
^Ws - split the current window horizontally
^Wt - go to top window
^Wv - split the current window vertically
^W^W - move to the next window
^W+ - increase the current window height the preceding number of lines
^W- - decrease the current window height the preceding number of lines
^W< - decrease the current window width the preceding number of columns
^W> - increase the current window width the preceding number of columns
^W_ - set the current window's height to the preceding number of lines
^W| - set the current window's width to the preceding number of columns
^X - decrement the number under the cursor
^Y - move the screen up one line
^Z - suspend vi
` - move the cursor to the following marker
`` - move the cursor back to the previous position
`. - move the cursor to the most recently modified character
~ - toggle the case of the character under the cursor
!! - print the output of the following Unix command to the file buffer
!}fmt - wrap the current line
@ - execute the key sequence stored in the following buffer
# - search for the last occurrence of the word under the cursor
$ - move the cursor to the end of the current line
% - move the cursor to the matching paretheses, brace or brackets
^ - move the cursor to the first nonblank character on the current line
& - repeat the last substitution (:s) command
* - search for the next occurrence of the word under the cursor
( - move the cursor to the beginning of the current of last sentence
) - move the cursor to the beginning of the next sentence
- - move the cursor to the beginning of the previous line
+ - move the cursor to the beginning of the next line
{ - move to the last blank line
} - move to the next blank line
[' - move the cursor to the last marker
]' - move the cursor to the next marker
[[ - move to the beginning of the current section
]] - move to the end of the current section
| - move to the column with the preceding number
; - repeat last f or F command
' - move the cursor to the line marked by the following marker
'' - move the cursor back to the line of the previous position
'. - move the cursor to the most recently modified line
" - do something with the following buffer
<< - tab the current line to the left
>> - tab the current line to the right
, - repeat the last f, F, t, or T command
. - repeat last editing command
/ - search forwards for the following pattern
? - search backwards for the following pattern
<esc> - go into command mode
<del> - same as h
<enter> - carriage return, same as ^M and +
<space> - same as l

ex commands:

:ab - show abbreviations, or abbreviate the following command
:ar[gs] - show the argument list (list of open files)
:arga[dd] - add a file to the argument list
:argd[elete] - delete a file from the argument list
:argdo - execute the following command on all files in the argument list
:arge[dit] - add a file to the argument list and edit it
:as[cii] - print the ASCII value of the character under the cursor
:bn[ext] - go to the next buffer in the argument list
:bp[revious] - go to the previous buffer in the argument list
:buffers - list all file buffers
:cm[ap] - map a command in command line mode
:co[py] - copy the preceding lines after the following line
:colo[rscheme] - load the following color scheme
:ctags - create a tags file mapping the subroutines of the following files
:d[elete] - delete the preceding lines
:display - list all character buffers
:Explore - show a list of files in your working directory or in the following directory in the current window
:file - show or set the name of the current file
:g/pattern/ - execute the following command on all lines containing pattern
:hardcopy - send the file to the printer
:help - view built in documentation for vi
:imap - map the following key for insert mode
:iunmap - unmap the following key for insert mode
:l - display the preceding lines with carriage returns marked by $ and tabs marked by ^I
:ls - list all file buffers
:m[ove] - move the preceding lines after the following line
:map - show mapped keys, or map the following key
:map! - map the following key for insert mode, same as :imap
:mapclear - clear all mappings
:marks - show all markers
:n - move to the next open file
:n! - move to the next open file without writing changes to this one
:nu[mber] - display the preceding lines and their numbers
:only - close all other windows
:p[rint] - display the preceding lines
:ptag - preview the following tag
:q[uit] - quit vi
:q[uit]! - quit vi without writing changes
:qa[ll] - close all windows
:qa[ll]! - close all windows without writing changes
:r[ead] - read the contents of the following file into the current file file buffer
:redr[aw] - redraw the screen, same as ^L
:rew - go to the first open file
:rew! - go to the first open file 
:s/old/new - replace old with new on the preceding lines
:sa[rgument] - split the current window and open the specified file in the argument list in the new window
:sal[l] - open a new window for each file in the argument list
:sav[eas] - save the current file under the following filename
:Sexplore - split the current window and list files in the new window
:sh[ell] - start a Unix subshell
:so[urce] - run the following ex script
:split - split the current window vertically, same as ^Ws
:sv[iew] - open a new window with the current file in read-only mode
:syntax on - turn on syntax highlighting
:syntax off - turn off syntax highlighting
:tag - jump to the file containing the following subroutine
:una - unabbreviate the following command
:unmap - unmap the following key
:unmap! - unmap the following key in insert mode, same as :iunmap
:v/pattern/ - execute the following command on all lines not containing pattern
:verbose - do the following command with the preceding verbose setting
:ve[rsion] - display the version number of vi
:Vexplore - split the current window vertically and list files in the new window
:vi[sual] - exit ex mode and go into vi mode
:vie[w] - edit a file in read-only mode
:w[rite] - write the file buffer to the current file
:wa[ll] - write all buffers
:wq - write any changes and quit
:wqa - write all buffers and then quit
:w! - exit read-only mode
:x - quit vi, writing any unsaved changes
:z - display the preceding lines with the following number of lines above or below (syntax: m,nz[+,-,=]q)
:& - repeat the last substitution command
:= - print the last line number


Settings:

:set - show settings
:set autoindent - automatically indent new lines
:set noautoindent - don't automatically indent new lines
:set compatible - make vim vi compatible
:set nocompatible - vim is not vi compatible
:set encoding=c - set the character encoding that vim uses
:set exrc - allow execution of vi scripts
:set noexrc - don't allow execution of vi scripts
:set flash - screen flashes when vi encounters and error
:set noflash - screen doesn't flash
:set ignorecase - searches are not case sensitive
:set noignorecase - searches are case sensitive
:set insertmode - inset mode is the default (never use this command)
:set noinsert mode - insert mode is not the default mode
:set isfname=<list> - set the characters included in filenames, used in regular expressions
:set isident=<list> - set identifier characters, used in regular expressions
:set iskeyword=<list> - set keyword characters, used in regular expressions
:set isprint=<list> - listed characters are displayed directly on the screen
:set lisp - enter lisp friendly mode
:set nolisp - exit lisp friendly mode
:set list - show whitespace characters
:set nolist - don't show whitespace characters
:set mesg - allow messages to be displayed while editing
:set nomesg - don't allow messages to be displayed while editing
:set number - number lines
:set nonumber - don't number lines
:set path=<list> - set the directories to be searched with the :find command or ^Wf
:set printfont=f - set the typeface that will be used when sending the file to the printer
:set readonly - set the current file(s) to read-only
:set noreadonly - current file(s) is/are writeable, same as :w!
:set scroll=n - ^U and ^D scroll the n lines up or down
:set sections=s - specify the nroff macros that will be used as section delimiters
:set shell - display the name of the shell, or specify which shell to use for the :sh and !! commands
:set shiftwidth=n - lines move n columns when shifted
:set showmatch - when the cursor is on a parenthesis or bracket, show the matching parethesis or bracket
:set noshowmatch - don't show the matching parenthesis or bracket
:set showmode - display mode at the bottom of the screen
:set noshowmode - don't display mode at the bottom of the screen
:set syntax=<file> - set the syntax to use for syntax highlighting
:set tabstop=n - tabs are shown as n spaces
:set term - display the terminal type
:set timeout - limit macros to 1 second
:set notimeout - don't limit macros to 1 second
:set verbose=n - if set to a number greater than 0, messages will be displayed for certain actions
:set warn - display an error message when you try to quit without writing changes
:set nowarn - don't display an error message
:set wrap - wrap lines
:set nowrap - don't wrap lines

  • 1


#525324 I/O in shell scripts

Posted by DarkLordoftheMonkeys on 14 November 2009 - 06:59 PM

This is my fourth and final tutorial on bash shell scripting. This tutorial will go over the I/O facilities of shell, namely echo, printf, read, and I/O redirection.


echo is used to direct a variable or text to the standard output of a shell script. The echo command has the following options:

-e interpret backslashed characters
-E don't interpret backslashed characters
-n omit the final newline


The read command is used to read user input into a shell script. It has the syntax:

read inputvariable;

The input the user enters is stored in the variable inputvariable.

Example:

echo "Enter a filename.";
read filename;
if [ ! -e $filename ]
then
	echo "$filename does not exist."
fi


printf is like echo, except that it allows for formatted text. It also allows for several escape sequences (not all of which work with echo, I've found).

The syntax for printf is:

printf "text %format-specifier text" formatted;

Some of the format specifiers used in printf are:

%d - Decimal integer
%.preE - Exponential (scientific) notation with pre precision.
%.pref - Floating point number to pre decimal places
%o - Octal value
%u - Unsigned decimal value
%x - Lowercase hexidecimal value
%X - uppercase hexidecimal value

Escape sequences:

\a Alert sound
\b Backspace
\c Omit final newline
\f Form feed
\n Newline
\t Tab character
\v Vertical tab
\0nnn 7-bit ASCII character for octal value nnn
\xHH 7-bit ASCII character for hexidecimal value HH


Example:

#!/bin/bash
printf "%.3E\n" 1500000;
printf "%.5f\n" 4.95;
printf "%x\n" 143;
printf "%u\n" -1;
printf "\x7e\n";

Outputs:

1.500E+06
4.95000
8f
18446744073709551615
~


I/O redirection:

There are six kinds of I/O redirection in Unix. Four of them are familiar to most Unix users: the >, <, >>, and | redirectors. Here is a rundown:

> Send the standard output of the preceding command to the following file, overwriting its original contents
< Get standard input for the preceding program from the following file
>> Append the standard output of the preceding command to the following file
| send the standard output of the preceding command to the following program, known as piping

There are two other types of I/O redirectors: here-documents and file descriptors.


Here-documents:

A here-document is when you write a macro script inside a shell script to control an interactive program, such as ed or ftp. The syntax is as follows:

program $input << EndOfScript;
program commands
EndOfScript;

Here, EndOfScript is the marker used to terminate the macro script.

As an example, here is a here-document script that uses ed to append text to a file:

#!/bin/bash
ed $1 << EndOfScript;
a
$2
.
w
q
EndOfScript


File descriptors:

A file descriptor is something that is generally only of concern to systems programmers as it controls the low level functioning of programs. Every time a Unix program is run, there are three file descriptors involved: standard output, standard input, and standard error. The first is called stdin and is given the number 0. The second is called stdout and is given the number 1. The third is called stderr and is given the number 2. The following script sends the standard output of cd / to the file file1.txt:

#!/bin/bash
echo "Text to add to file1.txt.";
cd / > ~/Desktop/file1.txt 1>&0;
echo $0;

That concludes my tutorial.
  • 2


#525239 Functions, operators, and options

Posted by DarkLordoftheMonkeys on 14 November 2009 - 11:12 AM

This is my third tutorial on bash shell scripting. Here I will be talking about some topics I did not go over previously, namely functions, arithmetic operators, typed variables, and how to parse command line options.


Functions:

A function, as most programmers with experience in other languages know, is a subroutine, a named procedure that can be called repeatedly throughout the script. I personally do not use functions much as my scripts are generally never more than 30 lines long, but programmers writing long scripts that carry out repetitive tasks might find them useful.

There are two syntaxes for declaring functions. The first is one that programmers in Javascript and PHP will probably be comfortable with:

function myfunc
{
	commands;
}

Note that in this case the inputs to the function are not specified. Instead, you use the variables $1, $2, etc. to access the inputs. You may also notice that the function declaration syntax differs from that of control structures in that it uses the curly bracket notation to define a block.

Here is the other notation for functions:

myfunc(inputs)
{
	commands;
}

This syntax may be more familiar to some programmers as it uses named variables instead of $1, $2, etc. as its inputs.

A function is called in much the same way that you would call a regular command:

funcname input1 input2;


Typed variables:

Variables can be given types using the declare or typeset builtins. There are four basic data types in Shell: integer, string, array, and function. There is no type for floating point numbers. An operation on two integer type variables that would normally result in a floating point number will be rounded off to an integer.

declare -a variable=value; - declares an array type
declare -f func; - declares the function named func as a variable. This is basically a string that is that function declaration.
declare -i variable=value; - declares an integer type
declare -r variable=value; - declares a read-only variable, or in other words, a constant


Operators in Shell:

Arithmetic operators:

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment by 1
-- Decrement by 1
** Exponent

Logical operators:

&& Logical AND
|| Logical OR
! Logical NOT

Assignment oparators:
(used without spaces)
= Equals
+= Increase by
-= Decrease by
*= Multiply by
/= Divide by
%= Modulus by
**= Raise to a power

Test operators:
(used with spaces)
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to

Bitwise operator:
<< Bitshift left
>> Bitshift right
<<= Bitshift left by
>>= Bitshift right by
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
^ Bitwise XOR


File test operators:

There are also several operators used to test conditions in files. These are used with control statements like if/then.

-a File exists
-d File is a directory
-e File exists
-f File is a regular file
-G You are in the group that owns file
-h File is a symbolic link
-N File was modified since last read
-O You own file
-r You have read permission
-s File exists and is not empty
-w You have write permission
-x You have execute permission
-nt Newer than
-ot Older than

Example:

This script deletes all empty files in a directory:

#!/bin/bash
for file in $(ls $1)
do
	if [ ! -s "$1/$file" ];
	then
		echo "$1/$file is empty.  Removing file.";
		if [ -d "$1/$file" ]
		then
			rmdir "$1/$file";
		elif [ -f "$1/$file" ]
		then
			rm "$1/$file";
		fi
	fi
done


Options:

Options (inputs beginning with a dash) can be parsed separately from the regular inputs by using the shift builtin. This makes it so that $1=$2, $2=$3, etc. Eventually, the script has its inputs in the right place and knows what options have been set. Here is a trivial script for doing this:

#!/bin/bash
# This script demonstrates how options and inputs
# to the script can be parsed separately.
opta="off";
optd="off";
optx="on";
for param in $*
do
	case $param in
		-*[ad]* )
			case $param in
				-*a* ) opta="on";;
			esac
			case $param in
				-*d* ) optd="on";;
			esac
			shift;;
		-* )
			echo "Error: $param is not an option.";
			printf "\a";
			shift;;
	esac
done
# Now the options have been stored for later use,
# while allowing the inputs to be processed.
filename=$1;
echo "You entered \"$filename\" as the filename.  Do you wish to proceed?";
if [ $opta = $optx ]
then
	echo "Option a is turned on.";
else
	echo "Option a is turned off.";
fi
if [ $optd = $optx ]
then
	echo "Option d is turned on.";
else
	echo "Option d is turned off.";
fi
# Now for some "garbage collection".
unset opta;
unset optd;
unset optx;

  • 1


#524876 Flow control in shell scripts

Posted by DarkLordoftheMonkeys on 13 November 2009 - 06:58 AM

This is my second tutorial on shell scripting. Before, I talked about shell variables. Now I will go into another important topic which is flow control.

There are six types of control structures in bash: if/then, for, case, while, until, and select.

The if/then statement is much like the equivalent statement in other languages, and is used to test if a condition is true:

if [ condition ]
then
	commands;
elif [ condition ]
then
	commands;
else
	commands;
fi

if [ condition ]; then literally means "If condition is true, then do what comes next." elif (the equivalent of else if) means "If not that but this...". Whatever comes after else is executed if none of the other conditions are true. fi (if spelled backwards) terminates the statement. Note that the elif and else sections are optional, but if, then, and fi are required.

The while loop repeats a series of commands for the period of time that a certain statement is true.

while [ condition ]
do
	commands;
done

The until loop is basically the opposite of a while loop. It repeats a series of commands as long as a condition is false.

until [ condition ]
do
	commands;
done

Example:

#!/bin/bash
declare -i int=0;
until [ $int -eq 10 ]
do
	echo $int;
	int++;
done

Outputs:
0
1
2
3
4
5
6
7
8
9

declare -i int initializes an integer variable int. -eq is the Shell notation for "equals" (for strings you use the = sign). ++ is the operator to increase an integer's value by 1.

The for loop is like the while loop, but rearranged. Instead of:

initialization;
while condition
do
	commands;
	increment;
done

You have:

for((initialization; condition; increment))
do
	commands;
done

This will be familiar to programmers with experience in languages like C, C++, and Java. Note, however, that this loop uses double parenthesis. This is to provide extra protection for the expressions inside.

The for loop can also loop through variables in a list, repeating an action for each list item. For instance:

#!/bin/bash
for param in $*
do
	echo $param;
done

If this script is called like this:

$ source forscript "snake" "camel"

It will output:

snake
camel

The case statement is similar to the switch-case statement of C-like languages, but with one major difference. Instead of testing the value of a variable, it tests to see whether a string matches a certain pattern. In Shell, these patterns are known as globs.

case $file in
	*.html )
		echo "HTML file";;
	*.css )
		echo "CSS stylesheet";;
	*.js )
		echo "Javascript script";;
	*
		echo "Another type of file";;
esac

Here, *.html, *.css, etc. are the globs. The * means any string of characters, so *.html would match either browser-stats.html or OS-stats.html.

The last flow control statement is the select statement, which creates a menu of items in a list.

#!/bin/bash
select file in *
do
	echo $file;
	break;
done

This script will create a menu of files in the current directory. When the user selects one of the menu items, the name of the file will be output. break is necessary because it prevents the statement from looping indefinitely.
  • 1


#524685 Unix shell variables

Posted by DarkLordoftheMonkeys on 12 November 2009 - 07:15 AM

Unix relies heavily on strings. When a variable is declared it is usually assumed to be a string. There is separate notation for declaring numbers and arrays. If a string is operated on as a number, it will be automatically converted to a number so that the operation may be done. This is because Shell uses weak typing. Unlike other languages, shell variables declared in a script remain after the script completes. To prevent this, type:

unset var;

at the end of the script.


Notation for variables:

Unix variables are set using the syntax:
var=value;

They are accessed with a $ sign:
echo $var;

Variables can also be written using curly bracket notation:
${variable}
This is commonly used for string operators.


Special variables:
$1, $2, $3, etc. - the first, second, third, etc. inputs to the script or function
$0 - the first word typed when calling the script. On my terminal this is always bash.
$# - the number of inputs to the script
$* - a list of all inputs to the script or function
$@ - a list of all inputs to the script or function, individually quoted
$? - the exit status or return value of the last command or function

Environment variables:
These variables are built into the shell:
$LINES - the number of lines in the terminal window
$COLUMNS - the width of the terminal window
$TERM - the name of the terminal
$SHELL - the name of the shell
$PATH - a list of all paths to the current application
$RANDOM - a random number

String operators:
${string:x:y} - substring of string starting at index x and continuing for y characters.
${#string} - the length of string
${string/pattern/new} - replace the first instance of pattern in string with new
${string//pattern/new} - replace all instances of pattern in string with new

Declaring an array:


array[0]=value1;
array[1]=value2;
etc.

array=([0]=value1 [1]=value2 [2]=value3);


Most of the string operators can be applied to arrays. When they are, they operate on the elements of the array, rather than individual characters.
  • 1


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download