Welcome, guest | Sign In | My Account | Store | Cart

This will create a header for a Bash script. It is a nice way keep a track of what your script does and when it was created, the author of the script, etc..

It will open the script automatically with one of the two most popular editor out there, Vim or Emacs! It also checks to see if there is a script with the same name in the current working directory so it will not overwrite another file.

v0.4: I had to kick this up a notch. I took the suggestion of "dev h" to add a chance for the user to select another name for the script.

Please leave comments and suggestions.

Bash, 87 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/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

I use this script a whole lot. I thought that other people might find it useful.

5 comments

dev h 12 years, 5 months ago  # | flag

thx, this is just what i was looking for!

note: i would add this to save a few keystrokes in case file exists:

# Check to see if script name exists already and, if so, ask user for another name.
if [ -e $title ] ; then
        while [ -e $title ]
            do 
                printf "\n%s\n\n" "A script with that name already exists. Try again: "
                read title
                test -e $title
            done
fi
userend (author) 12 years, 5 months ago  # | flag

dev h:

That is super cool. Great idea. I will add it to the script.

userend (author) 12 years, 5 months ago  # | flag

dev h:

I had to add a recursive function. The while loop was not working for me. After a new title is selected it should be checked for spaces and for the ".sh" extention.

Tom Rosey 8 years, 7 months ago  # | flag

Great Script I am even going to expand it a little for myself using dialog and adding a couple of functions I use in almost all my scripts. Lick centering, YES/NO, Are you root and system I am on.

I have one thing though to sujest on the basic script you have is to add nano on the list of Editors. It is listed higher then the others in the terms on users. also the other one usually installed by default by many distros is Vi.

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

......

case $editor in
    1) vi +12 $title
        ;;
    2) vim +12 $title &
        ;;
    3) emacs +12 $title &
        ;;
    4) nano +12 $title &
        ;;
    *) /usr/bin/clear

The following is a step by step of all the changes. In line 1 I rewrote the comment to include Nano and Vi. In line 2 there were 2 different changes, first was a new format added to incompass the new items. so the line now uses 2 more %s\n and then added the two new optionsin. and renumbered everything to ocomudate the change new options "1 for Vi." & "4. for Nano". so the new display should looks like this:

Select an editor. 1 for Vi. 2 for Vim. 3 for Emacs. 4 for Nano

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

    ......

  4. case $editor in

  5. 1) vi +12 $title
  6. ;;
  7. 1) vim +12 $title &
  8. ;;
  9. 2) emacs +12 $title &
  10. ;;
  11. 3) nano +12 $title &
  12. ;;
  13. *) /usr/bin/clear

I hope you like If you or anyone else is interested in the dialog version of this. Respond to this post and I can post it for others. Thanks again you got my wheels going.

minisha 7 years, 9 months ago  # | flag

May I know the color scheme you are using for vim?