Advance Innovation Centre
  • AIC Knowledge @ EEC for All
  • 😎Logical Thinking
    • Karel Robot
    • Code to Flowchart
    • Play with Docker
    • CNX Software
  • MCU & Interfacing with Infineon PSOC™
    • Basic MCU Interfacing
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
      • PSoC™ 6S2 Peripherals Interfacing (GPIO)
        • Hello World and LED Blinking
        • GPIO Principles
        • PSoC™ 6S2 GPIO-HAL LED Blink Lab
        • PSoC™ 6S2 GPIO-PDL LED Blink Lab
        • Button "Bounce" Principles
          • Push/Pull Button to Turn ON/OFF LED via HAL
          • Push/Pull Button to Turn ON/OFF LED via PDL
          • GPIO Button Interrupt via HAL
          • GPIO Button Interrupt via PDL
        • GPIO variables & functions
      • PSoC™ 6S2 Peripherals Interfacing (ADC, PWM)
        • PSoC™ 6S2 SAR ADC
          • ADC Principles
          • PSoC™ 6S2 with ADC Labs
            • Reading potentiometer sensor value via an ADC HAL
            • Reading potentiometer sensor value via an ADC PDL
        • PSoC™ 6S2 PWM & TCPWM
          • PWM Principles
          • PSoC™ 6S2 for PMW Function Labs
            • LED Brightness using PWM via HAL
            • LED Brightness using PWM via PDL
    • Sensor Interfacing and HMI
      • OLED Display
        • OLED Display Principles
        • Calling BDH’s OLED functions
        • Display ADC via Potentiometer on OLED
      • BDH Shell
        • Shell Principles
        • LED Blinking and CAPSENSE via BDH Shell
        • Adding "History" command
        • Adding "Reboot" command
        • CAPSENSE Button and Slider
          • CAPSENSE Button and Slider with Capsense Tuner
          • CAPSENSE Button and Slider using FreeRTOS
    • Serial Communication & Visualization
      • UART, I2C, SPI Communication via Infineon PSoC™6
      • BMX160 Sensor Communication via Infineon PSoC™6
        • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
        • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
        • Motion Sensors GUI Integration via Serial Studio
    • IoT Connectivity & Data Analytics via Node-Red
      • Node-Red Installation
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • Edge AI on PSoC™
      • Machine Learning on PSoC™6 via Edge-Impulse
    • Infineon PSoC™ Troubleshooting
  • IoT Development with Infineon PSOC™ & BDH Platform
    • PSoC™ IoT Development Kit
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
        • Hello World and LED Blinking
    • IoT Connectivity
      • Node-Red Installation
      • Controlling PSoC™ LED using MQTT
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • BDH IoT Connectivity
    • WireLinX™ IoT PLC
    • BDH X-Brain Data Analytics
      • PSoC6 Data Collection to CSV log file
    • Data Visualization
      • สร้าง Dashboard ด้วย Looker Studio
  • 🖥️Operation Systems
    • Prerequisites
      • Guideline from Ubuntu
        • Ubuntu and VSCode on WSL2
      • ติดตั้ง WSL 2
      • Run Ubuntu on VirtualBox7
    • Zero to Linux Hero
      • Computer OS Architecture
      • Anatomy of Linux System
        • UNIX/Linux History
        • UNIX/Linux Evolution
        • GNU Project
        • Linux OS Architecture
        • Command Line Interface (CLI)
          • Basic Commands
          • 😎Level up your Linux Shell
          • File & Dir. Commands
          • Searching Commands
          • 😎ChatGPT-based Terminal
          • SysAdmin Commands
          • Network Commands
          • Hacker Commands
        • Busybox
        • Shell Script
          • Awk Script
          • Bash Shell Script
            • Bash Snippets
            • Bash Useful Examples
      • Anatomy of Linux Kernel
        • Linux Kernel Principles
        • Linux Environment for Developer
      • Anatomy of Embedded Linux
        • Embedded Linux
        • Host & Target
        • Cross Toolchains
        • Bootloader
        • Building Embedded Linux
    • Linux OS Dev. Engineer
      • Process Management
        • Process Basic
        • Process State
        • Basic Process Mgmt. Commands
        • Advance Process Mgmt. Commands
        • Process API Programming
      • IPC
        • IPC Anatomy
        • Signal Programming
        • Pipe Programming
        • FIFO Programming
        • Msg. Queue Programming
          • System V
        • Share Memory Programming
          • System V
        • Socket Programming
      • POSIX Threads
        • Multi-tasking Basic
        • POSIX Thread Anatomy
        • Threading Programming
      • Applied IPC
        • Remote Commander
        • Multi-Remote Commanders
      • Process Synchronization
        • Mutex Programming
        • Semaphore Programming
      • Applied IPC with Semaphore
  • ⌚Embedded Systems Development
    • Introduction to ESD
      • Why's ESD?
      • What it use for?
      • How it works?
    • Enbedded System Development via PSoC6
      • Basic MCU Interfacing
        • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
        • Development Environment Preparation
        • PSoC™ 6S2 Peripherals Interfacing (GPIO)
          • Hello World and LED Blinking
          • GPIO Principles
          • PSoC™ 6S2 GPIO-HAL LED Blink Lab
          • PSoC™ 6S2 GPIO-PDL LED Blink Lab
          • Button "Bounce" Principles
            • Push/Pull Button to Turn ON/OFF LED via HAL
            • Push/Pull Button to Turn ON/OFF LED via PDL
            • GPIO Button Interrupt via HAL
            • GPIO Button Interrupt via PDL
          • GPIO variables & functions
        • PSoC™ 6S2 Peripherals Interfacing (ADC, PWM)
          • PSoC™ 6S2 SAR ADC
            • ADC Principles
            • PSoC™ 6S2 with ADC Labs
              • Reading potentiometer sensor value via an ADC HAL
              • Reading potentiometer sensor value via an ADC PDL
          • PSoC™ 6S2 PWM & TCPWM
            • PWM Principles
            • PSoC™ 6S2 for PMW Function Labs
              • LED Brightness using PWM via HAL
              • LED Brightness using PWM via PDL
      • Sensor Interfacing and HMI
        • OLED Display
          • OLED Display Principles
          • Calling BDH’s OLED functions
          • Display ADC via Potentiometer on OLED
        • BDH Shell
          • Shell Principles
          • LED Blinking and CAPSENSE via BDH Shell
          • Adding "History" command
          • Adding "Reboot" command
          • CAPSENSE Button and Slider
            • CAPSENSE Button and Slider with Capsense Tuner
            • CAPSENSE Button and Slider using FreeRTOS
      • Serial Communication & Visualization
        • UART, I2C, SPI Communication via Infineon PSoC™6
        • BMX160 Sensor Communication via Infineon PSoC™6
          • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
          • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
          • Motion Sensors GUI Integration via Serial Studio
    • Edge Computing and IoT Connectivity
    • Cloud-Based Data Analytics and Digital Twin
    • Edge Vision AI
    • Resources
      • Basic Hardware and Firmware
        • Environment Preparation
          • การติดตั้งโปรแกรม Arduino IDE
            • ตัวอย่างการเริ่มต้นใช้งาน Arduino IDE
          • การติดตั้งโปรแกรมสำหรับใช้งานเครื่องมือวัด NI MyDAQ
            • ตัวอย่างการตั้งค่าใช้ Digital Multimeter -NI ELVISmx
            • ตัวอย่างการตั้งค่าใช้ Oscilloscope-NI ELVISmx
          • ติดตั้งโปรแกรม KingstVIS
        • Basic measurement
          • Basic Digital and Analog I/O
            • LAB: Basic Digital Input/Output
            • LAB: Basic Analog Input/Output
          • Waveform
            • LAB: Oscilloscope
            • LAB: Oscilloscope and Function Generator
            • LAB: Pulse Width Modulation (PWM)
              • Homework
        • Interfacing and Communication
          • LAB: UART, RS485, RS232 Protocol
          • LAB: I2C Protocol
            • HOMEWORK
          • LAB: SPI Protocol
      • IoT Connectivity
        • Example: IoT with MQTT on Node-red
        • Data logger
        • LAB: Data Visualization
  • 🛠️C/C++ for Embedded Programming
    • Development Environment Preparation
      • ติดตั้ง WSL 2
      • ติดตั้ง Ubuntu environment
      • ติดตั้งโปรแกรม Visual Studio Code
      • การเชื่อมต่อ Virtual studio code เข้ากับ WSL
      • ติดตั้ง docker on WSL
    • Principle C/C++ Programming
      • Get started with C++
      • Makefile
        • Makefile Examples
      • Compiling and running
        • How to create a program that you can enter inputs.
          • Lab 1 Exercise
      • Arguments
        • Command line arguments in C and C++
      • signed and unsigned data types
      • Variable and Operator
      • If and If else
      • Loop, Infinite loop, and flag
        • Loop and Flag exercise
      • Array
        • Get to know with arrays
        • Implement example
      • Vector
    • Object Oriented Programming (OOP) in C++
      • Class and Object
      • Encapsulation and Abstraction
      • Polymorphism and Inheritance
    • C/C++ Preprocessing
      • Macro
        • Quiz Macro
      • File Inclusion
      • Conditional Compilation
      • Pragma directive
        • Quiz Pragma
    • String in C++
      • Concatenation
      • Split
    • Type conversions for C/C++
      • Conversion using Cast operator
    • Error handling
    • Data logger
      • การสร้างไฟล์และเขียนไฟล์
      • การอ่านไฟล์
      • การเก็บข้อมูลกับTime stamp
    • High performance programing
      • Multi-task and Multi-thread
        • Multi-threading example
      • Mutex
      • Queue
      • OpenCV
    • C/C++ Techniques
      • Makefile in action
      • Object Oriented Programming (OOP) in C++
        • Class and Object
        • Encapsulation and Abstraction
        • Polymorphism and Inheritance
      • C/C++ Preprocessing
        • Macro
          • Quiz Macro
        • File Inclusion
        • Conditional Compilation
        • Pragma directive
          • Quiz Pragma
      • Binary, Octal and Hexadecimal Numbers
      • Array and properties of an array
        • Get to know with arrays
        • Implement example
      • What's next?
  • 🤖Artificial Intelligence (AI)
    • VAMStack Design House, BUU
    • Data Analytics
      • Data cleansing
      • Data analytics
      • Data analytic exercise
    • Machine Learning
      • Neural Network Layers
      • Machine learning type
      • Dataset
      • Using Edge Impulse for AI Model
    • Basic Image Processing
      • Computer Vision using Python Language
        • Installation
        • Computer Vision Basics
          • Pixel and Color
          • Draw image
          • Basic Image processing
          • Morphology Transformations
          • Gaussian blur
          • Simple Thresholding
          • Contour
          • Canny edge detection
        • Case Study
          • Coin counting
          • Color detection & tracking
        • VAM_CV SDK
  • ⚙️FPGA Design and Development
    • Verilog HDL via Vivado IDE
      • LAB1: Setting Environment and Create Project
        • Create Vivado Project
      • LAB2: Hardware Description Language Work Flow
        • Simulation code
      • LAB3: Design HDL Project
        • Top Level Design
        • Top-level Simulation
      • LAB4: Asynchronous VS Synchronous Circuit
        • Simulation Synchronous counter
    • C/C++ Programming on Ultra96v2 FPGA Board
      • Application C/C++ on Ultra96v2 Part 1
        • Design Overview
        • Step 1 - Burn the image to SD card
        • Step 2 - Bring up Ultra96v2
        • Step 3 - Installing the Vitis-AI runtime packages
      • Application C/C++ on Ultra96v2 Part 2
        • STEP 1 : Setting auto boot Wifi
        • STEP 2 : How to working on Embedded
        • STEP 3 : How to run the test code
  • 🤖Robotics
    • Dobot Magician
      • Instruction of Dobot
      • Software Download
      • Basically of Program
        • Teaching and Playback
        • Write and Draw
        • LaserEngraving
        • 3D Printer
    • Robotino
      • Software Download
        • Robotino View
        • Robotino SIM
      • Charging
      • Connecting
      • Follow Line example
        • Basic block in Follow Line
    • RaspBlock
      • Get Started with Raspblock
  • 🚩Special Topics
    • Node-Red
      • Set up Raspberry Pi
      • Install node red in Raspberry Pi
      • Get started with Node Red
        • Open node-red
        • Turn off node red
        • Install Dashboard on Node-red
        • Use node red to show message
        • Using Ultrasonic sensor with node-red
    • IoT Cloud
      • Overview
        • How do they work?
          • Basic Knowlege
      • Installations
        • Install Docker
        • Install Mosquitto Broker
        • Install InfluxDB
        • Install Telegraf
        • Install Grafana
      • Get Sensor Value and Send to MQTT
        • Connect ESP3266 to sensor
        • Connect ESP3266 to MQTT
      • Integration
    • Senses IoT
      • SENSES IoT Platform
      • LAB8: MCU send data to IoT platform
    • CrowPi Dev Kit
      • Raspberry Pi with CrowPi
      • Remote to Raspberry Pi
      • Cross-Compile
        • Lab 1: Programming and cross complier
      • Hardware and Interfaces Usage CLI
        • LAB: Usage GPIO via CLI
        • LAB: Scan I2C bus via CLI
      • Python library for Crow Pi
      • wiringPi library (C) for CrowPi
        • Lab2: Crowpi and sensors
    • LVGL Development
      • LVGL - Light and Versatile Embedded Graphics Library
        • Setting program for LVGL Simulator
        • Get started with LVGL simulator
        • Example Library of LVGL
        • Create your own screen
          • Exercise
        • Style
          • Exercise
        • Event
    • Docker OS
      • Docker OS Part 1
        • Part 1 : Installation
        • Part 2 : Basic Docker OS and Linux CLI
      • Docker OS Part 2
        • Part 1 : Docker communication
        • Part 2 : Docker compose
      • Application Gstreamer on devcontainer
        • STEP 1 : Setting gstreamer environment
        • STEP 2 : Create the Gstreamer element on template
        • STEP 3 : Testing and application on your gst element
  • 🤟Recommended by AIC
    • Skill Roadmap
      • Embedded Engineer
      • Developer
    • Hardware Programming
    • Embedded Programming
    • General-propose Programming
    • Algorithmica
    • Thai Expert Knowledge
    • RT-Thread University Program
      • Infineon PSoC6
      • Kernel
        • Kernel Basics
        • Thread Management
        • Clock Management
        • Inter-thread synchronization
        • Inter-thread communication
        • Memory Management
        • Interrupt Management
        • Kernel porting
        • Atomic Operations
        • RT-Thread SMP
        • Kernel API Changelog
      • Tools
      • Devices & Drivers
        • SENSOR Devices
        • Touch Equipment
        • CRYPTO Devices
        • AUDIO Devices
        • Pulse Encoder Devices
      • Components
        • C Library (libc)
        • ISO/ANSI C Standard
        • POSIX Standard
          • FILE (File IO)
          • Pthread
          • Timer
          • IPC Semaphore
          • IPC Message Queues
          • Dynamic Modules
        • Network Components
          • FinSH Console
          • FAL: Flash Abstraction Layer
          • Virtual File System
          • tmpfs: temporary file system
          • ulog log
          • utest testing framework
          • Power Management
          • RT-Link
        • Software Packages
          • Internet of Things
            • MQTT-umqtt
            • Telnet
          • Tools
            • SystemView
            • SEGGER_RTT
          • LVGL Manual
            • Touch Screen Driver
      • Demo
        • Infineon Gateway
        • Handwriting Recognition (MNIST)
        • Object Detection (Darknet)
        • ROS using RT-Thread
        • Control the car using RT-Thread
        • LiDAR via RT-Thread
        • Detection via RT-Thread and ROS
        • Sensor Driver Development Guide
