Advertisement
SkidScripts

Untitled

Nov 29th, 2023
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.41 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Function to check if a file exists
  4. check_file_exists() {
  5.     if [ -f "$1" ]; then
  6.         echo "File '$1' exists."
  7.     else
  8.         echo "File '$1' does not exist."
  9.     fi
  10. }
  11.  
  12. # Function to count the number of lines in a file
  13. count_lines() {
  14.     local lines=$(wc -l < "$1")
  15.     echo "Number of lines in '$1': $lines"
  16. }
  17.  
  18. # Function to create a new file
  19. create_file() {
  20.     echo "Enter text to write into the file (press CTRL+D to save):"
  21.     cat > "$1"
  22.     echo "File '$1' created with the entered text."
  23. }
  24.  
  25. # Main menu
  26. while true; do
  27.     echo "===== Bash Script Menu ====="
  28.     echo "1. Check if a file exists"
  29.     echo "2. Count the number of lines in a file"
  30.     echo "3. Create a new file"
  31.     echo "4. Exit"
  32.  
  33.     read -p "Enter your choice [1-4]: " choice
  34.  
  35.     case $choice in
  36.         1)
  37.             read -p "Enter the filename to check: " filename
  38.             check_file_exists "$filename"
  39.             ;;
  40.         2)
  41.             read -p "Enter the filename to count lines: " filename
  42.             count_lines "$filename"
  43.             ;;
  44.         3)
  45.             read -p "Enter the filename to create: " filename
  46.             create_file "$filename"
  47.             ;;
  48.         4)
  49.             echo "Exiting the script. Goodbye!"
  50.             exit 0
  51.             ;;
  52.         *)
  53.             echo "Invalid choice. Please enter a valid option [1-4]."
  54.             ;;
  55.     esac
  56.     echo
  57. done
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement