බැෂ් පාලනය: ලිනක්ස් පරිශීලකයන් සඳහා අත්‍යවශ්‍ය විධාන, ස්ක්‍රිප්ට් මූලිකතා, සහ ස්වයංක්‍රීය තාක්ෂණ

1. Bash මූලික කරුණු

Bash ශෙල් එක කුමක්ද?

Bash (Bourne Again Shell) යනු Linux බෙදාහැරීම් වල සාමාන්‍යයෙන් භාවිතා වන කමාන්ඩ්-ලයින් අතුරුමුහුණතයි. මෙම සරල නමුත් බලවත් මෙවලම පරිශීලකයන්ට පද්ධතිය සමඟ අන්තර්ක්‍රියා කිරීමට වේදිකාවක් සපයයි, ගොනු සැකසීම, වැඩසටහන් ක්‍රියාත්මක කිරීම, සහ කාර්ය කළමනාකරණය වැනි මූලික කාර්යයන් සිදු කිරීමට ඉඩ දේ.

Bash හි වාසි

  • බලවත් ස්ක්‍රිප්ට් කිරීමේ හැකියාව : Bash පරිශීලකයන්ට ශෙල් ස්ක්‍රිප්ට් භාවිතයෙන් සංකීර්ණ කාර්යයන් ස්වයංක්‍රීය කිරීමට ඉඩ සලසයි.
  • විශාල සහය : එය බොහෝ Unix-ආශ්‍රිත මෙහෙයුම් පද්ධති සහ Linux බෙදාහැරීම් වල සහය දක්වයි.
  • ඉහළ අභිරුචිකරණය : අලියාස් සහ ශෙල් ක්‍රියාකාරකම් සමඟ, පරිශීලකයන්ට තම පරිසරය තම වැඩපිළිවෙළට ගැළපෙන ලෙස සැකසිය හැක.
    # Simple Bash command example
    echo "Hello, World!"
    

2. අත්‍යවශ්‍ය Bash විධාන

ගොනු මෙහෙයුම්

Bash හි බොහෝ වරක් භාවිතා වන ගොනු මෙහෙයුම් විධාන කිහිපයක් මෙහි ඇත.

  • ls : නාමාවලියක අන්තර්ගතයන් ලැයිස්තුගත කරයි.
  • cd : වත්මන් නාමාවලිය වෙනස් කරයි.
  • cp : ගොනු හෝ නාමාවලියන් පිටපත් කරයි.
  • mv : ගොනු ගෙන යාම හෝ නාමය වෙනස් කිරීම සිදු කරයි.
  • rm : ගොනු මකා දමයි.
    # Display directory contents in detail
    ls -l
    
    # Move to the home directory
    cd ~
    
    # Copy a file
    cp source.txt destination.txt
    
    # Move or rename a file
    mv old_name.txt new_name.txt
    
    # Delete a file
    rm unwanted_file.txt
    

පද්ධති තොරතුරු සහ ක්‍රියාවලී කළමනාකරණය

පද්ධති තොරතුරු පරීක්ෂා කිරීම සහ ක්‍රියාවලීන් කළමනාකරණය කිරීම සඳහා විධානද අත්‍යවශ්‍ය වේ.

  • ps : ක්‍රියාත්මක ක්‍රියාවලීන් පෙන්වයි.
  • top : ක්‍රියාවලීන්ගේ රියල්-ටයිම් ලැයිස්තුව සහ පද්ධති සාරාංශය පෙන්වයි.
  • kill : ක්‍රියාවලියක් අවසන් කිරීමට සංඥා යවයි.
    # Display active processes
    ps aux
    
    # Show system overview and process list
    top
    
    # Terminate a process with ID 1234
    kill 1234
    

3. Bash ස්ක්‍රිප්ට් ලිවීම

මූලික ස්ක්‍රිප්ට් ව්‍යුහය

Bash ස්ක්‍රිප්ට් යනු බහු විධාන අඩංගු ගොනුවකි. ස්ක්‍රිප්ට් සෑදීමෙන් පරිශීලකයන්ට මෙහෙයුම් කට්ටලයක් ස්වයංක්‍රීය කර ක්‍රියාත්මක කළ හැක.

#!/bin/bash
# This line is called a shebang and specifies the shell used to interpret the script.

echo "Hello, World!"  # Display a string using the echo command

විචල්‍ය භාවිතය

