The Bourne Again Shell
BASH (Bourne Again Shell) - It is a command interpreter and a high level programming language.
It was based on the original Bourne shell (sh) written by Steve Bourne of AT&T
(Portable Operating System Interface for UNIX) An IEEE 1003.1 standard that defines the language interface between application programs and the LINIX operating system. Adherence to the standard ensures compatibility when programs are moved from one UNIX computer to another. POSIX is primarily composed of features from UNIX System V and BSD UNIX.
A shell script is a file that contains commands that can be executed by the shell. The shell can be any command or group of commands that enter at the shell prompt.
Control structure (alters the execution of the flow of the program)
Make the file executable
Then to run it use use full path to indicate that program is in current dir. I.E. ./myscript -/myscript
COMMAND SEPARATION AND GROUPING
; - command separator
\ - if command is long you can use \ to continue command in the next line.
& - run on background task symbol
Combination of them
$ progl | prog2 | prog3 &
A command can send messages to standard error so it does not get mixed up with the information that it is sent by the command to the standard output.
>short for 1 > (redirect standard output)
<short for <0
2> redirects standard error
2>& 1 Declares file descriptor 2 to be a duplicate of file descriptor 1
I.E. cat x y 1> outputl 2> output2
If y does not exists the content of output2 will be the error message
$cat x y 1> hold 2>&1
the standard output and standard error are both redirected to hold
A job is a command pipeline. You can create several jobs concurrently without interfering your work.
You can move commands from foreground to background and vice versa.
$find /usr -name ace -print > findout &
(it creates a job # =second job)
$fg %2 (brings jog to foreground)
Suspending a job
CTRL-Z or CTRL-Y will suspend a job
$bg (it will resume its execution)
$ (sleep 5; cat > mytest) & (sleep - delay for a specified amount of time)
+ Stopped (tty input) (sleep 5; cat > mytest)
(sleep 5; cat > mytest)
I had to bring this job forward to complete it since the job attempted to read from the terminal
DIRECTORY STACK MANIPULATION
In bash you can store a list of directories that you are working with. This is referred to as a stack.
$dirs (it will display the stack)
$pushd ../ (will place on the stack my previous dir)
$pushd /home (it will place on the stack the dir /home)
$pushd /home/ftp (it will place on the stach the dir /ftp)
When you use the pushd dirname, the dirname will be the active(current) directory
When you use pushd by itself the next directory on the stack it will go to the top of the stack
$pushd +2 (+0,+1… first dir on the stack represented by 0)
$popd (it will remove the top dir from the stack)
$popd +1 (it will remove the second dirs from the stack)
$ps gives a snapshot of the current processes.
The process structure is hierarchical, it has parents, children, and even a root. A parent process forks a child process, which in turn can fork other processes.
[wmorales@rc33uxas01 wmorales]$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
100 S 501 8772 8771 0 76 0 - 434 wait4 pts/2 00:00:00 bash
000 R 501 8919 8772 0 79 0 - 617 - pts/2 00:00:00 ps
F- Flags associated with the process
UID - User id number
PID - process identification number
PPID - The process identification number of the parent process
PRI - Priority
WCHAN - wait channel. If process is wating for some even this column gives the address of the kernel functin that cuased the process to wait.
SZ - Size
TTY - Terminal name controlling the process
TIME - The number of minutes and second the process has been running
CMD - Command
Executing a command
Builtins – Programs that are built into shell
Calling a shell script
Giving a shell command, the shell forks (creates a child process that differs from the parent process) creating a duplicate of the shell process (a subshell). The new process tries to exec the command.
$bash whoison or $sh whoison
Specifying a shell
# without the != comment
If bash is the login shell
if you get into bash just typing bash it will read
When exiting the shell reads the .bash_logout in your home directory.