A collection of lightweight, efficient Bash shell scripts for performing mathematical computations, string checks, file operations, and CLI utility tasks.
These scripts have been modernized, debugged, and optimized to run seamlessly across both macOS (using default Bash 3.2) and Linux (Bash 4.0+) environments.
Here is a breakdown of every utility script included in this repository:
- Description: Generates and displays all prime numbers between
1and100. - Usage:
./100prime
- Description: Prompts the user to enter the base and height of a triangle and calculates its area. Uses the
bcutility for floating-point calculations. - Usage:
./Areatri # Sample prompt: # enter base of triangle: 5 # enter height of triangle: 4.5 # Output: 11.25
-
Description: Checks whether a user-input number is an Armstrong number (a number that is equal to the sum of its own digits each raised to the power of the number of digits, e.g.,
$1^3 + 5^3 + 3^3 = 153$ ). -
Usage:
./Armstrongno # Sample input: 153 # Output: 153 is an Armstrong number.
- Description: A terminal-based interactive calculator with a menu options layout:
a: Additions: Subtractionm: Multiplicationd: Division (includes zero-division check)q: Quit
- Usage:
./Calc
- Description: Opens a specified file in
vimonly after performing double validation checks: ensuring that the file exists and that the user has write permissions. - Usage:
./Editfile <filename>
-
Description: Generates and prints the Fibonacci series up to a user-specified number of terms (
$N$ ). -
Usage:
./Fibonacci # Sample Input: 5 # Output: 0 1 1 2 3
-
Description: Lists the first
20natural numbers (1 to 20) and calculates their total sum ($210$ ). -
Usage:
./Natsum
- Description: Case-insensitively checks if a user-provided word reads the same backward as forward.
- Usage:
./Palindrome # Sample Input: Racecar # Output: racecar is a palindrome.
-
Description: Searches and prints all perfect numbers (a positive integer that is equal to the sum of its proper divisors, e.g.,
$6 = 1 + 2 + 3$ ) between1and500. -
Usage:
./Perfectno # Output: 6, 28, 496
- Description: Checks whether a file exists locally. If it exists, it outputs the absolute, fully-resolved system path using the
realpathcommand. - Usage:
./Searchfile <filename>
- Description: Calculates the total number of digits in any integer. It supports positive numbers, negative numbers, and zero.
- Usage:
./no_of_digits # Sample Input: -9876 # Output: Number of digits: 4
-
Clone the repository:
git clone https://github.com/HypnoticShield/Shellscripts.git cd Shellscripts -
Make the scripts executable: By default, the scripts might not have execution permissions. Grant them using:
chmod +x * -
Run a script:
./Calc
To ensure these utilities run properly out-of-the-box, several bug fixes were applied:
-
Armstrongno: Fixed a bug where checking$sum -eq $numberthrew an error because$numberwas undefined. It now correctly checks$n. -
Calc: Fixed division validation which was checking if input was less than 0 rather than equal to 0. Removed a syntax-breakingcontinueoutside of a loop, replacing it with a cleanexit 1block. -
Editfile: Corrected flipped file check logic. Originally checked for writable status (-w) before confirming the file exists (-e), printing "File doesn't exist" even when a file existed but lacked write permission. -
no_of_digits: Fixed loop evaluation where an uninitialized variable$numbercaused the script to always print0digits. It now handles inputs (including negative numbers and zero) correctly. -
Palindrome: Modernized case-lowercasing. Replaced${w,,}(which fails in Bash versions older than 4.0, such as macOS default shell) withtr '[:upper:]' '[:lower:]'to ensure cross-platform compatibility.