1. Trang chủ
  2. » Giáo án - Bài giảng

The Linux Shell and BASH Scripting

32 397 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 32
Dung lượng 146 KB

Nội dung

The Saigon CTT The Linux Shell and BASH Scripting The Saigon CTT  Objectives Objectives  Identify differrent Linux shell environments  Understand the redirection of input  Understand and utilize command substitution  Write and configure BASH script using variables, flow controls interactive input, functions, arithmetic and arrays The Saigon CTT  Example of Script Example of Script $ more example_script #!/bin/bash /usr/bin/nmbd -D /usr/bin/smbd -D The Saigon CTT  The Linux Shell The Linux Shell  Shells : Bourne (sh), Bourne Again (bash), Korn (ksh), C shell (csh, tcsh)  Programs start from command line have separate environments : parameters, variables , functions. The Saigon CTT  The Linux Shell The Linux Shell  Shells : Bourne (sh), Bourne Again (bash), Korn (ksh), C shell (csh, tcsh)  Programs start from command line have separate environments : parameters, variables , functions. The Saigon CTT  Shell Environment Customize Shell Environment Customize  bash config files : • /etc/profile • ~/.bash_profile, ~/.bash_login, ~/.profile,  Default environment variables : PS1, PS2, HOME, LOGNAME, SHELL, PATH, PAGER, LPDEST, PWD, DISPLAY, MAIL,  set, unset, export, … commands The Saigon CTT  Using the Using the bash bash shell shell  Alt+Fn  gpm : mouse server deamon  up and down keys (~/.bash_history )  Ctrl+Z, Ctrl+C, *, ?, … The Saigon CTT  Redirecting Input and Output Redirecting Input and Output  Input (<) or (<0) #mail admin@sgctt.com < file_name  Output (>) or (1>) # ls –l > file_name ( set –C or set –o noclobber : prevent overwrite )  Append (>>)  Error (2>) The Saigon CTT  Redirecting Input and Output Redirecting Input and Output  Pipe (|) #ls –l *.JPG | tee file_name #ls –al /root | grep error >error_file  Back ticks (`) or “$()” # which passwd /usr/bin/passwd # ls –l `which passwd` -r-sr-xr-x 1 root root 13476 Aug 7 /usr/bin/passwd The Saigon CTT  Redirecting Input and Output Redirecting Input and Output  Sometimes the output of a command is a list (ex: ls) and we wish to execute another command on each of the entries, use xargs #ls | xargs grep README Do Not use : #ls –al | xarg grep README [...]... done The Saigon CTT  The while loop Example count=0 while [$count –lt 4] do echo $count count=$((count +1)) done Output: 0 1 2 3 The Saigon CTT  Return codes/Exit status  The variable $? contains the return code of the previous executed command or application 0 Success ≠0 Failure  The exit n command will cause the script to quit and assign the value of n to the variable $? The Saigon CTT  Tests and. .. commands : for, while, until The Saigon CTT  The for loop  Syntax : for in do #list of commands to do done The Saigon CTT  The for loop Example #!/bin /bash for file in $(ls *.txt) do newname=“$(basename $file txt).html” mv $file $newname done The Saigon CTT  The while and until loop  Syntax : while do #list of commands to do done until do #list of commands... esac shift done The Saigon CTT  Input  We can input the information into script when executing the script ( interactive)  Commands : read select The Saigon CTT  Input - read  Allow to read values into variables  Syntax : read VAR1 VAR2 …  If there is more input than you are looking for, all the extras are put in the last variable Input – read Example The Saigon CTT  #!/bin /bash echo “Enter... : use “[ ]” around expression  if-then-else : if [ ] # include SPACEs then # what to do if the exp1 is true elseif [ ] then # what to do if the exp2 is true else fi The Saigon CTT  Tests and Conditions  Case test: case expression in pattern1 ) action ;; pattern2 ) action ;; … esac The Saigon CTT  case test Example while [ $1 ] do echo –n “$1 hits the “ case $1 in a?c | ab? ) echo “first... called postion parameter $0 The name of script $1-$9 Parameters being passed to script $* String contains ALL parameters passed to script separated by the first chacracter in IFS $@ A list of ALL as separate string $# Number of parameters on included the command line  The shift command will shift the positional parameters one or more position to the left or right Flow control The Saigon CTT   Loops.. .The Saigon CTT  Background jobs  Job #ls –l *.JPG | tee file_name #ls –al /root | grep error >error_file  Backround jobs : - Append with (&) or - Ctrl+Z and #bg %job_id The Saigon CTT  Variables  Naming : not begin with a digit  Assigning : VAR=value VAR=$(command) VAR=`command` Note : not SPACES around “=” Ex: # VAR=“Hello World” The Saigon CTT  Variables  Quotes and Command Substitution... add them” read VAR1 VAR2 echo “$VAR1 + $VAR2 =$(($VAR1+$VAR2))” The Saigon CTT  Input - select  It is great for creating menu  Syntax : select VAR [in list] do #commands that use $VAR done The Saigon CTT  Functions Syntax: function function_name { } Or function_name () { } The Saigon CTT  Functions  Functions can be called in the main script by function’s name, parameters are given on the command... VAR1=`ls /var/log | wc –l` # echo $VAR1 65 The Saigon CTT  Variable Notation  Expand variables : use ${VAR} # VAR1=“This is a String1” # VAR2=“$VAR1xyz” # VAR3=“${VAR1}xyz” # VAR4=‘${VAR1}xyz’ # echo $VAR1; echo $VAR2; echo $VAR3 # echo $VAR4 This is a String1 Nothing # default This is a String1xyz ${VAR1}xyz The Saigon CTT  Passing Info to Sript  On the command line, info can be passed to script through... are given on the command line  It inherits ALL variables in main script  We can change the return code of function by using return n command The Saigon CTT  Advanced bash Concepts  Arithmetic Expression :  Arrays : var[subscript]  More thrilling Examples for file in $(ls file*) do mv $file ${file%html}txt done The Saigon CTT  Summary Step 1 : create script file (cat, vi, mc, …), enter script codes . The Saigon CTT The Linux Shell and BASH Scripting The Saigon CTT  Objectives Objectives  Identify differrent Linux shell environments  Understand the. variables , functions. The Saigon CTT  The Linux Shell The Linux Shell  Shells : Bourne (sh), Bourne Again (bash) , Korn (ksh), C shell (csh, tcsh)  Programs

Ngày đăng: 24/06/2013, 01:25

TỪ KHÓA LIÊN QUAN

w