Linux Bash Programming
1. Introduction
Shell is a program (command interpreter) that bridge the user's operating system in this case the kernel (the core operating system), generally provides a shell prompt as the user interface, where users type in commands in the form desired internal command shell (internal command), execute a command or program file (external command), otherwise the shell will allow users to compile a set of commands on one or more files to be executed as a program.
Kinds of shell
Operating systems such as family unix linux until now supplemented by many of the shell with a set of commands so much, allowing users the choice of which shell is best to help finish the job, or can be moved from one shell to another shell with easy, some shell in linux, among others:
- Bourne shell (sh),
- C shell (csh),
- Korn shell (ksh),
- Bourne again shell (bash),
- etc..
Each of shell has advantages and disadvantages that may be more based on user needs is increasing every day, for this documentation is the shell used by the GNU bash shell, which is the development of the Bourne shell and taking a few features (features) from the C shell and Korn shell, Bash shell is a shell that is used quite a lot of linux users because of the ease and the many facilities that provided. Bash command shell version that I use is 2:04
[fajar@linux$]echo $BASH_VERSION
2:04:12 bash (1)-release
Perhaps when you read this documentation the latest version of bash was released with the addition of other features.
2. Programming Shell
It's arrange or classify some shell commands (atupun internal external command) into a collection of commands that perform specific tasks according to their constituent goals. Advantages of linux shell than other operating systems is that in linux shell allows us to construct a series of commands such as programming language (language interpreter), to process I / O, selecting conditions, loops, create function, etc.. are process which is generally performed by a programming language, so with a shell in linux we can make a program like programming language, for user shell programming unix or linux shell script call.
3. Basic Needs
Before studying Bash shell programming in linux you should already know and use the commands - the basic commands that either the internal shell command that has been provided as well as external shell command or utility, such as
- cd, pwd, times, aka, umask, exit, logout, fg, bg, ls, mkdir, rmdir, mv, cp, rm, clear, ...
- utilities such as paint, cut, paste, chmod, lpr, ...
- redirection (how to send output to a file or receive input from a file), use the redirect operators>,>>, <, <<, for example:
- ls> data
ls results sent to the data file, if no files will be created, but otherwise its contents will be overwritten. - ls>> data
almost the same, the difference if the file already exists then the contents will be added at the end of the file. - cat
data files used as input by the command cat - pipe (the output of one command to another command inputs), operators: |, for example:
- ls-l | sort-s
outputs the command ls-l (long) to input the command sort-s (in descending sort), it's better use ls-l-r course - ls-l | sort-s | more
- databaru
- Wildcard characters *,?, [], For example:
- ls i *
show all files that start with i - ls i? i
show files that begin with i, then any single character, and ending with i - ls [ab] *
files show that begins with one character a or b
An initial step should check your current shell, use the ps (process status report)
[fajar@ linux $] ps
PID TTY TIME CMD
219 tty1 00:00:00 bash
301 tty1 00:00:00 ps
bash is the shell is active in my system, if you are a different example disystem csh or ksh change with change command shell
[fajar@ linux $] chsh
Password:
New shell [/ bin / csh]: / bin / bash
Shell changed
or by typing bash
[fajar@ linux $] bash
now let you type the following command at the shell prompt
echo "My first shell script in linux"
echo "My first shell script in linux"
My first shell script in linux
string enclosed by double quotation marks (double quoted) will be displayed on your screen, echo is a statement (commands) built-in bash that serves display information to standard output that defaults to the screen. if you want to repeat the process, you will type the command again, but with enough history facilities use the arrow keys we can repeat the command, what if a collection of commands that quite a lot, certainly in our history facility will also repeat the inconvenience the desired command much less if after a while maybe the orders had been affected by other commands because history has a specified storage capacity. for it should they will be commands saved to a file which we can call at any time desired.
try to follow the steps:
- Sign in to your editor, if using vi, pico, emacs, etc. ...
- type the following command
- #! / bin / bash
- echo "Hello, apa khabar"
- save with the test file name
- change the test file permissions using chmod
- [fajar@ linux $] chmod 755 test
- run
- [fajar@ linux $]. / test
whenever you want live call executes the test file, if desired set your working directory so the path listed on the search type the following command
PATH = $ PATH:.
after that, the above script can be run in a way
[fajar@ linux $] test
Hello, apa khabar
sign #! in / bin / bash in the test script is translated into commands to execute the kernel is included in the path of this program directory bash in / bin, in fact, without these lines include you still can execute bash script, with notes bash is a shell active. atau dengan mengetikkan bash pada prompt shell. or by typing bash at the shell prompt.
[fajar@ linux $] bash tests
certainly less efficient this way, include the path to the bash program early in the script and then modify permissions on files so that you can execusi is the most efficient way.
Now let us make a shell script that displays the following information:
- Time system
- Info about you
- the number of users who are logged in the system
example:
#! / bin / bash
# myinfo
# clean the screen
clear
# display the information
echo-n "Time system:"; date
echo-n "You:"; whoami
echo-n "A lot of users:"; who | wc-l
before running do not forget to change the permissions on files that can myinfo executed by you
[fajar@ linux $] chmod 755 myinfo
[fajar@ linux $]. / myinfo
Time system: Sat Nov 25 22:57:15 BORT 2001
You: fajar
Many users: 2
course layout above will be adjusted to the system you use echo statement with the option-n will make the cursor position to move into a new line by default because the echo statement will end the process of printing to standard output with the new line character (newline), you can try without use the-n option, and see the difference. opsi lain yang dapat digunakan adalah -e (enable), allows the use of backslash characters or character sequences as in C or perl, for example:
echo-e "\ abunyikan bell"
if executed bell sounds, the information on the options echo statement and backslash character can be seen via the man at the shell prompt.
[fajar@linux$]man echo

0 comments:
Posting Komentar
Kalo sudah baca, silahkan tulis komentar, kesan atau pesan...