Powered by GitBook

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University

On this page
  • Command Sequences
  • Standard I/O
  • Redirections

Was this helpful?

  1. Operation Systems
  2. Zero to Linux Hero
  3. Anatomy of Linux System
  4. Command Line Interface (CLI)

Basic Commands

Basic Linux Command Interface

Useful shortcuts

Shortcut
คำอธิบาย

Ctrl + A

ย้าย cursor ไปที่ต้นบรรทัด

Ctrl + E

ย้าย cursor ไปที่ปลายบรรทัด

Alt + F

ขยับ cursor ไปข้างหน้าโดยข้ามไปทีละคำ (word)

Alt + B

ขยับ cursor ถอยหลังโดยข้ามไปทีละคำ (word)

Ctrl + L

ล้างหน้าจอ terminal คล้ายกับคำสั่ง clear

Ctrl + R

ค้นหาคำสั่งที่เคยพิมพ์มาก่อนหน้านี้

Ctrl + C

หยุดการทำงานของโปรแกรม

Ctrl + D

logout หรือออกจากคำสั่งที่กำลังทำงานอยู่

Ctrl + Z

หยุดพักคำสั่งชั่วคราว

Basic Commands

ชุดคำสั่ง
คำอธิบาย
ตัวอย่าง

mkdir

