Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sure, here's an explanation of each line:
- 1. `#!/bin/bash`: This line specifies the interpreter to be used for executing the script, which in this case is Bash.
- 2. `set -x`: This line enables debugging mode in the script, causing each command to be printed to the terminal before it is executed.
- 3. `read -p 'Enter swap size in megabytes: ' size_mb`: This line prompts the user to enter the size of the swap space in megabytes and stores the input in the variable `size_mb`.
- 4. `size_kb=$((1024*${size_mb}))`: This line calculates the size of the swap space in kilobytes based on the value entered by the user (stored in `size_mb`) and stores it in the variable `size_kb`.
- 5. `dd if=/dev/zero of=/swap bs=1024 count=${size_kb}`: This line creates a swap file named `/swap` with the size specified by `size_kb` using the `dd` command, which reads from `/dev/zero` and writes to `/swap`.
- 6. `chmod 0600 /swap`: This line sets the permissions of the swap file `/swap` to read and write for the owner only.
- 7. `mkswap /swap`: This line initializes the swap file `/swap` to be used as swap space.
- 8. `swapon /swap`: This line activates the swap space defined by the file `/swap`.
- 9. `if [ "$(grep '/swap' /etc/fstab)" ]; then`: This line checks if the file `/etc/fstab` already contains a line defining the swap space.
- 10. `echo "Error: file /etc/fstab already has 'Swap' record"`: This line prints an error message if the swap space is already defined in `/etc/fstab`.
- 11. `else`: Indicates the start of the `else` block.
- 12. `echo "Add Swap record to /etc/fstab"`: This line prints a message indicating that a swap record is being added to `/etc/fstab`.
- 13. `echo -e '\n/swap swap swap defaults 0 0' >> /etc/fstab`: This line appends a line to the file `/etc/fstab` defining the swap space `/swap` with default options.
- 14. `fi`: Indicates the end of the `if` block.
- 15. `swapon --show`: This line displays the currently active swap spaces.
Advertisement
Add Comment
Please, Sign In to add comment