mirror of https://github.com/nirenjan/dotfiles.git
				
				
				
			
		
			
				
	
	
		
			141 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			141 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
| #!/bin/bash
 | |
| # Script to create a new shell script and open it for editing
 | |
| 
 | |
| APP=$(basename $0)
 | |
| 
 | |
| #######################################################################
 | |
| # Set default options
 | |
| #######################################################################
 | |
| NEWSH_SCRIPT_TYPE=bash
 | |
| NEWSH_USE_ABS_PATH=0
 | |
| NEWSH_NO_EXEC_FLAG=0
 | |
| NEWSH_TRUNC_SCRIPT=0
 | |
| NEWSH_SCRIPT_FILE=
 | |
| 
 | |
| #######################################################################
 | |
| # Usage information
 | |
| #######################################################################
 | |
| usage()
 | |
| {
 | |
|     cat <<-EOM
 | |
| Usage: $APP OPTIONS <new-shell-script-filename>
 | |
| 
 | |
| OPTIONS
 | |
|     -t <type>       Type of script to create (bash/python/ruby/expect)
 | |
|     -a              Use absolute path to binary instead of the env wrapper
 | |
|     -x              Don't make the file executable
 | |
|     -f              Overwrite the script file if it already exists
 | |
|     -h              Display this help message
 | |
| 
 | |
| $APP will abort if the specified filename already exists
 | |
| EOM
 | |
| }
 | |
| 
 | |
| #######################################################################
 | |
| # Get path to binary
 | |
| #######################################################################
 | |
| get_binary_path()
 | |
| {
 | |
|     local binary=$1
 | |
|     local env_path=$(which env)
 | |
|     local bin_path=$(which $binary)
 | |
| 
 | |
|     if [[ -z "$bin_path" ]]
 | |
|     then
 | |
|         echo "$APP: fatal: Cannot find $binary" >&2
 | |
|         exit 1
 | |
|     fi
 | |
| 
 | |
|     case "$NEWSH_USE_ABS_PATH" in
 | |
|     0)
 | |
|         # Use env as the path specifier
 | |
|         NEWSH_BINARY_PATH="$env_path $binary"
 | |
|         ;;
 | |
| 
 | |
|     1)
 | |
|         # Use absolute path to the binary as the path specifier
 | |
|         NEWSH_BINARY_PATH="$bin_path"
 | |
|         ;;
 | |
| 
 | |
|     *)
 | |
|         echo "$APP: fatal: Corrupted internal state!" >&2
 | |
|         echo "$APP: USE_ABS_PATH=$NEWSH_USE_ABS_PATH" >&2
 | |
|         exit 2
 | |
|         ;;
 | |
|     esac
 | |
| }
 | |
| 
 | |
| 
 | |
| #######################################################################
 | |
| # Verify command line switches
 | |
| #######################################################################
 | |
| while getopts "t:axfh" OPTION
 | |
| do
 | |
|     case "$OPTION" in
 | |
|     h)
 | |
|         usage
 | |
|         exit 0
 | |
|         ;;
 | |
| 
 | |
|     t)
 | |
|         NEWSH_SCRIPT_TYPE=$OPTARG
 | |
|         ;;
 | |
| 
 | |
|     a)
 | |
|         NEWSH_USE_ABS_PATH=1
 | |
|         ;;
 | |
| 
 | |
|     x)
 | |
|         NEWSH_NO_EXEC_FLAG=1
 | |
|         ;;
 | |
| 
 | |
|     f)
 | |
|         NEWSH_TRUNC_SCRIPT=1
 | |
|         ;;
 | |
| 
 | |
|     \?)
 | |
|         exit 1
 | |
|         ;;
 | |
| 
 | |
|     :)
 | |
|         echo "$APP: Option -$OPTARG requires an argument" >&2
 | |
|         exit 1
 | |
|         ;;
 | |
|     esac
 | |
| done
 | |
| 
 | |
| shift $((OPTIND - 1))
 | |
| NEWSH_SCRIPT_FILE=$1
 | |
| 
 | |
| if [[ -z "$NEWSH_SCRIPT_FILE" ]]
 | |
| then
 | |
|     echo "$APP: fatal: Missing script file!" >&2
 | |
|     exit 1
 | |
| fi
 | |
| 
 | |
| if [[ -e "$NEWSH_SCRIPT_FILE" ]]
 | |
| then
 | |
|     if [[ "$NEWSH_TRUNC_SCRIPT" == "0" ]]
 | |
|     then
 | |
|         echo "$APP: fatal: Existing script file $NEWSH_SCRIPT_FILE" >&2
 | |
|         exit 1
 | |
|     elif [[ "$NEWSH_TRUNC_SCRIPT" == "1" ]]
 | |
|     then
 | |
|         echo "$APP: overwriting existing script file $NEWSH_SCRIPT_FILE" >&2
 | |
|     fi
 | |
| fi
 | |
| 
 | |
| get_binary_path $NEWSH_SCRIPT_TYPE
 | |
| (cat <<-EOF
 | |
| #!$NEWSH_BINARY_PATH
 | |
| # Autogenerated by $APP on $(date +%F) at $(date +%T%z)
 | |
| 
 | |
| EOF
 | |
| ) > "$NEWSH_SCRIPT_FILE"
 | |
| 
 | |
| # Check and make file executable
 | |
| if [[ "$NEWSH_NO_EXEC_FLAG" == "0" ]]
 | |
| then
 | |
|     chmod +x "$NEWSH_SCRIPT_FILE"
 | |
| fi
 |