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

เชี่ยวชาญ Bash Variables: 6 ตัวอย่างปฏิบัติ Global & Local Variables

บทความนี้เป็นส่วนหนึ่งของชุดบทช่วยสอน Bash ต่อเนื่อง เหมือนภาษาโปรแกรมอื่นๆ Bash ก็มี Variables

Bash Variables ให้ Temporary Storage สำหรับข้อมูลที่ต้องการระหว่าง Lifespan ของโปรแกรม

Syntax:

VARNAME=VALUE

หมายเหตุ: ไม่ควรมี Space รอบเครื่องหมาย = ในการ Assign Variable เมื่อใช้ VAR=VALUE Shell เห็น = เป็น Symbol และถือว่าเป็น Variable Assignment เมื่อใช้ VAR = VALUE Shell สมมติว่า VAR เป็นชื่อ Command และพยายาม Execute มัน

Shell ไม่สนใจ Type ของ Variables Variables เก็บ Strings, Integers หรือ Real Numbers ได้

ตัวอย่าง 1: Simple Bash Variable Assignment Usage

Script ต่อไปนี้สร้าง Variable ชื่อ LIST และ Assign ค่า "/var/opt/bin" เพื่อเข้าถึง Variable ให้ Prefix Variable Name ด้วย $ จะได้ค่าที่เก็บอยู่

$ cat sample.sh
#!/bin/bash
LIST="/var/opt/bin/"
ls -l $LIST

รัน Script ข้างต้น จะ List /var/opt/bin ใน Long Format:

$ ./sample.sh
total 8
drwxrwsr-x 2 bin bin 4096 Jan 29 06:43 softwares
drwxr-sr-x 5 root bin 4096 Sep 2 2009 llist

Bash Variable Scope – Local และ Global

ใน Bash Variables ไม่ต้อง Declared แต่เมื่อ Access Variable ที่ยังไม่ได้ใช้ จะไม่ได้ Warning หรือ Error แทนที่จะแสดง Blank Value

ตัวอย่าง 2: Blank Values ใน Bash Variables

$ cat var1.sh
#!/bin/sh
echo "Variable value is: $VAR1"
VAR1="GEEKSTUFF"
echo "Variable value is: $VAR1"
$ ./var1.sh
Variable value is:
Variable value is: GEEKSTUFF

เห็นได้ว่าเริ่มต้น Variable จะมี Blank Value หลัง Assign แล้วจึงได้ค่า export Command ใช้ Export Variable จาก Interactive Shell export แสดง Effect บน Scope ของ Variables

ตัวอย่าง 3: Bash Variables โดยไม่มี Export

Assign Variable ด้วยค่าใน Interactive Shell และพยายาม Access ใน Shell Script

$ VAR2=LINUX
$ cat var2.sh
#!/bin/bash
echo "VAR2=$VAR2"
VAR2=UNIX
echo "VAR2=$VAR2"

รัน Script ข้างต้น:

$ ./var2.sh
VAR2=
VAR2=UNIX

ยังคงได้ Blank Value สำหรับ VAR2 Shell เก็บ VAR2 ไว้เฉพาะ Current Shell ระหว่าง Execute var2.sh มัน Spawn Shell และ Execute Script ดังนั้น VAR2 จะไม่มีค่าใน Spawned Shell ต้อง Export Variable เพื่อให้ถูก Inherit โดยโปรแกรมอื่น – รวมถึง Shell Script

ตัวอย่าง 4: Exporting a Bash Variable

$ export VAR2=LINUX
$ cat var2.sh
#!/bin/bash
echo "VAR2=$VAR2"
VAR2=UNIX
echo "VAR2=$VAR2"

รัน Script ข้างต้น:

$ ./var2.sh
VAR2=LINUX
VAR2=UNIX
$ $echo $VAR2
LINUX

ตอนนี้หลัง Execute var2.sh ค่า VAR2 คือ LINUX เพราะ Variables จะไม่ส่งคืนไปยัง Interactive Shell เว้นแต่ Execute Script ใน Current Shell

Declaring a Bash Variable (การประกาศตัวแปร)

ใช้ declare Statement ใน Bash สำหรับจำกัดค่าที่ Assign ให้ Variables จำกัด Properties ของ Variables Option ใน Declare Statement ใช้กำหนด Type ของ Variable

Syntax:

declare option variablename
  • declare เป็น Keyword
  • option อาจเป็น:
    • -r Read-only Variable
    • -i Integer Variable
    • -a Array Variable
    • -f สำหรับ Functions
    • -x Declares และ Export ไปยัง Subsequent Commands ผ่าน Environment

ตัวอย่าง 5: Declaration of Bash Variable ใช้ Declare

$ cat declar.sh
#!/bin/bash
declare -i intvar
intvar=123  # Assign Integer Value
echo $intvar
intvar=12.3  # พยายามเก็บ String Type ให้ Integer Variable
echo $intvar
declare -r rovar=281
rovar=212  # พยายามเปลี่ยน Readonly Variable
$ ./declar.sh
123
t.sh: line 6: 12.3: syntax error: invalid arithmetic operator (error token is ".3")
123
t.sh: line 11: rovar: readonly variable

Global Bash Variables (ตัวแปร Global)

Global Variables เรียกอีกอย่างว่า Environment Variables จะ Available ให้ All Shells คำสั่ง printenv ใช้แสดง Environment Variables ทั้งหมด

$ printenv
SHELL=/bin/bash
HISTSIZE=1000
SSH_TTY=/dev/pts/1
HOME=/root
LOGNAME=root
CVS_RSH=ssh

Local Bash Variables (ตัวแปร Local)

Local Variables มองเห็นได้เฉพาะใน Block ของ Code local เป็น Keyword สำหรับประกาศ Local Variables ใน Function Local Variable มีความหมายเฉพาะใน Function Block นั้นๆ

ตัวอย่าง 6: Global และ Local Bash Variables

$ cat localvar.sh
#!/bin/bash
pprint() {
  local lvar="Local content"
  echo -e "Local variable value with in the function"
  echo $lvar
  gvar="Global content changed"
  echo -e "Global variable value with in the function"
  echo $gvar
}
gvar="Global content"
echo -e "Global variable value before calling function"
echo $gvar
echo -e "Local variable value before calling function"
echo $lvar
pprint
echo -e "Global variable value after calling function"
echo $gvar
echo -e "Local variable value after calling function"
echo $lvar

Execute Script ข้างต้น:

$ sh t.sh
Global variable value before calling function
Global content
Local variable value before calling function
Local variable value with in the function
Local content
Global variable value with in the function
Global content changed
Global variable value after calling function
Global content changed
Local variable value after calling function

ใน Output ข้างต้น Local Variables จะมีแค่ Empty Value ก่อนและหลังเรียก Function Scope อยู่แค่ใน Function หายไปนอก Function ในขณะที่ Global Variable มี Updated Value แม้หลัง Function Execution