This document discusses practicing with the Bash shell in Linux. It covers the history feature of Bash, creating Bash shell scripts, and using the vi editor. The objectives are to understand the shell, use Bash history, configure scripts to run automatically, and create/execute scripts in vi. The document provides basics on shells, profiles, history, job control, and vi commands before describing experiments on Bash profiles and history features.
2. https://github.com/syaifulahdan/ospractice|Operating System Practice |2 to 52
A. Objectives
1. Understanding the shell on Linux operating system.
2. Using feature history in Bash Shell.
3. Change the history feature in Bash Shell.
4. the shell prompt.
5. Configure Bash Shell to run scripts automatically.
6. Create and execute simple shell scripts via vi editor.
7. Understand the job control.
8. Understand the stack.
9. Using aliases
4. https://github.com/syaifulahdan/ospractice|Operating System Practice |4 to 52
1. SHELL
Shell is a Command executive, meaning the program awaits
the user's instructions, checks the syntax of the given
instruction, then executes the command.
Shell is marked with a prompt. For user use prompt $ and
for superuser use promp #.
shell type:
/bin/sh
Developed by UNIX Berkeley known as C-Shell
/bin/csh
Bourne shell, designed by Steve Bourne of AT & T
/bin/bash
Compatible with Bourne Shell and also adapting Korn-Shell's ability.
The fundamental difference between Shell is resolved almost non-
existent, except in programming and editing facilities.
5. https://github.com/syaifulahdan/ospractice|Operating System Practice |5 to 52
1. SHELL
Shell is a Command executive, meaning the program awaits
the user's instructions, checks the syntax of the given
instruction, then executes the command.
Shell is marked with a prompt. For user use prompt $ and
for superuser use promp #.
shell type:
/bin/sh
Developed by UNIX Berkeley known as C-Shell
/bin/csh
Bourne shell, designed by Steve Bourne of AT & T
/bin/bash
Compatible with Bourne Shell and also adapting Korn-Shell's ability.
The fundamental difference between Shell is resolved almost non-
existent, except in programming and editing facilities.
6. https://github.com/syaifulahdan/ospractice|Operating System Practice |6 to 52
2. PROFILE
At login time, the program will run some program that is:
1. /etc/profile
It contains a shell script that applies to all Linux users.
2. Profile for each user
In the home directory, the login will first check the .bash_profile file.
If no, then the .bash_login file will be searched. If .bash_login does not
exist, then look for file named .profile
3. .bashrc
This file will be executed for switching from one shell to another through
su.
4. .bash_logout
At logout, bash will search for file .bash_logout. When present, the file
will be executed before logout
7. https://github.com/syaifulahdan/ospractice|Operating System Practice |7 to 52
The contents of / etc / profile:
# System wide environment and startup programs
# Functions and aliases go in /etc/bashrc
PATH=”$PATH:/usr/X11R6/bin”
PS1=”[u@h W]$ “
umask 022
USER=’id –un’
LOGNAME=$USER
MAIL=”/var/spool/mail/$USER”
HOSTNAME=’/bi n/hostname’
HISTSIZE=1000
HISTFILESIZE=1000
Export PATH PS1 HOSTNAME HISTSIZE HISTFILESIZE USER
LOGNAME MAIL
8. https://github.com/syaifulahdan/ospractice|Operating System Practice |8 to 52
PATH merupakan daftar nama direktori. Bila sebuah instruksi
diberikan dari prompt shell, maka instruksi tersebut akan
dicari pada daftar tersebut.
PS1 adalah prompt dimana
u = Nama User
h = Nama Host
W = Nama working direktory
9. https://github.com/syaifulahdan/ospractice|Operating System Practice |9 to 52
3. HISTORY
History is adapted from C-Shell, which is a record of all
the instructions that have so far been done. These
records can be viewed as history, then can be re-
selected, edited and executed. History allows the user to
re-edit complex and lengthy instructions, especially
when errors occur in the writing of instructions and
parameters.
^P (Ctrl-P) see previous instructions
^N (Ctrl-N see the next instruction
!! execution of previous instructions
!! –3 3 previous instructions will be repeated
!!88 repeat instruction no 88
10. https://github.com/syaifulahdan/ospractice|Operating System Practice |10 to 52
4. BASH-SCRIPT
Bash-script is a file that contains a collection of executable
programs. For bash script execution use .
before the bash-script file which means shell and sign
execution ./ means the bash-script file is in the actual
directory.
11. https://github.com/syaifulahdan/ospractice|Operating System Practice |11 to 52
5. JOB CONTROL
Job is a program execution given to the kernel. A Job is
deemed complete, when the execution of the program ends.
Execution Job is the same as the execution of the program,
both Background process and Foreground process.
12. https://github.com/syaifulahdan/ospractice|Operating System Practice |12 to 52
5. EDITOR vi
Vi is a full screen editor, meaning the editor can take
advantage of the full-screen facility. Vi has 2 pieces mode,
namely:
Command line
The vi editor interprets the input as an instruction to be
executed by the editor, examples such as searching text,
changing text automatically and others.
Editing
The vi editor interprets the output as text to be inserted into
the buffer editor. At the bottom of the screen will display the
text "INSERTING".
13. https://github.com/syaifulahdan/ospractice|Operating System Practice |13 to 52
At the beginning of vi run, the program enters command mode. With
pressing the "i" button will then enter editing. To return to
command mode, press the Esc key.
The keys of the vi editor text can be seen in the table as follows:
H Move cursor to left one character
J Move the cursor to the right of one character
K Pinda h kursor ke atas
L Move the cursor down
O Insert text (one line after cursor position) To exit the 5 insert lock
models next to this and
activate other keys, then we
must press the Esc key first
I Insert text (to the left of the cursor position)
A Insert text (to the right of the cursor position)
I (Shift i) Insert text (in the starting position of the line)
A (Shift a) Insert text (at the end of the line)
14. https://github.com/syaifulahdan/ospractice|Operating System Practice |14 to 52
X Deleting 1 letter (to the right of the cursor position)
Dw Delete 1 word (to the right of the cursor position)
Dd Removing 1 row (to the right of cursor position)
Yy Copying 1 line
2yy Copying 2 lines
P (Paste) Displays a sentence line that has been copied with yy key.
Cw Replace 1 word that has been written to the right of the cursor position with another word
Cc Replace 1 sentence line that has been written to the right of the cursor position with
another sentence
ctrl- b Reverse one screen
ctrl- f Forward one screen
ctrl- d Forward half screen
B Move the cursor to the left of one word
W Move the cursor to the right of one word
^ Go to the beginning of the line
$ Go to the end of the line
U Cancel the last command
U Undoes all text changes on the line where the cursor is located
:! Quit temporarily from vi editor and execute another command
15. https://github.com/syaifulahdan/ospractice|Operating System Practice |15 to 52
:wq Write and quite, save the file and exit
:q! Exit vi without saving
:se all Displays all set status options
:se nu Displays the line number on the left of the screen
/string Looking for strings forward
?string Looking for strings toward the back
N Forward the search for the same direction
N Continue searching for different directions
17. https://github.com/syaifulahdan/ospractice|Operating System Practice |17 to 52
1 Login as user.
2 Open the Console Terminal and do the experiments
below and then analyze the results of the
experiment.
3 Conduct the experiments below and then analyze
the experimental results.
4 Complete the practice questions.
19. https://github.com/syaifulahdan/ospractice|Operating System Practice |19 to 52
Experiment 1 : Profile
1. The .bash_profile file runs on the user's home directory
login. The .bash_profile file is a hidden file, so to see it use
option a on the ls instruction.
$ ls –a
$ more .bash_profile
2. The .bash_logout file will be executed shortly before
logout, functioning as a house clearing job, which means to
clear everything, for example deleting temporary file or
other job. View the .bash_logout file with instructions
$ cat .bash_logout
20. https://github.com/syaifulahdan/ospractice|Operating System Practice |20 to 52
Experiment 2 : Using Feature History Bash
1. Bash shell keeps the "history" command used before.
You can do history in some way. The easiest way is to use
the Up Arrow, then the previous command will be
displayed.
2. Next, give the Bash shell some commands to remember.
Enter the following command and press Enter on each line.
$ cd
$ ls –l /etc
$ ls –l
$ whoami
$ who
21. https://github.com/syaifulahdan/ospractice|Operating System Practice |21 to 52
3. To check if this command is added to the history, it can
use the history command to see all the commands ever
inserted..
$ history
4. You can select the previous command by using Up
Arrow, but this is not efficient for increasing orders.
A simple way of using numbers in history commands or
searching for them. To select and execute commands by
number, enter the key! followed by the command number..
$ !<Nomor Perintah> Contoh : !780
22. https://github.com/syaifulahdan/ospractice|Operating System Practice |22 to 52
3. To check if this command is added to the history, it can
use the history command to see all the commands ever
inserted..
$ history
4. You can select the previous command by using Up
Arrow, but this is not efficient for increasing orders.
A simple way of using numbers in history commands or
searching for them. To select and execute commands by
number, enter the key! followed by the command number..
$ !<Nomor Perintah> Contoh : !780
23. https://github.com/syaifulahdan/ospractice|Operating System Practice |23 to 52
5. You can search for commands by including the desired
commands. For example !? Etc ?! will execute the
previously used ls l /etc command.
$ !?etc?
6. Then use the history command, it will show the ls
l /etc command instead of !? Etc?.
$ history
7. If the string is not found in the history command there
will be an error message.
$ !?wombat99?
24. https://github.com/syaifulahdan/ospractice|Operating System Practice |24 to 52
8. If typed !Who then it is executed is the who command.
But if you type !Whoa then the whoami command is
executed.
$ !who
$ !whoa
9. You can replace string in history command, especially on long
command.
- For example type cat /bin /bash | strings | grep shell | less and
press Enter. It will show all the strings in the /bin /bash file
containing the word "shell". To exit press q.
- If you want to display the word "alias", then you do not have to
type long command again, but just type ^shell^alias^ and press
Enter it will replace the word "shell" with "alias".
$ cat /bin/bash | strings | grep shell | less
$ ^shell^alias^
25. https://github.com/syaifulahdan/ospractice|Operating System Practice |25 to 52
Experiment 3 : Change Feature
History Bash
1. Bash shell will keep the history command even
after logging out and logging in again.
The .bash_history file stores the history file
contained in the home directory.
$ cd
26. https://github.com/syaifulahdan/ospractice|Operating System Practice |26 to 52
2. View a few lines in the .bash_history file by typing
"tail .bash_history" and press Enter.
$ tail .bash_history
3. Type history and press Enter. Then it will be seen
that the last line is history command and the previous
line is tail .bash_history.
The history command is up to date, as it is stored on
system memory.
$ history
28. https://github.com/syaifulahdan/ospractice|Operating System Practice |28 to 52
4. Type the following command.
5. Log out and log in again as the same user.
Type history and press Enter, Then the command
echo 'This my command' will be on the last line.
See the .bash_history file, then the command will be
in the .bash_history file.
$ echo 'This is my command'
$ history
$ tail .bash_history
29. https://github.com/syaifulahdan/ospractice|Operating System Practice |29 to 52
7. To see how many of the history commands that
exist in the following file and output that comes out
similar below
$ history|less
6. Type history | less to see the last history
command on screen, Press spacebar to see more files.
To exit press q
$ wc –l .bash_history
1000 .bash_history
30. https://github.com/syaifulahdan/ospractice|Operating System Practice |30 to 52
8. The output shows that 1000 history commands are
stored in the history file.
To see the range (limit) history command used
HISTSIZE variable. To see the history range type as
follows
$ set|grep HISTSIZE
9. If you want to enlarge the file history range, change
the variable
HISTSIZE on a startup script called .bashrc in the
home directory.
$ echo ‘HISTSIZE=5000’ >> .bashrc
31. https://github.com/syaifulahdan/ospractice|Operating System Practice |31 to 52
10. Log out and log in again as the same user. View
HISTSIZE variable changes.
$ set|grep HISTSIZE
11. Type a history command several times, then this
command will be stored in BASH history even though
the command is typed the same.
12. You can configure BASH to not add commands to
history if the commands you type are the same as
before.
This is done by adding the HISTCONTROL variable
and given the value of ignoredups in the .bashrc file
$ echo ‘HISTCONTROL=ignoredups’ >> .bashrc
33. https://github.com/syaifulahdan/ospractice|Operating System Practice |33 to 52
Experiment 4 : Change Shell Prompt
1. The Bash shell prompt is configured by setting the
PS1 variable value.
In addition to displaying static strings as prompts, you
can display to be dynamic.
For example, if you want to show current directory or
current time. Type PS1='t:' and press Enter to display
system time in 24 hour format as Bash prompt.
Format in HH: MM: SS
$ PS1=”t:”
34. https://github.com/syaifulahdan/ospractice|Operating System Practice |34 to 52
3. Log out and log in again as the same user. Type
history a few times and notice how many times the
history appears.
$ PS1=”t:”
4. Most people want the Bash prompt to display the
current working directory.
The directory can be displayed in the form of an
entire path or just a directory name.
The w character displays only the directory name.
If current directory is the home directory, then the
prompt appears ~:.
$ PS1=”t:”
36. https://github.com/syaifulahdan/ospractice|Operating System Practice |36 to 52
7. There are several other BASH prompts that can be
changed, ie PS2, PS3 and PS4.
Prompt PS2 is used as a secondary prompt. To see how it's
used, type echo 'Hello (without ending the quote) and
press Enter.
A symbol larger than (>) will appear. This tells us that
BASH is waiting for you to complete the order.
$ echo ’Hello
>’
Type in quote cover (') and press Enter. This
command will complete the PS2 prompt, the word
"Hello," appears followed by the PS1 prompt on the
new line.
37. https://github.com/syaifulahdan/ospractice|Operating System Practice |37 to 52
8. You can change the PS2 prompt like changing the
PS1 prompt. Type the following command:
$ PS2 = “Done entering your command:'”
>’
9. Then type echo 'Hello (without ending the quote)
and press Enter.
On the next line will appear. Done entering your
command :. Then type quote cover (') and press
Enter.
If the command is complete, then the word Hello will
appear followed by the PS1 prompt on the new line.
$ echo “Hello
Done entering your command:’
38. https://github.com/syaifulahdan/ospractice|Operating System Practice |38 to 52
10. The BASH prompt can be displayed in sequence
by setting the color-setting string. For example, the
BASH prompt is set with w$, will displays the
current working directory followed by $ (or # if you
login as root).
For setting the color to blue type the following:
$ PS1=”033[0;34mw$ 033[0;37m”
11. To get a red prompt type the following:
$ PS1=”033[0;31mw$ 033[0;37m”
39. https://github.com/syaifulahdan/ospractice|Operating System Practice |39 to 52
10. The BASH prompt can be displayed in sequence
by setting the color-setting string. For example, the
BASH prompt is set with w$, will displays the
current working directory followed by $ (or # if you
login as root).
For setting the color to blue type the following:
$ PS1=”033[0;34mw$ 033[0;37m”
11. To get a red prompt type the following:
$ PS1=”033[0;31mw$ 033[0;37m”
30 = black, 31 = red, 32 = green, 34 = blue, 35 = purple, 36 = cyan, 37 = white.
40. https://github.com/syaifulahdan/ospractice|Operating System Practice |40 to 52
12. If you want multiple colors, type the following
command:
$ PS1=”033[0;31mw033[0;32m$ 033[0;37m”
13. You can display visual attributes such as brighter,
blinking and the opposite color. To display a brighter
prompt, the control attribute is replaced by 1, like
the following command:
$ PS1=”033[1;34mw033[1;32m$ 033[0;37m”
41. https://github.com/syaifulahdan/ospractice|Operating System Practice |41 to 52
14. To display a prompt with the opposite color, the
control attribute is replaced by 7, like the following
command:
15. To display a flashing prompt, the control attribute
is replaced by 5, like the following command:
$ PS1=”033[7;34mw033[7;32m$ 033[0;37m”
$ PS1=”033[7;34mw033[7;32m$ 033[0;37m”
42. https://github.com/syaifulahdan/ospractice|Operating System Practice |42 to 52
Experiment 5 : Adding automation to
Prompt Shell
1. Make sure you are in the home directory
$ cd ~
2. Create a simple script to sort the file list. You can
use the text editor, but since it is only one line, use the
echo command to create the file.
$ echo ’sort ~/list > ~/r13; mv ~/r13 ~/list’ > ~/sorter
3. Make the script file above to be an executable file
$ chmod +x sorter
43. https://github.com/syaifulahdan/ospractice|Operating System Practice |43 to 52
4. Run the sorter program above each Bash shell
displaying the PS1 prompt. To do this, create a
PROMPT_COMMAND variable where the value is
the name of the sorter program.
PROMPT_COMMAND=~/sorter
5. Type echo 'John Smith: 13001' >> list and press
Enter. If the list file does not exist, it will be created
automatically, but if it exists, the string 'John Smith:
13001' will be added.
$ echo ’John Smith:13001’>>list
44. https://github.com/syaifulahdan/ospractice|Operating System Practice |44 to 52
6. Type cat list and press Enter. Then you will see the
contents of the file list. At this time, the file may have
only one line so it can not be seen whether the file has
been sorted.
$ cat list
7. Enter some command similar to point 5 but with
different names and numbers. Then type cat list and press
Enter.
$ echo ’debian:13002’>>list
$ echo ’suse:13003’>>list
$ echo ’xandros:13004’>>list
$ echo ’centos Bob:13005’>>list
$ echo ’mint:13006’>>list
$ echo ’Squid:13007’>>list
45. https://github.com/syaifulahdan/ospractice|Operating System Practice |45 to 52
8. If you do not want Shell Bash to show the file all the
time, you do not need to add the PROMPT_COMMAND =~/
sorter variable in the configuration file like .bashrc.
If you want BASH to stop running the sorter program, then
type the PROMPT_COMMAND = variable and press
Enter or log out and login again.
$ PROMPT_COMMAND=
49. https://github.com/syaifulahdan/ospractice|Operating System Practice |49 to 52
“Pleasure in a job makes perfection on the results
achieved”. Aristoteles
“Believe you can. You're halfway”. Theodore Roosevelt
“You might be able to delay, but time will not wait”.
Benjamin Franklin
“The effort will work if someone does not give up”.
Napoleon Hill
“Opportunity to find a better strength in us arises
when life seems to be very challenging”. Joseph
Campbell