Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/sh
- help() {
- cat << EOF
- Usage: make_new_cwd_script.sh [filename] [extension] [permissions] [editor]
- The script creates a new shell or Python script file with the specified name and opens it in the chosen editor.
- Arguments:
- filename The name of the file to be created. Must contain only alphanumeric characters, underscores, and periods.
- 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.
- permissions The file permissions in octal format (e.g., 755). Defaults to 755 if not provided.
- editor The text editor to open the file with. Acceptable values are: nano, vi, vim, gedit, vscode. Defaults to nano if not provided.
- Examples:
- make_new_cwd_script.sh myscript .sh 755 nano # Creates a shell script named myscript.sh with permissions 755 and opens it in nano.
- make_new_cwd_script.sh myscript .py 644 gedit # Creates a Python script named myscript.py with permissions 644 and opens it in gedit.
- Help Options:
- -? Show the short help message.
- -h Show the short help message.
- --help Show the full help message.
- --man Show the full help message.
- Error Messages:
- "Error: Invalid filename format" - The filename contains invalid characters. Only alphanumeric characters, underscores, and periods are allowed. (Exit Code: 1)
- "Error: [filename] exists" - A file with the specified name already exists in the current directory. (Exit Code: 2)
- "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)
- EOF
- }
- # Display help if requested
- if [[ "$1" == "-?" || "$1" == "-h" ]]; then
- help 0
- exit 0
- elif [[ "$1" == "--help" || "$1" == "--man" ]]; then
- help 1
- exit 0
- fi
- # Ensure filename is provided
- if [[ -z "$1" ]]; then
- echo "Error: Filename is required"
- exit 1
- fi
- # Check if the name contains only alphanumeric characters, underscores, and periods
- if [[ ! "$1" =~ ^[[:alnum:]_.]+$ ]]; then
- echo "Error: Invalid filename format"
- exit 1
- fi
- # Store the filename by appending the appropriate extension based on the script type
- name="$1"
- extension="${2:-.sh}" # Default to .sh if not specified
- perm="${3:-755}" # Default permissions to 755 if not specified
- editor="${4:-nano}" # Default editor to nano if not specified
- # Determine the extension based on the desired script type
- case "$extension" in
- .sh|.py) ;;
- *)
- echo "Error: Invalid extension. Use .sh or .py."
- exit 1
- ;; # Exit if invalid extension is provided
- esac
- # Append the extension if it's not already present
- if [[ ! "$name" =~ \.${extension#*.}$ ]]; then
- name="$name$extension"
- fi
- # Check if a file with the same name already exists
- if [ -e "$name" ]; then
- echo "Error: $name exists"
- exit 2
- fi
- # Validate the editor before proceeding
- case "$editor" in
- nano|vi|vim|gedit|vscode) ;;
- *)
- echo "Unknown editor specified. Please use nano, vi, vim, gedit, or vscode."
- exit 3
- ;;
- esac
- # Create the file with the specified name, including the shebang line
- if [ "$extension" == ".sh" ]; then
- printf '%s\n\n' '#!/usr/bin/sh' > "$name"
- else
- printf '%s\n\n' '#!/usr/bin/python3' > "$name"
- fi
- # Change ownership and permissions of the file
- chmod "$perm" "$name"
- # Open the file for editing using the specified text editor
- case "$editor" in
- nano) nano "$name" ;;
- vi) vi "$name" ;;
- vim) vim "$name" ;;
- gedit) gedit "$name" & ;;
- vscode) code "$name" ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment