• 2 Posts
  • 4 Comments
Joined 1 year ago
cake
Cake day: June 13th, 2023

help-circle

  • I guess I’ll post it here then:

    #!/bin/bash
    
    # Function to display the menu
    display_menu() {
      echo "Select an option:"
      echo "1. Move all .LRV files to 'LRV' directory"
      echo "2. Undo move operation"
      echo "3. Rename files starting with 'GL' to 'GX'"
      echo "4. Undo renaming operation for .LRV files"
      echo "5. Exit"
      read -p "Enter choice (1, 2, 3, 4, or 5): " choice
    }
    
    # Function to handle option 1
    move_lrv_files() {
      # Create the LRV directory
      mkdir "LRV"
    
      # Move all the .LRV files to the LRV directory
      mv *.LRV "LRV"
      echo "Moved all .LRV files to 'LRV' directory"
    }
    
    # Function to handle option 2
    undo_move_operation() {
      # Move all .LRV files back to the working directory
      mv LRV/*.LRV .
    
      # Remove the LRV directory
      rmdir LRV
      echo "Undid move operation"
    }
    
    # Function to handle option 3
    rename_files() {
      # Rename files starting with 'GL' to 'GX'
      for file in GL*; do
        newname="${file/GL/GX}"
        mv "$file" "$newname"
        echo "Renamed file '$file' to '$newname'"
      done
    }
    
    # Function to handle option 4
    undo_rename_files() {
      # Undo renaming operation for .LRV files (revert files from 'GX' to 'GL')
      for file in GX*.LRV; do
        newname="${file/GX/GL}"
        mv "$file" "$newname"
        echo "Renamed file '$file' to '$newname'"
      done
    }
    
    # Main script
    if [[ $1 == "-d" ]]; then
      # Use directory provided as command-line argument
      directory=$2
    else
      # Ask user for the working directory
      read -p "Please enter the path of the working directory: " directory
    fi
    
    # Change the working directory and check if it was successful
    if cd "$directory"; then
      while true; do
        display_menu
    
        # Check user choice and execute the appropriate function
        case $choice in
          1)
            move_lrv_files
            ;;
          2)
            undo_move_operation
            ;;
          3)
            rename_files
            ;;
          4)
            undo_rename_files
            ;;
          5)
            echo "Exiting..."
            break
            ;;
          *)
            echo "Invalid choice"
            ;;
        esac
    
        echo
      done
    else
      # Error message if cd command fails
      echo "Error: Invalid path. Please double check the working directory."
    fi