HasteBin0

Bash script or Python script file creation automation V1

Jun 9th, 2024 (edited)
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.67 KB | Source Code | 0 0
  1. #!/usr/bin/sh
  2.  
  3. help() {
  4.     cat << EOF
  5. Usage: make_new_cwd_script.sh [filename] [extension] [permissions] [editor]
  6.  
  7. The script creates a new shell or Python script file with the specified name and opens it in the chosen editor.
  8.  
  9. Arguments:
  10.   filename    The name of the file to be created. Must contain only alphanumeric characters, underscores, and periods.
  11.   extension   The file extension indicating the script type. Acceptable values are .sh for shell scripts and .py for Python scripts. Defaults to .sh if not provided.
  12.   permissions The file permissions in octal format (e.g., 755). Defaults to 755 if not provided.
  13.   editor      The text editor to open the file with. Acceptable values are: nano, vi, vim, gedit, vscode. Defaults to nano if not provided.
  14.  
  15. Examples:
  16.   make_new_cwd_script.sh myscript .sh 755 nano    # Creates a shell script named myscript.sh with permissions 755 and opens it in nano.
  17.   make_new_cwd_script.sh myscript .py 644 gedit   # Creates a Python script named myscript.py with permissions 644 and opens it in gedit.
  18.  
  19. Help Options:
  20.   -?        Show the short help message.
  21.   -h        Show the short help message.
  22.   --help    Show the full help message.
  23.   --man     Show the full help message.
  24.  
  25. Error Messages:
  26.   "Error: Invalid filename format"  - The filename contains invalid characters. Only alphanumeric characters, underscores, and periods are allowed. (Exit Code: 1)
  27.   "Error: [filename] exists"        - A file with the specified name already exists in the current directory. (Exit Code: 2)
  28.   "Unknown editor specified. Please use nano, vi, vim, gedit, or vscode."  - The specified editor is not recognized. Only nano, vi, vim, gedit, and vscode are accepted. (Exit Code: 3)
  29. EOF
  30. }
  31.  
  32. # Display help if requested
  33. if [[ "$1" == "-?" || "$1" == "-h" ]]; then
  34.     help 0
  35.     exit 0
  36. elif [[ "$1" == "--help" || "$1" == "--man" ]]; then
  37.     help 1
  38.     exit 0
  39. fi
  40.  
  41. # Ensure filename is provided
  42. if [[ -z "$1" ]]; then
  43.     echo "Error: Filename is required"
  44.     exit 1
  45. fi
  46.  
  47. # Check if the name contains only alphanumeric characters, underscores, and periods
  48. if [[ ! "$1" =~ ^[[:alnum:]_.]+$ ]]; then
  49.     echo "Error: Invalid filename format"
  50.     exit 1
  51. fi
  52.  
  53. # Store the filename by appending the appropriate extension based on the script type
  54. name="$1"
  55. extension="${2:-.sh}"  # Default to .sh if not specified
  56. perm="${3:-755}"  # Default permissions to 755 if not specified
  57. editor="${4:-nano}"  # Default editor to nano if not specified
  58.  
  59. # Determine the extension based on the desired script type
  60. case "$extension" in
  61.     .sh|.py) ;;
  62.     *)
  63.         echo "Error: Invalid extension. Use .sh or .py."
  64.         exit 1
  65.         ;;  # Exit if invalid extension is provided
  66. esac
  67.  
  68. # Append the extension if it's not already present
  69. if [[ ! "$name" =~ \.${extension#*.}$ ]]; then
  70.     name="$name$extension"
  71. fi
  72.  
  73. # Check if a file with the same name already exists
  74. if [ -e "$name" ]; then
  75.     echo "Error: $name exists"
  76.     exit 2
  77. fi
  78.  
  79. # Validate the editor before proceeding
  80. case "$editor" in
  81.     nano|vi|vim|gedit|vscode) ;;
  82.     *)
  83.         echo "Unknown editor specified. Please use nano, vi, vim, gedit, or vscode."
  84.         exit 3
  85.         ;;
  86. esac
  87.  
  88. # Create the file with the specified name, including the shebang line
  89. if [ "$extension" == ".sh" ]; then
  90.     printf '%s\n\n' '#!/usr/bin/sh' > "$name"
  91. else
  92.     printf '%s\n\n' '#!/usr/bin/python3' > "$name"
  93. fi
  94.  
  95. # Change ownership and permissions of the file
  96. chmod "$perm" "$name"
  97.  
  98. # Open the file for editing using the specified text editor
  99. case "$editor" in
  100.     nano) nano "$name" ;;
  101.     vi) vi "$name" ;;
  102.     vim) vim "$name" ;;
  103.     gedit) gedit "$name" & ;;
  104.     vscode) code "$name" ;;
  105. esac
  106.  
Advertisement
Add Comment
Please, Sign In to add comment