Bekerja Dengan Bash Shell Praktikum 6 Sistem Operasi
POKOK BAHASAN: History pada Bash Shell Membuat Bash Shell Script
Tujuan: Siswa mampu … Memahami shell pada sistem operasi Linux. Menggunakan feature history pada Bash Shell. Mengubah feature history pada Bash Shell. Mengubah prompt shell. Melakukan konfigurasi Bash Shell untuk menjalankan skrip secara otomatis. Membuat dan mengeksekusi shell script sederhana melalui editor vi. Memahami job control. Memahami stack. Menggunakan alias.
Why Shell? The commercial UNIX used Korn Shell For Linux, the Bash is the default Why Shell? For routing jobs, such as system administration, without writing programs However, the shell script is not efficient, therefore, can be used for prototyping the ideas For example, % ls –al | more (better format of listing directory) % man bash | col –b | lpr (print man page of man)
What is Shell? Shell is the interface between end user and the Linux system, similar to the commands in Windows Bash is installed as in /bin/sh Check the version % /bin/sh --version Kernel Other programs csh bash X window
Interactive Login ELSE IF ~/.bash_profile exists IF /etc/profile exist THEN execute /etc/profile ELSE IF ~/.bash_profile exists THEN execute ~/.bash_profile ELSE IF ~/.bash_login exist THEN execute ~/.bash_login ELSE IF ~/.profile exist THEN execute ~/.profile END IF
Pada saat login, program akan menjalankan beberapa program yaitu : /etc/profile Berisi shell script yang berlaku untuk seluruh pengguna Linux. .bash_profile The personal initialization file, executed for login shells.bash_login .bash_login .profile Berisi shell script yang berlaku untuk pengguna tertentu. 5 .bashrc File ini akan dieksekusi untuk perpindahan dari satu shell ke shell yang lain melalui instruksi su.
Interactive Logout .bash_logout Pada saat logout, maka bash akan mencari file .bash_logout. Bila ada, file tersebut akan dieksekusi sebelum logout
Environment Variables Think of the shell as any other program that you write. Your program maintains information about its current state. Since the shell's main job is to act as a liaison between the kernel and the user, it maintains information about the computing environment. The environment variables hold this information.
Important Environment Variables HOME - your home directory. USER and LOGNAME - your login ID. HOSTNAME - the name of the host computer. PWD - the current working directory. MAIL - where your mail is located. PATH - a list of directories in which to look for executable commands. Certain applications and commands may communicate with the shell and reference the environment variables that it maintains. For example, it seems that frm and nfrm seem not to work if $MAIL is not defined. frm and nfrm are commands to list the contents of your inbox without logging into pine.
Checking environment variable $printenv Display value of each variable environmet $echo $<env_variable> $echo $HOME $echo $USER Mengeset value variable environmet $export var = value $ ORACLE_HOME=/usr/lib/oracle/11.2/client64 $ export ORACLE_HOME Mengeset PATH JAVA $ JAVA_PATH=/opt/jdk/bin $ export JAVA_PATH
How to Save Environment Variable If we type environment variable on the shell, when we restart the system, the environmet variable will disappear You can add it to the file .profile or .bashrc or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded. If u want for all user, set it in /etc/profile
Prompt string $ echo $PS1 Output : \\u@\h \\W]\\$ Let us try to set the prompt so that it can display today’d date and hostname: PS1="\d \h $ “ Output : Sat Jun 02 server $ Now setup prompt to display date/time, hostname and current directory $ PS1="[\d \t \u@\h:\w ] $ “ Output : [Sat Jun 02 14:24:12 vivek@server:~ ] $
Sintax PS \a : an ASCII bell character (07) \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”) \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required \e : an ASCII escape character (033) \h : the hostname up to the first ‘.’ \H : the hostname \j : the number of jobs currently managed by the shell \l : the basename of the shell’s terminal device name \n : newline
\r : carriage return \s : the name of the shell, the basename of $0 (the portion following the final slash) \t : the current time in 24-hour HH:MM:SS format \T : the current time in 12-hour HH:MM:SS format \@ : the current time in 12-hour am/pm format \A : the current time in 24-hour HH:MM format \u : the username of the current user \v : the version of bash (e.g., 2.00) \V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde \W : the basename of the current working directory, with $HOME abbreviated with a tilde \! : the history number of this command \# : the command number of this command \$ : if the effective UID is 0, a #, otherwise a $ \nnn : the character corresponding to the octal number nnn \\ : a backslash \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt \] : end a sequence of non-printing characters
Eksekusi Program Shell Membuat file p1.sh $ vi p1.sh echo “Program bash Script” Mengubah program menjadi executable $ ls –l p1.sh $ chmod +x p1.sh $ ls –l p1.sh Menjalankan script $ bash p1.sh $ sh p1.sh $ . p1.sh $ ./p1.sh
HISTORY $export HISTCONTROL=ignoreboth:erasedups
Dirs : melihat stack directory Pushd : Memasukkan direktori tertentu ke stack Popd : Mengeluarkan direktory tertentu ke stack Alias : Melakukan alias untuk perintah tertentu Unalias
el