Bash Shell Scripting
Shell is a user program or its environment provided for user interaction, it is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file.
Shell is not part of system kernel, but uses the system kernel
to
execute programs, create files etc.
Below is a small script to get you started with writing shell scripts.
We are using Nano within the Bourne shell window.
Some Linux distributions configure Dash as the default shell, we
will be using Bash.
In the terminal type the following:
sudo dpkg-reconfigure bash
To check your shell environment type:
echo $SHELL
The output should be:
/bin/bash
A quick example of a shell script (all done within the command line) is as follows.
nano user.sh
This brings up the Nano editor in your terminal window.
Type the following into Nano:
clear
echo "Hello $USER"
echo "Today is";date
echo "Number of user login : " ; who | wc -l
echo "Calendar"
cal
echo "You are using $OSTYPE"
exit 0
Save the script with ctrl+o <Enter>
To exit Nano, do ctrl+x
Back in the terminal type in:
chmod +x user.sh
This makes the file executable to the user.
Then...
./user.sh
Here is your first shell script ;-)
If you have configured your path and /bin folder, then copy
the file
into /bin and execute with:
user
(notice we do not need either ./ nor .sh once the path is
configured
to your scripts).
To discover which shells you have on your system, type in the
command
terminal:
cat /etc/shells
On your system, the output should be quite limited, we can therefore do an apt-get install:
apt-get install csh ksh zsh
Then cat /etc/shells once again:
The output will then be something similar to below:
/bin/sh
/bin/bash
/sbin/nologin
/bin/zsh
/bin/tcsh
/bin/csh
/bin/ksh
Bash is the main shell to
work your scripts with, although csh, ksh and tcsh can be valuable
also.
Bash is the Bourne Again
SHell,
developed by Brian Fox and Chet
Ramey,
this is the most common form of shell on a Linux system.
csh is literally the C
shell, developed by Bill Joy at the University of California (for BSD).
The C shell has syntax and usage that is closely related to the C
programming language.
ksh is the Korn shell,
developed by David Korn at AT&T Bell Labs.
tcsh is an enhanced, but entirely compatible version
of csh.
Linuxfx Computing have cobbled together a list for use within
the Bash environment, it is a non-exhaustive list, but it will be
useful to know some of the commands to input. Some of the following
commands may possibly require you to download applications related to
the information you require.
| COMMAND |
DESCRIPTION |
| passwd |
Changes user
password |
| pwd |
Prints
current directory |
| cd |
changes directory |
| ls |
List
files in a directory |
| wildcards |
* matches any
number of
characters |
| file |
Prints
the type of file |
| cat |
Displays the
contents of a file |
| pr |
Displays
the contents of a file |
| pg or page |
Displays the
contents of a file
one page at a time |
| more |
Displays
the contents of a file one page at a time |
| clear |
Clears the screen |
| cp
or copy |
Copies
a file |
| chown |
Changes ownership
of a file |
| chgrp |
Changes
the group of a file |
| chmod |
Changes the modes,
permissions |
| rm |
Removes
a file from the system |
| mv |
Renames a file |
| mkdir |
Creates
a new directory |
| rmdir |
Removes a directory |
| grep |
Pattern
matching |
| egrep |
A 'grep' command
for extended
regular expressions |
| find |
Locates
files and directories |
| >> |
Appends to the end
of a file |
| > |
Redirects,
creates, or overwrites a file |
| | |
Pipe - Strings
commands together |
| |
| |
Logical
OR - command |
| & |
Executes in the
background |
| && |
Logical
AND - command |
| date |
Displays the system
time and date |
| echo |
Writes
strings to standard output |
| sleep |
Halts execution for
the
specified number of seconds |
| wc |
Counts
the number of words, lines and characters in a file |
| head |
View the top of a
file |
| tail |
View
the end of a file |
| diff |
Compares two files |
| sdiff |
Compares
two files side by side |
| spell |
Spell checker |
| lp,
lpr, eng, qprt |
Prints
a file |
| lpstat |
Status of system
print queues |
| enable |
Enable
or start a print queue |
| disable |
Disables or stops a
print queue |
| cal |
Displays
a calendar |
| who |
Displays
information about
system users |
| w |
Extended
'who' command |
| whoami |
Displays $LOGNAME
or $USER
environment parameters |
| who
am i |
Displays
login name, terminal, login date / time, where logged in |
| f, finger |
logged in users,
including the
user's .plan and .project |
| talk |
Enables
split-screen converation between two users |
| write |
Displays a message
on user's
screen |
| wall |
Displays
(broadcasts) a message to all users |
| rwall |
Displays
(broadcasts) a message
to all users on a remote host |
| rsh
or remsh |
Executes
a command or login on a remote host |
| df |
Displays filesystem
statistics |
| ps |
Displays
information on currently running processes |
| netstat -a |
Shows the network
status |
| vmstat |
Shows
virtual memory status |
| iostat |
Shows input /
output status |
| uname -a |
Displays
operating system name and machine information |
| sar |
Reports system
activity |
| basename |
Displays
base filename of a string parameter |
| man |
Displays current
online
reference manual |
| su |
Super
user, switches to another user |
| cut |
Writes out selected
characters |
| awk |
programming
language to parse characters |
| sed |
programming
language for
character substitution |
| vi |
Invokes
the 'Vi' editor |
| emacs |
Invokes the 'Emacs'
editor |
| nano
*recommended |
Invokes
the 'Nano' editor |
| SYMBOL
COMMANDS |
|
| COMMAND |
DESCRIPTION |
| ( ) |
Runs the enclosed
command in a
sub-shell |
| ((
)) |
Evaluates
and assigns value to a variable and does math in a shell |
| $ (( )) |
Evaluates the
enclosed expression |
| [
] |
The
same as the 'test' command |
| < > |
Used for string
comparison |
| $
(
) |
Command
substitution |
| 'command' |
Command substitution |
