Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

เชี่ยวชาญ Bash Special Parameters: 4 ตัวอย่าง Shell Script ปฏิบัติ

เป็นส่วนหนึ่งของชุดบทช่วยสอน Bash ต่อเนื่อง เราพูดถึง Bash Positional Parameters ในบทความก่อนหน้า ในบทความนี้มาพูดถึง Bash Special Parameters พร้อมตัวอย่าง Shell Script ปฏิบัติ

Bash Special Parameters ที่จะพูดถึง: $*, $@, $#, $$, $!, $?, $-, $_

เพื่อ Access Whole List ของ Positional Parameters มี Special Parameters 2 ตัวคือ $* และ $@ นอก Double Quotes ทั้งคู่เทียบเท่า: ทั้งคู่ Expand ไป List of Positional Parameters เริ่ม $1 (คั่นด้วย Spaces)

ภายใน Double Quotes แตกต่างกัน: $* ใน Double Quotes เทียบเท่า List of Positional Parameters คั่นด้วย First Character ของ IFS "$1c$2c$3..."

$@ ใน Double Quotes เทียบเท่า List of Positional Parameters คั่นด้วย Unquoted Spaces คือ "$1" "$2"..."$N"

ตัวอย่าง 1: ใช้ Bash $* และ $@ Expand Positional Parameters

ตัวอย่างนี้แสดงค่าใน $* และ $@

สร้าง expan.sh ก่อน:

$ cat expan.sh
#!/bin/bash
export IFS='-'
cnt=1
# Printing the data available in $*
echo "Values of \"\$*\":"
for arg in "$*"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done
cnt=1
# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

รัน expan.sh เพื่อดู $* และ $@ ทำงาน:

$ ./expan.sh "This is" 2 3
Values of "$*":
Arg #1= This is-2-3
Values of "$@":
Arg #1= This is
Arg #2= 2
Arg #3= 3
  • Script ข้างต้น Export ค่า IFS (Internal Field Separator) ด้วย '-'
  • มี 3 Parameters ส่งให้ Script expan.sh $1="This is",$2="2" และ $3="3"
  • พิมพ์ค่า Special Parameter "$*" ให้ค่าเดียวคือ Whole Positional Parameter Delimited by IFS
  • ในขณะที่ "$@" ให้แต่ละ Parameter เป็น Separate Word

ตัวอย่าง 2: ใช้ $# นับ Positional Parameters

$# เป็น Special Parameter ใน Bash ให้ Number of Positional Parameters ใน Decimal

สร้าง arithmetic.sh:

$ cat arithmetic.sh
#!/bin/bash
if [ $# -lt 2 ]
then
  echo "Usage: $0 arg1 arg2"
  exit
fi
echo -e "\$1=$1"
echo -e "\$2=$2"
let add=$1+$2
let sub=$1-$2
let mul=$1*$2
let div=$1/$2
echo -e "Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n"

ถ้าจำนวน Positional Parameters น้อยกว่า 2 จะโยน Usage Info:

$ ./arithemetic.sh 10
Usage: ./arithemetic.sh arg1 arg2

ตัวอย่าง 3: Process Related Parameters – $$ และ $!

Special Parameter $$ ให้ Process ID ของ Shell $! ให้ Process ID ของ Most Recently Executed Background Process

Script ต่อไปนี้พิมพ์ Process ID ของ Shell และ Last Executed Background Process ID

$ cat proc.sh
#!/bin/bash
echo -e "Process ID=$$"
sleep 1000 &
echo -e "Background Process ID=$!"

รัน Script และเช็ค Process ID ที่พิมพ์:

$ ./proc.sh
Process ID=9502
Background Process ID=9503
$ ps
PID TTY TIME CMD
5970 pts/1 00:00:00 bash
9503 pts/1 00:00:00 sleep
9504 pts/1 00:00:00 ps
$

ตัวอย่าง 4: Bash Special Parameters อื่นๆ – $?, $-, $_

  • $? ให้ Exit Status ของ Most Recently Executed Command
  • $- Options Set ด้วย Set Builtin Command
  • $_ ให้ Last Argument ของ Previous Command ตอน Shell Startup ให้ Absolute Filename ของ Shell Script ที่ Execute อยู่
$ cat others.sh
#!/bin/bash
echo -e "$_";  ## Absolute name of the file which is being executed
/usr/local/bin/dbhome
# Execute the command.
# Check the exit status of dbhome
if [ "$?" -ne "0" ]; then
  echo "Sorry, Command execution failed !"
fi
echo -e "$-";  # Set options - hB
echo -e $_     # Last argument of the previous command.

ใน Script ข้างต้น Echo Statement สุดท้าย echo -e $_ ($ underscore) ก็พิมพ์ hB ซึ่งเป็นค่า Last Argument ของ Previous Command ดังนั้น $_ จะให้ค่าหลัง Expansion

$ ./others.sh
./others.sh
/home/oracle
Sorry, Command execution failed !
hB
hB