විචල්‍යයන් ඔබට දත්ත ගබඩා කර ඔබේ ස්ක්‍රිප්ට් තුළ නැවත භාවිතා කිරීමට ඉඩ සලසයි.

#!/bin/bash
message="Hello, Bash Scripting!"
echo $message

කොන්දේසි ප්‍රකාශන සහ ලූප

සංකීර්ණ තර්ක සහ නැවත නැවත සිදු වන කාර්යයන් සම්බන්ධයෙන් කොන්දේසි ප්‍රකාශන සහ ලූප භාවිතා කරන්න.

#!/bin/bash
# Example of an if statement
if [ $1 -gt 100 ]
then
  echo "The number is greater than 100."
else
  echo "The number is 100 or less."
fi

# Example of a for loop
for i in 1 2 3 4 5
do
  echo "Looping ... number $i"
done

4. Bash සමඟ කාර්ය ස්වයංක්‍රීය කිරීම

කාර්ය ස්වයංක්‍රීය කිරීමේ සමාලෝචනය

Bash ස්ක්‍රිප්ට් මඟින් නැවත නැවත සිදු වන කාර්යයන් කාර්යක්ෂමව ස්වයංක්‍රීය කිරීමේ හැකියාව ලැබේ. පද්ධති බැකප් කිරීම, දත්ත සමමුහුර්ත කිරීම, හෝ වාර්තා සකස් කිරීම වැනි කාර්යයන් සිදු කරන විට, Bash පද්ධති පරිපාලනයේ බර අඩු කරයි.

ස්වයංක්‍රීය බැකප් ස්ක්‍රිප්ට්

පහත ස්ක්‍රිප්ට් එක නියමිත නාමාවලියක් දිනපතා දත්ත ආරක්ෂාව සඳහා නිතර බැකප් කරයි.

#!/bin/bash
SRC_DIR="/home/user/documents"
DST_DIR="/backup/documents"
DATE=$(date +%Y%m%d)

# Create backup directory if it does not exist
if [ ! -d "$DST_DIR" ]; then
  mkdir -p "$DST_DIR"
fi

# Compress and back up directory contents
tar -czf "$DST_DIR/backup_$DATE.tar.gz" -C "$SRC_DIR" .
echo "Backup completed successfully."

cron සමඟ ස්ක්‍රිප්ට් ස්වයංක්‍රීයව ධාවනය කිරීම

cron භාවිතයෙන් බැකප් ස්ක්‍රිප්ට් එක දිනපතා පෙ.ව. 2:00 ට ධාවනය කරන්න.

0 2 * * * /path/to/backup.sh

දෝෂ හසුරුවීම සහ දැනුම්දීම

මෙම ස්ක්‍රිප්ට් එක දෝෂ හසුරුවීම ඇතුළත් කර ඇති අතර, බැකප් ක්‍රියාවලිය අතරතුර ගැටළුවක් සිදුවේ නම් පරිපාලකයාට දැනුම් දෙයි.

#!/bin/bash
SRC_DIR="/home/user/documents"
DST_DIR="/backup/documents"
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y%m%d)

if [ ! -d "$DST_DIR" ]; then
  mkdir -p "$DST_DIR"
fi

if tar -czf "$DST_DIR/backup_$DATE.tar.gz" -C "$SRC_DIR" .; then
  echo "Backup successful on $DATE" >> $LOG_FILE
else
  echo "Backup failed on $DATE" | mail -s "Backup Failure" admin@example.com
fi

5. Troubleshooting and Common Errors

Understanding and Resolving Bash Errors

It is common for errors to occur when executing Bash scripts. Here are some frequent errors and how to resolve them.

Command Not Found Error

This occurs when the command you are trying to run is not installed or the path is not configured correctly.

command not found
  • Solution : Ensure the command is installed and verify that the $PATH environment variable is properly configured.

Permission Error

This error occurs when you lack the necessary permissions to access a file or directory.

Permission denied
  • Solution : Run the command with a user who has the required permissions, or modify permissions using chmod or chown .

Syntax Error

This error occurs when the script contains improperly formatted code.

syntax error: unexpected end of file
  • Solution : Carefully review the script and correct any syntax issues.

File Not Found Error

This error occurs when a specified file does not exist.

No such file or directory
  • Solution : Verify the path and ensure the file exists.

Using Debugging Tools

The set -x command is useful when debugging Bash scripts. It displays each step as the script executes, making it easier to identify the cause of an error.

set -x  # Enable script debugging
侍エンジニア塾