ใช้สร้างไดเรกทอรี่ใหม่

mkdir (ชื่อไดเรกทอรี่)

เช่น mkdir test1

cd

การเข้าสู่ไดเรกทอรี่ที่ต้องการ

cd (ไดเรกทอรี่ที่ต้องการเข้า)

เช่น cd test1

touch

ใช้สร้างไฟล์ หรืออัพเดตเวลาการแก้ไขไฟล์ล่าสุด

touch test.txt touch -m test.txt

ls

แสดงไฟล์ที่อยู่ในไดเรกทอรี่

ls

cp

เป็นคำสั่งที่ใช้ในการคัดลอกไฟล์

(ทั้งไฟล์เดียวและหลายไฟล์)

โดยระบุ source และ target

cp (ชื่อไฟล์) (ที่อยู่ที่ต้องการคัดลอกไฟล์ไปไว้)

เช่น cp test.txt test1 คือการ copy file ชื่อ test.txt ไปไว้ในไดเรกทอรี่ test1

mv

เป็นคำสั่งที่ใช้ในการโยกย้ายไฟล์ หรือ

เปลี่ยนชื่อไฟล์

mv (ชื่อไดเรกทอรี่เดิม) (ชื่อไดเรกทอรี่ใหม่ที่ต้องการย้ายไป)

