Tìm kiếm theo tiêu đề

Tin tức cộng đồng

[MỜI HỢP TÁC] Các kỳ thi Olympic Quốc tế 2026 (IMO - IEO - ISO)

Kính gửi Quý Lãnh đạo, Ban Giám hiệu và Quý Thầy/Cô, FermatTech (Đối tác Google tại VN) phối hợp cùng SCO Ấn Độ trân trọng kính mời tham gia 3 kỳ thi uy tín dành cho HS từ lớp 1 - 12: - IMO: Olympic Toán Quốc tế. - IEO: Olympic Tiếng Anh Quốc tế. - ISO: Olympic Khoa học...
Xem tiếp

Tin tức thư viện

Chức năng Dừng xem quảng cáo trên violet.vn

12087057 Kính chào các thầy, cô! Hiện tại, kinh phí duy trì hệ thống dựa chủ yếu vào việc đặt quảng cáo trên hệ thống. Tuy nhiên, đôi khi có gây một số trở ngại đối với thầy, cô khi truy cập. Vì vậy, để thuận tiện trong việc sử dụng thư viện hệ thống đã cung cấp chức năng...
Xem tiếp

Hỗ trợ kĩ thuật

  • (024) 62 930 536
  • 0919 124 899
  • hotro@violet.vn

Liên hệ quảng cáo

  • (024) 66 745 632
  • 096 181 2005
  • contact@bachkim.vn

The Linux Shell and BASH Scripting

Wait
  • Begin_button
  • Prev_button
  • Play_button
  • Stop_button
  • Next_button
  • End_button
  • 0 / 0
  • Loading_status
Nhấn vào đây để tải về
Báo tài liệu có sai sót
Nhắn tin cho tác giả
(Tài liệu chưa được thẩm định)
Nguồn: Sưu tầm
Người gửi: Nguyễn Việt Vương
Ngày gửi: 14h:53' 08-04-2008
Dung lượng: 146.0 KB
Số lượt tải: 42
Số lượt thích: 0 người
The Linux Shell and BASH Scripting
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
Example of Script
$ more example_script
#!/bin/bash
/usr/bin/nmbd -D
/usr/bin/smbd -D
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 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.
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
Using the bash shell
Alt+Fn
gpm : mouse server deamon
up and down keys (~/.bash_history )
Ctrl+Z, Ctrl+C, *, ?, …
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>)
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
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
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
Variables
Naming : not begin with a digit
Assigning : VAR=value
VAR=$(command)
VAR=`command`
Note : not SPACES around “=”

Ex: # VAR=“Hello World”
Variables
Quotes and Command Substitution

Note: # VAR=“Hello World”
# echo “$VAR”  Hello World
# echo ‘$VAR’  $VAR

# VAR1=`ls /var/log | wc –l`
# echo $VAR1
65
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
Passing Info to Sript
On the command line, info can be passed to script through pre-set variables 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
Loops : do something more than one
Loop commands : for, while, until
The for loop
Syntax :
for in
do
#list of commands to do
done
The for loop Example
#!/bin/bash
for file in $(ls *.txt)
do
newname=“$(basename $file .txt).html”
mv $file $newname
done
The while and until loop
Syntax :
while
do
#list of commands to do
done

until
do
#list of commands to do
done
The while loop Example
count=0
while [$count –lt 4]
do
echo $count
count=$((count +1))
done
Output:
0
1
2
3
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 $?
Tests and Conditions (p.10-18)
Test : 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
Tests and Conditions
Case test:
case expression in
pattern1 )
action
;;
pattern2 )
action
;;
…
esac
case test Example
while [ $1 ]
do
echo –n “$1 hits the “
case $1 in
a?c | ab? )
echo “first case.” ;;
abcde )
echo “second case.” ;;
abc123 )
echo “third case.” ;;
* )
echo “fallout case.” ;;
esac
shift
done
Input
We can input the information into script when executing the script ( interactive)

Commands :
read
select

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
#!/bin/bash
echo “Enter 2 number, i will add them”
read VAR1 VAR2
echo “$VAR1 + $VAR2 =$(($VAR1+$VAR2))”
Input - select
It is great for creating menu
Syntax :
select VAR [in list]
do
#commands that use $VAR
done
Functions
Syntax:
function function_name
{
}
Or
function_name ()
{
}
Functions
Functions can be called in the main script by function’s name, parameters 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
Advanced bash Concepts
Arithmetic Expression :
Arrays : var[subscript]
More thrilling Examples
for file in $(ls file*)
do
mv $file ${file%html}txt
done
Summary
Step 1 : create script file (cat, vi, mc, …), enter script codes.

Step 2 : add execute permission mode to file ( chmod +x file )

Step 3 : run it (add script directory to PATH environment or use absolute path)
 
Gửi ý kiến