This document provides instructions on practicing various features of the Bash shell in Linux, including:
1. Creating and running simple shell scripts using tools like vi editor and chmod.
2. Using job control commands like foreground and background processes.
3. Manipulating the directory stack and using aliases to create command shortcuts.
4. Customizing shell features like history, prompts, and profiles.
Exercises are provided to help practice and explore these Bash shell capabilities.
2. https://github.com/syaifulahdan/ospractice|Operating System Practice |2 to 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
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 39
: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 39
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.
20. https://github.com/syaifulahdan/ospractice|Operating System Practice |20 to 39
3. Running the script
$ bash p1.sh
$ sh p1.sh
$ . p1.sh
$ ./p1.sh
4. The convention in shell script creation is expressed as
#!/ Bin/bash. Add to the p1.sh file of the convention.
$ vi p1.sh
#!/bin/bash
echo “Program bash script”
21. https://github.com/syaifulahdan/ospractice|Operating System Practice |21 to 39
5. Create a p2.sh file
$ vi p2.sh
#!/bin/bash
echo “Program 2 bash script”
6. Running multiple shell programs in a single line of
instructions separated by a sign;
$ cat p1.sh ; cat p2.sh
$ ./p1.sh ; ./p2.sh
25. https://github.com/syaifulahdan/ospractice|Operating System Practice |25 to 39
Experiment 8 : Manipulation stack for
Directories
1. Dirs instruction is used to view the directory stack, the
output is displayed only home directory ~
$ dirs
2. Create 3 directory pieces
$ mkdir marketing sales support
3. Dirs instruction is used to view the directory stack, the
output is displayed only home directory ~
$ dirs
26. https://github.com/syaifulahdan/ospractice|Operating System Practice |26 to 39
Experiment 9 : Alias
1. Alias is a mechanism for naming aliases on one or a
group of instructions. To view aliases that have been
registered on the system:
$ alias
2. Create multiple aliases
$ alias del=’rm –i’
$ alias h=’history’
29. https://github.com/syaifulahdan/ospractice|Operating System Practice |29 to 39
Exercise : Practice 5B
1 Execution of all existing profiles:
a. Edit the profile file /etc/profile and display the message
as follows:
echo 'Profile of / etc / profile'
b. Assuming your name is student, then edit all existing profiles:
/home/student /.bash_profile
/home/. student/.bash_login
/home/student /.profile
/home/student /.bashrc
30. https://github.com/syaifulahdan/ospractice|Operating System Practice |30 to 39
c. Rename /home/student with your own name. , include the echo
instruction, eg On each file in
/home/student/.bash_profile:
echo "Profile from .bash_profile"
d. Do the same for other files, adjust the display with the
corresponding file name.
2 Run the user subtitute instruction, then exit with the command exit as
follows:
$ su student
$ exit
then use the - option as follows:
$ su – student
$ exit
Explain the differences between the two utilities.
31. https://github.com/syaifulahdan/ospractice|Operating System Practice |31 to 39
3 Logout
a. Edit .bash_logout file, show message and hold for 5 seconds,
before execution logout
Echo "Thank you for the session given"
Sleep 5
Clear
4 History
a. Change the value of HISTSIZE from 1000 to 20
$ HISTSIZE=20
$ h
b. Use the history feature by editing the 5th line instruction from the
last instruction.
$ !5
32. https://github.com/syaifulahdan/ospractice|Operating System Practice |32 to 39
c. Repeat the last instruction. Use also ^ P and ^ N to navigate to the
history buffer
$ !!
d. Repeat the instructions on the history buffer of a certain number,
eg number 150
$ !150
e. Repeat instruction with "ls" prefix
$! ls
$!? ls?
Explain the different instructions above
33. https://github.com/syaifulahdan/ospractice|Operating System Practice |33 to 39
5 Prompt String (PS)
a. Edit the .bash_profile file, replace the PS1 prompt with '>'. An
export instruction is required with the variable name of the variab le,
in order to change the PS1 variable known to all shells
PS1=’> ‘
export PS1
PS1 results experiment:
PS1=“! > “
69 > PS1=”d > “
Mon Sep 23 > PS1=”t > “
10:10:20 > PS1=”Saya=u > “
I =stD02001 > PS1=”w >”
~ > PS1=h >”
b. Change the color of the shell prompt in blue and blink.
34. https://github.com/syaifulahdan/ospractice|Operating System Practice |34 to 39
6 Bash script
a. Create 3 p1sh, p2.sh, p3.sh script with their respective contents:
p1.sh
#! /bin/bash
echo “Program p1”
ls –l
p2.sh
#! /bin/bash
echo “Program p2”
Who
p3.sh
#! /bin/bash
echo “Program p3”
ps x
36. https://github.com/syaifulahdan/ospractice|Operating System Practice |36 to 39
7 Jobs
a. Create a shell-script that does a loop with the name pwaktu.sh,
every 10 second, then save the date and time in the result file.
#!/bin/bash
while [ true ]
Do
date >> hasil
Sleep 10
Done
b. Run as background; then run one program (find utility)
$ jobs
$ find / print > files 2>/dev/null &
$ jobs
39. https://github.com/syaifulahdan/ospractice|Operating System Practice |39 to 39
“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