mv file.txt /test1 เปลี่ยนชื่อไฟล์ mv (ชื่อเก่า) (ชื่อใหม่) mv file.txt newfile.txt

rm

ใช้ในการลบไฟล์โดยสามารถใช้ได้ทั้ง

ไฟล์เดียวหรือหลายไฟล์

rm (ตามด้วยชื่อไฟล์ที่ต้องการลบ)

rm test1.txt

rmdir

ใช้ลบไดเรกทอรี่ ซึ่งสามารถลบได้

เฉพาะไดเรกทอรีว่างเท่านั้น

rmdir directory_name

echo

เป็นคำสั่งที่ใช้ในการแสดงข้อความใดๆ

ที่ต้องการให้ถูกปรากฏบนหน้าต่างเทอร์มินัล หรือสามารถใช้แทรกข้อความลงในไฟล์ได้

echo (ข้อความที่ต้องการแสดง)

echo Hello echo this is message >> test.txt

cat

ใช้แสดงข้อมูลที่อยู่ในไฟล์ออกมา

แสดงครั้งเดียวพร้อมกันทั้งหมด ในบางครั้งก็

ใช้ในการรวมไฟล์หลายไฟล์เข้าด้วยกันมา

เป็นไฟล์เดียว และสามารถใช้สร้างไฟล์

