Shell script เป็น file text ธรรมดาสามารถสร้างด้วย editor ชนิดไหนก็ได้ เช่น nano หรือ vim
กำหนด interpreter หรือ shebang บรรทัดแรกของ shell script เราต้องกำหนด shebang เพื่อให้ program รู้ว่าควรใช้ interpreter ใดในการ load ข้อมูล file ซึ่งสัญลักษณ์ที่ใช้คือ #!<shell program>
#!/bin/sh
#!/bin/csh
#!/bin/bash
#!/bin/ksh
#!/usr/bin/perl
#!/usr/bin/env python
ใส่ชุดคำสั่ง command line
ส่วนนี้คือ code program ที่เราต้องการให้ shell script ทำงาน ซึ่งสามารถศึกษาชุดคำสั่ง Shell ของ Unix และ Linux ได้












กำหนดสิทธิ์ execute ให้กับ shell script
หลักจากเราสร้าง Shell script ขึ้นมาได้แล้ว ลองสังเกตุจะเห็นว่า file ที่ได้มีสิทธิ์ หรือ permission ของ user owner (-rw-rw-r–) เพียงแค่ write กับ read เท่านั้น ไม่สามารถ execute ได้ ซึ่งก็ไม่ต่างกับ text file ธรรมดา
c@c:~/cos3105$ ls -ltr test.sh
-rw-r--r-- 1 c c 20 Jul 24 14:03 test.sh
ตอนจะรัน shell script ใช้คำสั่งดังนี้ แต่จากที่ผลลัพธ์คือรันไม่ได้
bash: ./test.sh: Permission denied
ต้องเปลี่ยน permission ของไฟล์ให้สามารถรันได้ก่อนนั่นก็คือ
c@c:~/cos3105$ chmod 700 test.sh
c@c:~/cos3105$ ls -ltr test.sh
-rwx------ 1 c c 20 Jul 24 14:03 test.sh
ซึ่งเมื่อมีเครื่อง x แล้วนั้นหมายถึงเราสามารถ execute file นี้ได้แล้ว
การสร้างตัวแปรใน shell script (variable)
เชลล์สคริปต์เหมือนกับโปรแกรมทั่วๆไปที่มีตัวแปรไว้เก็บค่าต่างๆ สำหรับใช้งาน ตัวแปรที่ใช้ในเชลล์นั้นไม่จำเป็นต้องประกาศชนิดตัวแปรเหมือนกับภาษา C สามารถตั้งค่าแล้วนำไปใช้ได้ทันที
variable=value
การเรียกใช้งานตัวแปรใน shell script (variable)
การเรียกใช้งานตัวแปรใน shell script (variable)
ซึ่งผลลัพธ์ที่ได้คือ เครื่องหมาย Quote ( ‘ ) จะมีค่าตามสิ่งที่พิมพ์ ส่วนในเครื่องหมาย Double quote ( “ ) จะเป็นการอ้างอิงนำผลลัพธ์ที่ตัวแปรเก็บไว้หรือค่าที่ตัวแปรเก็บไว้มาใช้
การใช้งานตัวแปร environment (environment variable)
ตัวแปรที่ใช้ในเชลล์จะมีสองชนิดด้วยกัน คือ
- ตัวแปรธรรมดา (variable)
- ตัวแปรสภาพแวดล้อม (environment variable)
ซึ่งผลลัพธ์ที่ได้คือ $PWD คือ Print Working Directory
ถ้าเราเรียกใช้ตัวแปรที่ไม่ได้อยู่ใน environment ผลลัพธ์ที่ได้จะมีค่าเป็น null
วิธีแก้คือ
c@c:~/cos3105$ export NAME="Krit Chomaitong"
หลังจากนั้น
ตัวแปรแบบพิเศษ
| ตัวแปร | ความหมาย |
|---|
$0 $1 $2 … | ใช้สำหรับอ้างอิงชื่อ shell script และ Argument ของ shell script เรียกว่า position parameter |
$# | ใช้บอกจำนวน Argument ที่อยู่ใน shell script นั้นๆ |
$* | แทน Argument ของ script เรียงกันทั้งหมด |
$@ | คล้ายกับ $* แต่จะใช้ช่องว่างคั่นระหว่าง position parameter |
$? | ใช้แสดงสถานการณ์จบการทำงานครั้งสุดท้าย ถ้าสั่งได้ถูกต้องไม่มี error จะแสดงค่า 0 ออกมา แต่ถ้าผิดพลาดจะแสดงค่าที่ไม่ใช่ 0 ออกมา |
$! | ใช้แสดง process ID ของ shell ที่ทำงานอยู่ |
c@c:~/cos3105$ ./test.sh hello world
$#
c@c:~/cos3105$ ./test.sh hello world
c@c:~/cos3105$ ./test.sh hello world aaa
c@c:~/cos3105$ ./test.sh hello world aaa
# ต้องใช้ long process อย่าง sleep ถึงจะบอกได้ว่า process ID คืออะไร
ใช้เครื่องหมาย # สำหรับการ comment (ยกเว้นบรรทัดแรกเพราะเป็น shebang)
จากผลลัพธ์ที่ได้ โปรแกรมจะไม่แสดงบรรทัดที่ comment ออกมา
echo $txt[0] $txt[1] $txt[2]
echo ${txt[0]} ${txt[1]} ${txt[2]}
ผลลัพธ์ที่ได้
OUTPUT
bio.db bio.txt cron.log email.sh lab2-2.sh lab2.sh test.bak test.sh
OUTPUT
./test.sh: line 3: a/b: division by 0 (error token is "b")
if [ "$int1" -eq "$int3" ]
if [ "$int1" -ne "$int2" ]
echo "$int1 not equal $int2"
if [ "$int2" -gt "$int1" ]
echo "$int2 greater than $int1"
if [ "$int2" -ge "$int1" ]
echo "$int2 greater than or equal $int1"
if [ "$int1" -lt "$int2" ]
echo "$int1 less than $int2"
if [ "$int1" -le "$int2" ]
echo "$int1 less than or equal $int2"
OUTPUT
2 greater than or equal 1
if [ "$str2" != "$str3" ]
echo "$str1 not equal $str3"
echo "$str4 is not empty"
echo "$str4 contains space"
echo "$str5 is also empty"
OUTPUT
echo "$folderName is a folder"
echo "$fileName is a file"
echo "$fileName is readable"
echo "$fileName is writable"
echo "$fileName is executable"
echo "$fileName is not empty"
OUTPUT
- && คือ and ต้องใช้ภายใต้ [[…]]
- || คือ or ต้องใช้ภายใต้ [[…]]
if [[ $a -lt 10 && $b -gt 5 ]]; then
echo "Both conditions are true"
if [[ $a -lt 5 || $b -gt 5 ]]; then
echo "At least one condition is true"
if [ "$a" -eq "$b" ]; then
if [ "$a" -eq "$b" ]; then
elif [ $((a * 2)) -eq "$b" ]; then
OUTPUT
ตัวอย่าง
OUTPUT
ตัวอย่าง
OUTPUT
ตัวอย่าง1
OUTPUT
ตัวอย่าง2
OUTPUT
OUTPUT
c@c:~/cos3105$ ./test.sh 1 2
| คำสั่ง | ความหมาย |
|---|
echo "Hello" > file.txt | Write output to file.txt (overwrite) |
echo "Hello" >> file.txt | Append output to file.txt |
| คำสั่ง | ความหมาย |
|---|
cat < file.txt | Read from file.txt |
OUTPUT
- grep = “global regular expression print”
- ดึงค่า name:
address: 123 Maplewood Lane, Fairview, CA 90210, USA
OUTPUT
c@c:~/cos3105$ grep "name: " bio.txt
cut -d':' -f2- bio.txt # -d for single character
grep "^name:" bio.txt | cut -d':' -f2- # grep with cut
OUTPUT
123 Maplewood Lane, Fairview, CA 90210, USA
sed -n 's/^name:\(.*\)/\1/p' bio.txt
OUTPUT
sed -i 's/^name:.*/name:Krit Chomaitong2/' bio.txt # update
sed -n 's/^name:\(.*\)/\1/p' bio.txt # print match name:
OUTPUT
OUTPUT
1 curl -fsSL https://code-server.dev/install.sh | sh
2 sudo apt install neovim build-essential gcc g++ gdb
- การใช้ร่วมกับ grep โดยใช้ pipe
OUTPUT
c@c:~/cos3105$ history | grep cat
OUTPUT
Sun Aug 3 01:39:36 PM UTC 2025
Sun Aug 3 01:39:36 PM UTC 2025
Sun Aug 3 01:39:36 PM UTC 2025
Sun Aug 3 01:39:36 PM UTC 2025
Sun Aug 3 01:39:36 PM UTC 2025
| Format | ความหมาย | ตัวอย่าง |
|---|
%Y | ปี (4 หลัก) | 2025 |
%y | ปี (2 หลัก) | 25 |
%m | เดือน (01-12) | 08 |
%d | วัน (01-31) | 03 |
%H | ชั่วโมง (00–23) | 19 |
%M | นาที (00–59) | 50 |
%S | วินาที (00–59) | 00 |
%A | ชื่อวัน (เต็ม) | Sunday |
%a | ชื่อวัน (ย่อ) | Sun |
%B | ชื่อเดือน (เต็ม) | August |
%b | ชื่อเดือน (ย่อ) | Aug |
ตัวอย่าง
date +"%Y-%m-%d %H:%M:%S"
date -d "tomorrow" +"%Y-%m-%d" # tomorrow
OUTPUT