Bash

From AdminWiki

(Difference between revisions)
Jump to: navigation, search
m (More Documentation)
Line 1: Line 1:
-
= Bash =
+
= Bash Handling =
 +
== Job Control ==
 +
 
 +
== Interactive ==
 +
=== Key Combinations ===
 +
==== Ctrl - r ====
 +
Ctrl - r allows you to recall-by-typing from the bash history.
 +
 
 +
==== Ctrl - o ====
 +
Ctrl - o alone does nothing, but while pressed when on the line of a with Ctrl - r recalled history entry, it will execute the bash history entry and when it exists, put the next command in history into the shell buffer.
 +
 
 +
Example:
 +
philip@dinky:~$ vi test.c
 +
philip@dinky:~$ make test
 +
cc    test.c  -o test
 +
philip@dinky:~$ ./test
 +
Hello World!
 +
philip@dinky:~$ (press Ctrl-r vi here)
 +
(reverse-i-search)`vi': vi test.c (press enter here)
 +
philip@dinky:~$ vi test.c
 +
philip@dinky:~$ (press Ctrl-r mak here)
 +
(reverse-i-search)`mak': make test (press Ctrl-o here)
 +
philip@dinky:~$ make test
 +
cc    test.c  -o test
 +
philip@dinky:~$ ./test
 +
 
 +
= Bash Programming =
Bash is the most common shell form on linux, and most other unix systems.
Bash is the most common shell form on linux, and most other unix systems.

Revision as of 12:16, 10 April 2009

Contents

Bash Handling

Job Control

Interactive

Key Combinations

Ctrl - r

Ctrl - r allows you to recall-by-typing from the bash history.

Ctrl - o

Ctrl - o alone does nothing, but while pressed when on the line of a with Ctrl - r recalled history entry, it will execute the bash history entry and when it exists, put the next command in history into the shell buffer.

Example:

philip@dinky:~$ vi test.c
philip@dinky:~$ make test
cc     test.c   -o test
philip@dinky:~$ ./test
Hello World!
philip@dinky:~$ (press Ctrl-r vi here)
(reverse-i-search)`vi': vi test.c (press enter here)
philip@dinky:~$ vi test.c
philip@dinky:~$ (press Ctrl-r mak here)
(reverse-i-search)`mak': make test (press Ctrl-o here)
philip@dinky:~$ make test
cc     test.c   -o test
philip@dinky:~$ ./test

Bash Programming

Bash is the most common shell form on linux, and most other unix systems.

Just a small reminder. Before you start writing a long bash script, think if you can do this in perl.


Error and Output Redirect

to redirect output and error

 ls -l 1>normal_out 2>error_out

if you want to have both in one file

 ls -l 1>normal_out 2>&1

Questions and Solutions

Below some examples for bash problems that might come up

Arrays in Bash:

 foo[0]=1;
 foo[1]=2;
 foo[2]=3;
 # loop
 for (( i=0; i<${#foo[@]}; i++ ));
 do
     echo ${foo[$i]};
 done;
 # more like a foreach
 for i in ${foo[@]};
 do
     echo $i;
 done;

Variable Variables

 foobar=5;
 bar="foobar";
 echo $foobar;
 echo $bar;
 echo ${!bar};

Variable Variables in Arrays

 foo[0]=1;
 foo[1]=2;
 foo[2]=3;
 foo_tcp[0]="AT";
 foo_upd[0]="AU";
 foo_tcp[1]="BT";
 foo_upd[1]="BU";
 foo_tcp[2]="CT";
 foo_upd[2]="CU";
 # loop
 for (( i=0; i<${#foo[@]}; i++ ));
 do
     echo ${foo[$i]};
     for k in upd tcp;
     do
         data=foo_${k}[$i];
         echo ${!data};
     done;
 done;

More Documentation

The best advanced guide for bash scripting: http://www.tldp.org/LDP/abs/html/

Personal tools