cat (ไฟล์.txt)

เช่น cat test.txt

clear

ล้าง terminal ให้อยู่ใน init state

clear

df

แสดง ผลได้ทั้งจำนวนพื้นที่ที่มีการใช้งาน

ไปแล้วในระบบ และพื้นที่ว่างที่สามารถใช้งาน

df -h

du

คำสั่งที่ใช้ในการตรวจสอบขนาดการใช้งาน

ไดเรกทอรีที่ชี้อยู่ (mount point) รวมถึง

ไดเรกทอรีย่อยๆลงไปจากตำแหน่งปัจจุบัน

du -h

pwd

แสดงไดเรกทอรี่ที่กำลังใช้งาน

pwd

ifconfig

ตรวจสอบว่ากำลังใช้ Network Interface

Card (NIC) หมายเลขตัวใดอยู่ เช่น eth0

หรือ eth1 เป็นต้น

ifconfig

tar

ใช้สำหรับแตกไฟล์นามสกุล tar

และบีบอัดไฟล์หรือไดเรกทอรี่ให้เป็น

ไฟล์นามสกุล tar

tar cvf (ชื่อไฟล์.tar) (ไดเรกทอรี่หรือไฟล์ที่ต้องการบีบอัด)

เช่น tar cvf test1.tar test1

