



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Program:- #!/bin/bash
read - p "Enter the starting number: " start read - p "Enter the ending number: " end
if [ "$start" - lt 2 ]; then start= fi echo "Prime numbers between $start and $end:" for (( num=start; num<=end; num++ )); do is_prime=
for (( i=2; i*i<=num; i++ )); do if [ $((num % i)) - eq 0 ]; then is_prime= break fi done if [ $is_prime - eq 1 ]; then echo - n "$num " fi done echo "" # Newline at the end#!/bin/bash
read - p "Enter the starting number: " start read - p "Enter the ending number: " end
if [ "$start" - lt 2 ]; then start= fi echo "Prime numbers between $start and $end:" for (( num=start; num<=end; num++ )); do is_prime=
for (( i=2; i*i<=num; i++ )); do if [ $((num % i)) - eq 0 ]; then is_prime= break fi done if [ $is_prime - eq 1 ]; then echo - n "$num " fi done echo "" # Newline at the end OUTPUT:-
Program:- #!/bin/bash
read - p "Enter a single character: " input
if [ ${#input} - ne 1 ]; then echo "Error: Please enter exactly ONE character." exit 1 fi
if [[ "$input" =~ [[:alpha:]] ]]; then echo "'$input' is an Alphabet." elif [[ "$input" =~ [[:digit:]] ]]; then echo "'$input' is a Digit." else echo "'$input' is a Special Character." Fi OUTPUT:-
Program:- #!/bin/bash
read - p "Enter a number: " num
num=${num#-}
if! [[ "$num" =~ ^[0-9]+$ ]]; then echo "Error: Please enter a valid whole number." exit 1 fi sum= temp=$num
while [ $temp - gt 0 ]; do digit=$((temp % 10)) sum=$((sum + digit)) temp=$((temp / 10)) done echo "Sum of digits of $num is: $sum" OUTPUT:-
Program:- #!/bin/bash
read - p "Enter the maximum value (N): " N
if! [[ "$N" =~ ^[0-9]+$ ]]; then echo "Error: Please enter a valid non-negative integer." fn=$((a + b)) a=$b b=$fn done
while [ $temp - gt 0 ]; do remainder=$((temp % 10)) reverse=$((reverse * 10 + remainder)) temp=$((temp / 10)) done
if [ $num - eq $reverse ]; then echo "$num is a Palindrome." else echo "$num is NOT a Palindrome." fi OUTPUT:-
Program:- #!/bin/bash
usage() { echo "Usage: $0 " echo "Example: $0 test.txt create" echo " $0 test.txt delete" }
create_file() { local file="$1" if [ - z "$file" ]; then read - p "Enter filename to create: " file fi if [ - z "$file" ]; then echo "[-] Error: Filename cannot be empty." return 1 fi if [ - e "$file" ]; then echo "[!] Warning: File '$file' already exists." else touch "$file"
if [ $? - eq 0 ]; then echo "[+] Success: File '$file' created successfully." else echo "[-] Error: Failed to create file '$file'." fi fi }
delete_file() { local file="$1" if [ - z "$file" ]; then read - p "Enter filename to delete: " file fi if [ - z "$file" ]; then echo "[-] Error: Filename cannot be empty." return 1 fi if [ - e "$file" ]; then rm - i "$file" if [! - e "$file" ]; then echo "[+] Success: File '$file' deleted." else echo "[!] Deletion canceled or failed." fi else echo "[-] Error: File '$file' does not exist." fi }
show_menu() { local target_file="$1" while true; do echo "=================================" echo " FILE MANAGER MENU " echo "=================================" echo "Target File: ${target_file:-"(None specified)"}" echo "1. Create File" echo "2. Delete File" echo "3. Exit" echo "=================================" read - p "Select an option [1-3]: " choice case $choice in
Program:- #!/bin/bash
GREEN='\033[0;32m' CYAN='\033[0;36m' RED='\033[0;31m' NC='\033[0m' # No Color
if [ - n "$1" ]; then input_str="$1" else echo - e "${CYAN}[*] Input string not provided as an argument.${NC}"
read - p "Enter string: " input_str fi
if [ - z "$input_str" ]; then echo - e "${RED}[-] Error: String cannot be empty.${NC}" exit 1 fi
str_length=${#input_str}
reversed_str=$(echo "$input_str" | rev)
echo "" echo - e "${CYAN}========== STRING ANALYSIS ==========${NC}" echo - e "Original String : ${GREEN}$input_str${NC}" echo - e "String Length : ${GREEN}$str_length${NC} characters" echo - e "Reversed String : ${GREEN}$reversed_str${NC}" echo - e "${CYAN}=====================================${NC}" OUTPUT:-
Program:-
echo "Digits : $digits" echo "Vowels : $vowels" echo "Consonants : $consonants" echo "=====================================" OUTPUT:-
Program:- #!/bin/bash
CYAN='\033[0;36m' GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' # No Color
read_number() { local prompt_msg="$1"
local var_name="$2" while true; do read - p "$prompt_msg" num
if [[ "$num" =~ ^-?[0-9]+(.[0-9]+)?$ ]]; then eval "$var_name='$num'" break else echo - e "${RED}[-] Invalid input. Please enter a valid number.${NC}" fi done } while true; do echo "" echo - e "${CYAN}=====================================${NC}" echo - e "${CYAN} MENU CALCULATOR ${NC}" echo - e "${CYAN}=====================================${NC}" echo "1. Addition (+)" echo "2. Subtraction (-)" echo "3. Multiplication (*)" echo "4. Division (/)" echo "5. Exit" echo - e "${CYAN}=====================================${NC}" read - p "Select an option [1-5]: " choice case $choice in
read_number "Enter first number: " num read_number "Enter second number: " num result=$(echo "scale=2; $num1 + $num2" | bc) echo - e "${GREEN}[+] Result: $num1 + $num2 = $result${NC}" ;;
read_number "Enter first number: " num read_number "Enter second number: " num result=$(echo "scale=2; $num1 - $num2" | bc) echo - e "${GREEN}[+] Result: $num1 - $num2 = $result${NC}" ;;
read_number "Enter first number: " num read_number "Enter second number: " num result=$(echo "scale=2; $num1 * $num2" | bc) echo - e "${GREEN}[+] Result: $num1 * $num2 = $result${NC}" ;;
if [ - n "$1" ]; then input_str="$1" else read - p "Enter a string: " input_str fi
if [ - z "$input_str" ]; then echo "[-] Error: Input string cannot be empty." exit 1 fi
swapped_str=$(echo "$input_str" | tr 'a-zA-Z' 'A-Za-z')
echo "=====================================" echo "Original String : $input_str" echo "Swapped Case : $swapped_str" echo "=====================================" OUTPUT:-
Program:- #!/bin/bash
CYAN='\033[0;36m' GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' # No Color echo - e "${CYAN}=====================================${NC}" echo - e "${CYAN} COMMAND RUNNER SHELL ${NC}" echo - e "${CYAN} Type 'exit' or 'quit' to close ${NC}" echo - e "${CYAN}=====================================${NC}" while true; do
echo "" read - p "Enter command to execute: " user_cmd
if [[ "$user_cmd" == "exit" || "$user_cmd" == "quit" ]]; then echo "Exiting command runner. Goodbye!" break fi
if [ - z "$user_cmd" ]; then echo - e "${RED}[-] Warning: No command entered.${NC}" continue fi
base_cmd=$(echo "$user_cmd" | awk '{print $1}')
if command - v "$base_cmd" > /dev/null 2>&1; then echo - e "${GREEN}[+] Executing: $user_cmd${NC}" echo "-------------------------------------"
eval "$user_cmd"
exit_code=$? echo "-------------------------------------" if [ $exit_code - eq 0 ]; then echo - e "${GREEN}[+] Command completed successfully.${NC}" else echo - e "${RED}[!] Command exited with status code $exit_code.${NC}"
echo " Reading File: $filepath" echo "==================================================" line_number=
while IFS= read - r line || [ - n "$line" ]; do echo "Line $line_number: $line" ((line_number++)) done < "$filepath" echo "==================================================" echo " Finished reading $((line_number - 1)) line(s)." echo "==================================================" OUTPUT:-
Program:- #!/bin/bash
if [ $# - eq 3 ]; then num1="$1" num2="$2" num3="$3" else echo "Please enter 3 numbers:"
read - p "Number 1: " num read - p "Number 2: " num read - p "Number 3: " num fi
for num in "$num1" "$num2" "$num3"; do if! [[ "$num" =~ ^-?[0-9]+(.[0-9]+)?$ ]]; then echo "[-] Error: '$num' is not a valid number." exit 1 fi done
if (( $(echo "$num1 >= $num2" | bc - l) )) && (( $(echo "$num1 >= $num3" | bc - l) )); then largest="$num1" elif (( $(echo "$num2 >= $num1" | bc - l) )) && (( $(echo "$num2 >= $num3" | bc
echo "=================================" echo "Numbers : $num1, $num2, $num3" echo "Largest : $largest" echo "=================================" OUTPUT:-