Welcome, guest | Sign In | My Account | Store | Cart
#!/bin/bash -       
#title           :mkscript.sh
#description     :This script will make a header for a bash script.
#author		 :bgw
#date            :20111101
#version         :0.4    
#usage		 :bash mkscript.sh
#notes           :Install Vim and Emacs to use this script.
#bash_version    :4.1.5(1)-release
#==============================================================================

today=$(date +%Y%m%d)
div=======================================

/usr/bin/clear

_select_title(){

    # Get the user input.
    printf "Enter a title: " ; read -r title

    # Remove the spaces from the title if necessary.
    title=${title// /_}

    # Convert uppercase to lowercase.
    title=${title,,}

    # Add .sh to the end of the title if it is not there already.
    [ "${title: -3}" != '.sh' ] && title=${title}.sh

    # Check to see if the file exists already.
    if [ -e $title ] ; then 
        printf "\n%s\n%s\n\n" "The script \"$title\" already exists." \
        "Please select another title."
        _select_title
    fi

}

_select_title

printf "Enter a description: " ; read -r dscrpt
printf "Enter your name: " ; read -r name
printf "Enter the version number: " ; read -r vnum

# Format the output and write it to a file.
printf "%-16s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%s\n\n\n" '#!/bin/bash -' '#title' ":$title" '#description' \
":${dscrpt}" '#author' ":$name" '#date' ":$today" '#version' \
":$vnum" '#usage' ":./$title" '#notes' ':' '#bash_version' \
":${BASH_VERSION}" \#$div${div} > $title

# Make the file executable.
chmod +x $title

/usr/bin/clear

_select_editor(){

    # Select between Vim or Emacs.
    printf "%s\n%s\n%s\n\n" "Select an editor." "1 for Vim." "2 for Emacs."
    read -r editor

    # Open the file with the cursor on the twelth line.
    case $editor in
        1) vim +12 $title
            ;;
        2) emacs +12 $title &
            ;;
        *) /usr/bin/clear
           printf "%s\n%s\n\n" "I did not understand your selection." \
               "Press <Ctrl-c> to quit."
           _select_editor
            ;;
    esac

}

_select_editor

Diff to Previous Revision

--- revision 2 2011-10-02 15:48:26
+++ revision 3 2011-11-02 01:57:07
@@ -2,35 +2,42 @@
 #title           :mkscript.sh
 #description     :This script will make a header for a bash script.
 #author		 :bgw
-#date            :20110831
-#version         :0.3    
+#date            :20111101
+#version         :0.4    
 #usage		 :bash mkscript.sh
 #notes           :Install Vim and Emacs to use this script.
 #bash_version    :4.1.5(1)-release
-#===============================================================================
+#==============================================================================
 
 today=$(date +%Y%m%d)
 div=======================================
 
 /usr/bin/clear
 
-# Get the user input.
-printf "Enter a title: " ; read -r title
+_select_title(){
 
-# Remove the spaces from the title if necessary.
-title=${title// /_}
+    # Get the user input.
+    printf "Enter a title: " ; read -r title
 
-# Convert uppercase to lowercase.
-title=${title,,}
+    # Remove the spaces from the title if necessary.
+    title=${title// /_}
 
-# Add .sh to the end of the title if it is not there already.
-[ "${title: -3}" != '.sh' ] && title=${title}.sh
+    # Convert uppercase to lowercase.
+    title=${title,,}
 
-# Check to see if the file exists already.
-if [ -e $title ] ; then
-	printf "\n%s\n\n" "A script with that name already exists."
-	exit 1
-fi
+    # Add .sh to the end of the title if it is not there already.
+    [ "${title: -3}" != '.sh' ] && title=${title}.sh
+
+    # Check to see if the file exists already.
+    if [ -e $title ] ; then 
+        printf "\n%s\n%s\n\n" "The script \"$title\" already exists." \
+        "Please select another title."
+        _select_title
+    fi
+
+}
+
+_select_title
 
 printf "Enter a description: " ; read -r dscrpt
 printf "Enter your name: " ; read -r name
@@ -56,10 +63,13 @@
 
 /usr/bin/clear
 
-# Allow user to select Vim or Emacs.
 _select_editor(){
+
+    # Select between Vim or Emacs.
     printf "%s\n%s\n%s\n\n" "Select an editor." "1 for Vim." "2 for Emacs."
     read -r editor
+
+    # Open the file with the cursor on the twelth line.
     case $editor in
         1) vim +12 $title
             ;;
@@ -71,6 +81,7 @@
            _select_editor
             ;;
     esac
+
 }
 
 _select_editor

History