คำสั่งแตกไฟล์ tar

tar xvf test1.tar

chmod

การกำหนดสิทธิ์ในการเข้าถึงไฟล์

โดยแบ่งสิทธิ์ไว้ 3 กลุ่มคือ Owner Group

publie ซึ่งจะแทนตัวเลข 0-7 ในการกำหนด

สิทธิ์ของแต่ละกลุ่ม เช่น 644 (เลขฐานแปด)หรือเทียบเท่ากับ rw-r--r-- หมายถึง เจ้าของอ่านและเขียนได้ แต่คนอื่น

ทั่วไปอ่านได้อย่างเดียว

chmod (กำหนดสิทธิ์การเข้าถึง) (ชื่อไฟล์)

เช่น chmod 644 test

uname

แสดงชื่อของระบบปฏิบัติการที่ใช้งานอยู่

uname -a

ps aux

ใช้แสดงรายการประมวลผลต่างที่กำลัง

ทำงานอยู่ของระบบแบบระเอียด

ps aux

kill

การส่งสัญญาณเข้าไปขัดจังหวะโปรเซส

เพื่อบอกกับโปรเซสตามวัตถุประสงค์ของสัญญาณ้ส่งไป สามารถดูตัวเลข process

ได้จาก คำสั่ง kill -l

kill (ตัวเลข process) (PID)

เช่น kill –9 203

zip

ใช้บีบอัดไฟล์เป็นนามสกุล zip

zip (ชื่อไฟล์.zip) ไฟล์ที่ต้องการzip

เช่น zip test.zip test

unzip

ใช้แตกไฟล์นามสกุล zip

unzip (ไฟล์.zip)

เช่น unzip test.zip

sudo su

ใช้เมื่อผู้ใช้ต้องการเข้าในฐานะผู้ดูแลระบบ

ที่เรียกว่า Superuser หรือ root

sudo su

Command Sequences

ผู้ใช้สามารถป้อนคำสั่งมากกว่าหนึ่งคำสั่งไปพร้อมกันโดยใช้ตัวดำเนินการควบคุม (Control Operators) ได้แก่ || && & ; ;; | ( ) เป็นต้น ซึ่งการเรียงชุดคำสั่งอย่างง่ายที่สุดในกรณีที่มีมากกว่า 1 คำสั่งเป็นต้นไป จะใช้เครื่องหมาย ; อัฒภาค (semicolon) shell จะรับคำสั่งและดำเนินการทีละคำสั่งตามลำดับ ในกรณีที่จะมีการตรวจสอบผลลัพธ์การทำงานของแต่ละคำสั่งว่าสำเร็จหรือล้มเหลวนั้น ระบบปฏิบัติการลีนุกซ์จะมีการคืนค่ากลับคือถ้าคืนค่ากลับมาเลขศูนย์จะหมายถึงคำสั่งดำเนินการสำเร็จ แต่ถ้าเป็นตัวเลขอื่นๆจะถือว่าคำสั่งนั้นทำงานล้มเหลว ดังนั้นผู้ใช้สามารถกำหนดเส้นทางการทำงานตามเงื่อนไขที่ออกมาโดยใช้ตัวดำเนินการ AND && และ OR || ตัวอย่างเช่น

$ ls
android  ccache     Downloads  kernel   output.txt  Templates  workspace
aosp     Desktop    error.log  Music    Pictures    test.txt
bin      Documents  git        one.txt  Public      Videos

$ ls test.txt && echo "OK... File exists"
test.txt
OK... File exists

$ ls mail.txt && echo "OK... File exists"
ls: cannot access mail.txt: No such file or directory

จากคำสั่งข้างต้น ไฟล์ test.txt มีอยู่ในไดเรกทอรีจึงทำให้ระบบปฏิบัติการลีนุกซ์คืนค่ากลับมาเป็นศูนย์ คำสั่งถัดไปจึงทำงานต่อได้ แต่ในขณะที่ไฟล์ mail.txt ไม่ได้อยู่ในไดเรกทอรี แสดงถึงการทำงานล้มเหลว ทำให้ไม่มีการทำคำสั่งตัวถัดไป แต่หากใช้ตัวดำเนินการ || คำสั่งถัดมาจะถูกทำงานในกรณีที่คำสั่งแรกมีการคืนค่ากลับมาไม่เท่ากับศูนย์ ดังตัวอย่างข้างล่าง

$ ls test.txt || echo "OK... File exists"
test.txt

$ ls mail.txt || echo "No File exists"
ls: cannot access mail.txt: No such file or directory
ON File exists

แต่หากต้องการนำตัวดำเนินการ && และ || มาประยุกต์ใช้ในการตรวจสอบเงื่อนไขเหมือนชุดคำสั่ง if (เงื่อนไข) ..(จริง).. else ..(เท็จ).. หรือ if (เงื่อนไข) ? ..(จริง).. : ..(เท็จ).. ในภาษาโปรแกรมทั่วไป จะมีรูปแบบการเขียนดังนี้

$ ls test.txt && echo "OK...File exists" || echo "Oh Bad... File not found"
test.txt
OK...File exists

$ ls mail.txt && echo "OK...File exists" || echo "Oh Bad... File not found"
ls: cannot access mail.txt: No such file or directory
Oh Bad... File not found

Standard I/O

รายละเอียดขบวนการทำงานของ Shell ในระบบปฏิบัติการลีนุกซ์ จะมีพื้นฐานสำคัญคือ วิธีการเชื่อมต่อระหว่างโปรแกรมและสิ่งแวดล้อมของตัวโปรแกรมเองภายในเทอร์มินัล (Terminal) ที่เรียกว่า I/O ซึ่งรูปข้างล่างนี้เป็นการแสดงการเชื่อมต่อของอุปกรณ์ I/O มาตราฐานพื้นฐานของระบบที่มีอยู่ 3 ชนิด ได้แก่

ตารางรายละเอียดพื้นฐานของ Standard I/O

Stream มาตราฐาน
FD
รายละเอียด

stdin (Standard Input Stream)

0

สำหรับรับคำสั่งจากผู้ใช้เพื่อส่งต่อให้โปรแกรม อาทิเช่นการรับข้อมูลคำสั่งจากการกดคีย์บอร์ด

stdout (Standard Output)

1

สำหรับแสดงผลลัพธ์ที่ถูกส่งออกมาจากโปรแกรม เพื่อส่งข้อความผลลัพธ์ออกมาแสดงบนจอภาพ

stderr (Standard Error)

2

สำหรับแสดงผลความผิดพลาดเกิดจากการทำงานของโปรแกรมที่รับคำสั่งมาประมวลผล ออกจากหน้าจอภาพ

Redirections

ระบบปฏิบัติการลีนุกซ์นั้นได้เตรียมเครื่องมือตัวดำเนินการที่สามารถควบคุมกลไกการไหลของข้อมูลจากทิศทางหนึ่งไปยังอีกทิศทางหนึ่งหรืออธิบายง่ายๆคือการเปลี่ยนเส้นทางข้อมูลว่าจะให้ออกตัว standard stream ตัวใด โดยการใช้ตัวดำเนินการ < แทน stdin (Standard Input) และ > แทน stdout (Standard Output) ซึ่งตัวดำเนินการ redirection นั้นมีอยู่ด้วยกัน 5 แบบตามรายละเอียดในตารางข้างล่าง

ตารางตัวดำเนินการ redirection

ตัวดำเนินการ
รายละเอียด

< ไฟล์

เปิดไฟล์สำหรับอ่านข้อมูลภายในไฟล์

<< token

ใช้ในกรณีที่เป็นคำสั่งหรือเชลล์สคริปท์ที่ต้องการรับค่าจนกระทั่งเจอ token

> ไฟล์

เปิดไฟล์สำหรับเขียนทับข้อมูลใหม่

>> ไฟล์

เปิดไฟล์สำหรับเขียนต่อท้ายจากข้อมูลเดิม

n>&m

เปลี่ยนเส้นทางของ File Descriptor (FD) เดิม n ไปที่ใหม่ m

ยกตัวอย่างเช่น การใช้งานการส่งอินพุทจากไฟล์ /etc/passwd ให้กับคำสั่ง grep และการส่งอินพุทจากการป้อนข้อความให้กับคำสั่ง sort ดังแสดงข้างล่าง

$ grep -i student < /etc/passwd
student:x:1000:1000:EE-Burapha Student,,,:/home/student:/bin/bash

$ sort << END
> 55233424 Wiroon Sriborrirux
> 55237346 Nayot Kurukitkoson
> 55236477 Panuwat Dankhang
> END
55233424 Wiroon Sriborrirux
55236477 Panuwat Dankhang
55237346 Nayot Kurukitkoson
$ sort -k2 << END
> 55233424 Wiroon Sriborrirux
> 55237346 Nayot Kurukitkoson
> 55236477 Panuwat Dankhang
> END
55237346 Nayot Kurukitkoson
55236477 Panuwat Dankhang
55233424 Wiroon Sriborrirux

เมื่อมีการใช้ตัวดำเนินการ > ผลลัพธ์จากคำสั่งจะถูกส่งไปเก็บไว้ในไฟล์ /tmp/results แทนที่จะออกหน้าจอ ดังตัวอย่างข้างล่าง

$ grep -i student /etc/passwd > /tmp/results

$ ls /tmp/
CRX_75DAF8CB7768    orbit-student       ssh-pqdoFN1336  vmware-root-2
hsperfdata_student  pulse-Xti8iSZ9STOh  VMwareDnD       vmware-student
keyring-27UIsq      results             vmware-root

$ cat /tmp/results 
student:x:1000:1000:EE-Burapha Student,,,:/home/student:/bin/bash

ตัวอย่างการใช้คำสั่ง cat เพื่อแสดงข้อมูลภายในไฟล์ จะมีการแสดงข้อความผิดพลาด (Error message) ออกทางหน้าจอ ถ้าไฟล์นั้นไม่มีอยู่ในไดเรกทอรี

$ cat one.txt two.txt 2>&1
This is the data inside
cat: two.txt: No such file or directory

สามารถกรองข้อความผิดพลาดให้ไปเก็บไว้ในไฟล์ชื่อ error.log ด้วยตัวดำเนินการ 2>

$ cat one.txt two.txt 2> error.log
This is the data inside

ในกรณีที่ไม่ต้องการเก็บข้อความผิดพลาด สามารถทำได้โดยส่งไปให้ไฟล์ชื่อ /dev/null

$ cat one.txt two.txt 2> /dev/null
This is the data inside

บันทึกข้อความผิดพลาดเพิ่มต่อเข้าไปในไฟล์ error.log ด้วยตัวดำเนินการ 2>>

$ cat one.txt two.txt three.txt 2>> error.log
This is the data inside

$ cat error.log 
cat: two.txt: No such file or directory
cat: two.txt: No such file or directory
cat: three.txt: No such file or directory

Last updated: July 2023

Authors:

  • Assoc. Prof. Wiroon Sriborrirux

  • Thanaluk Pranekunakol (AIC-Researcher), Waratith Sawangboon (AIC-Researcher)

PreviousCommand Line Interface (CLI)NextLevel up your Linux Shell

Last updated 1 year ago

Was this helpful?

🖥️