script for writing new scripts

All Scripts go here. In progress, finished, etc.

script for writing new scripts

Postby iesaya » Thu Jan 21, 2010 7:43 am

Very simple Script that automates the process of writing a new script:
1- creates a file of your choice
2- inserts shebang
3- makes it executable
4- opens it with vim [my choice...]

Code: Select all
#!/bin/bash
#written by iesaya

if [ -n "$1" ]; then
  echo "#!/bin/bash" > $1
  chmod +x $1
  vim $1
else
  echo "This command needs an argument"
fi


Enjoy...
iesaya
 
Posts: 6
Joined: Thu Jan 21, 2010 7:23 am

Re: script for writing new scripts

Postby gofree » Fri Jan 22, 2010 5:25 am

Hi iesaya,

That's a sweet script. Thanks. :)

geo
User avatar
gofree
 
Posts: 40
Joined: Mon Jan 18, 2010 8:10 pm

Re: script for writing new scripts

Postby crouse » Fri Jan 22, 2010 10:15 pm

Another vi user besides myself :) woot! lol.

Well...... take a look at THIS vi plugin ;)

http://www.thegeekstuff.com/2009/02/mak ... rt-plugin/

It's pretty slick ;)
User avatar
crouse
Site Admin
 
Posts: 561
Joined: Sun May 15, 2005 9:36 pm
Location: Des Moines, Iowa

Re: script for writing new scripts

Postby iesaya » Sun Jan 24, 2010 2:20 pm

Indeed, THIS is! nice...
The tutorial is a little out of date, though -- at least if you want the latest version. You don't need to change your .vimrc [well, you do have to include 'filetype plugin on'], everything works through templates now...
So I wrote a script that installs it and configures some basic stuff.

Code: Select all
#!/bin/bash
#written by iesaya
#requires wget, unzip

#create ~/.vim dir if doesn't exist
if [ ! -d $HOME/.vim ]; then
  mkdir $HOME/.vim
fi

#download bash-support v.3.0.1
wget -O $HOME/.vim/bash-support.zip http://www.vim.org/scripts/download_script.php?src_id=11913
#extract bash-support
unzip $HOME/.vim/bash-support.zip -d $HOME/.vim/

#sets AUTHOR,EMAIL,COMPANY
echo ""
echo "**********************************************"
echo "if you leave empty, default '---' will be used"
echo "**********************************************"
echo ""

echo -n "type in your name: "
read name
echo -n "type in your email: "
read email
echo -n "type in your company: "
read comp

if [ "$name" = "" ]; then
  echo "|AUTHOR| =   ---" >> $HOME/.vim/bash-support/templates/Templates
else
  echo "|AUTHOR| = $name" >> $HOME/.vim/bash-support/templates/Templates
fi

echo "|AUTHORREF| =   ---" >> $HOME/.vim/bash-support/templates/Templates

if [ "$email" = "" ]; then
  echo "|EMAIL| =   ---" >> $HOME/.vim/bash-support/templates/Templates
else
  echo "|EMAIL| = $email" >> $HOME/.vim/bash-support/templates/Templates
fi

if [ "$comp" = "" ]; then
  echo "|COMPANY| =   --- " >> $HOME/.vim/bash-support/templates/Templates
else
  echo "|COMPANY| = $comp" >> $HOME/.vim/bash-support/templates/Templates
fi

echo ""
echo "******************************************************************************"
echo "you can change those values by editing ~/.vim/bash-support/templates/Templates"
echo "******************************************************************************"


and I changed my first script. As bash-support needs an *.sh ending, the script will check for it and if it is missing, will insert it.
I couldn't get pass the fact, that vim needs to terminate in order for the script to execute chmod though... :-(

Code: Select all
#!/bin/bash
#written by iesaya

if [ -n "$1" ]; then
  arg=$1
  var=${arg##*.} #checks the ending of the file
  if [ $var = "sh" ]; then
    vim $arg
    chmod +x $arg*
  else
    vim $arg.sh
    chmod +x $arg*
  fi
else
  echo "This command needs an argument"
fi
iesaya
 
Posts: 6
Joined: Thu Jan 21, 2010 7:23 am

Re: script for writing new scripts

Postby crouse » Sun Feb 07, 2010 8:43 pm

Thanks :)
User avatar
crouse
Site Admin
 
Posts: 561
Joined: Sun May 15, 2005 9:36 pm
Location: Des Moines, Iowa

Re: script for writing new scripts

Postby thims » Fri Mar 19, 2010 2:58 pm

This is the script I use to generate the header I like in bash scripts. It could use a bit of improvements, but should be easily expanded upon.



Code: Select all
#!/bin/bash

# Synopsis: Shell script to auto populate header info for shell scripts(eg this line and the next few)
# Version:  0.1
# Author:   thims (thims DOT local AT gmail DOT com)
# Date:     20091101
# Comment: 



date=$(date +%Y%m%d)
author="thims (root DOT packet AT gmail DOT com)"
defversion=0.1

while [ $# -gt 0 ]
do
  case "$1" in
       
    "-f"|"--filename")
      filename="$2"
    ;;


    "-h"|"--help")
cat << EOF
./$0 [-fhvs]

  -f,--filename  -  Filename of desitnation script
  -h,--help      -  Print this help and exit
  -v,--version   -  What version of script
  -s,--synop     -  The synopsis of the script
EOF
    ;;


    "-v"|"--version")
      version="$2"
    ;;


    "-s"|"--synop")
      synop="$2"
    ;;
 
  esac
  shift
done


if [ -z "$version" ] ;then
  version=$defversion
fi

if [ -z "$synop" ] ;then
  synop="Please fill in a description, $0 synop field was left blank!"
fi


cat >> "$filename" << EOF
#!/bin/bash

# Synopsis: $synop
# Version:  $version
# Author:   $author
# Date:     $date
# Comment:


EOF
fi
thims
 
Posts: 2
Joined: Tue Mar 16, 2010 11:45 pm

Re: script for writing new scripts

Postby Sector11 » Wed Jun 30, 2010 1:55 pm

thims wrote:This is the script I use to generate the header I like in bash scripts. It could use a bit of improvements, but should be easily expanded upon.


I'm really =:) here and this doesn't work for me, I made a "couple" of changes:

  1. added the line: cd ~/bin
  2. added the comment
  3. changed:
    1. date=$(date +%d-%m-%Y)
    2. author="Sector11 (no email)"

Code: Select all
#!/bin/bash

# Synopsis: Shell script to auto populate header info for shell scripts (eg this line and the next few)
# Version:  0.1
# Author:   thims (thims DOT local AT gmail DOT com)
# Date:     20091101
# Comment:  grabbed at: http://bashscripts.org/forum/viewtopic.php?f=7&t=963#p3591

cd ~/bin

date=$(date +%d-%m-%Y)
author="Sector11 (no email)"
defversion=0.1

while [ $# -gt 0 ]
do
  case "$1" in
       
    "-f"|"--filename")
      filename="$2"
    ;;


    "-h"|"--help")
cat << EOF
./$0 [-fhvs]

  -f,--filename  -  Filename of desitnation script
  -h,--help      -  Print this help and exit
  -v,--version   -  What version of script
  -s,--synop     -  The synopsis of the script
EOF
    ;;


    "-v"|"--version")
      version="$2"
    ;;


    "-s"|"--synop")
      synop="$2"
    ;;

  esac
  shift
done


if [ -z "$version" ] ;then
  version=$defversion
fi

if [ -z "$synop" ] ;then
  synop="Please fill in a description, $0 synop field was left blank!"
fi


cat >> "$filename" << EOF
#!/bin/bash

# Synopsis: $synop
# Version:  $version
# Author:   $author
# Date:     $date
# Comment:


EOF
fi

and I run it:
Code: Select all
Wed Jun 30, 16:44:59
Sector11 working in: ~
0 $ new
/home/sector11/bin/new: line 59: : No such file or directory
/home/sector11/bin/new: line 70: syntax error near unexpected token `fi'
/home/sector11/bin/new: line 70: `fi'

Wed Jun 30, 16:45:02
Sector11 working in: ~
2 $ new test2
/home/sector11/bin/new: line 59: : No such file or directory
/home/sector11/bin/new: line 70: syntax error near unexpected token `fi'
/home/sector11/bin/new: line 70: `fi'

Wed Jun 30, 16:45:23
Sector11 working in: ~
2 $

What am I doing wrong?
User avatar
Sector11
 
Posts: 3
Joined: Wed Jun 30, 2010 12:17 pm

Re: script for writing new scripts

Postby Patsie » Wed Jun 30, 2010 2:45 pm

Well, the 2 mistakes I see are that you're not using the '-f' parameter to specify a filename to write output to (hench the 'No such file' error) and there is an extraneous 'fi' at the end of the file. This is not matched against any previous 'if'.

edit: the error handling in the script is minimal and doesn't force you to use the right parameters or respond with clear error messages when you don't.
User avatar
Patsie
 
Posts: 51
Joined: Sun Jun 27, 2010 12:57 am

Re: script for writing new scripts

Postby Sector11 » Wed Jun 30, 2010 4:49 pm

Patsie wrote:Well, the 2 mistakes I see are that you're not using the '-f' parameter to specify a filename to write output to (hench the 'No such file' error) and there is an extraneous 'fi' at the end of the file. This is not matched against any previous 'if'.

edit: the error handling in the script is minimal and doesn't force you to use the right parameters or respond with clear error messages when you don't.


That last "fi" is in the original script (told you I was green) :)

OK so it goes like this:
Code: Select all
new -f test3

nope need more:
Code: Select all
Wed Jun 30, 19:46:19
Sector11 working in: ~
0 $ new -f test4 -v Alpha 0.01 -s This is a test

Gives me: "This"
Code: Select all
#!/bin/bash

# Synopsis: This
# Version:  Alpha
# Author:   Sector11 (no email)
# Date:     30-06-2010
# Comment:


getting closer thank you.

No spaces allowed.....

Learned something today - my day is a success!
User avatar
Sector11
 
Posts: 3
Joined: Wed Jun 30, 2010 12:17 pm

Re: script for writing new scripts

Postby Patsie » Wed Jun 30, 2010 10:47 pm

Sector11 wrote:
Code: Select all
0 $ new -f test4 -v Alpha 0.01 -s This is a test

No spaces allowed.....

Learned something today - my day is a success!

A whitespace character is a separator between arguments in shell
If you want to use spaces in a single argument, you should enclose them between single or double quotes (which have different effects on variables) to tell bash that it is one argument.
Code: Select all
$> new -f test4 -v "Alpha 0.01" -s "This is a test"

In your example the -s This is a code actually tries to parse the words is, a and test as command arguments.
(un)fortunately since there is hardly any error checking, the script silently drops them since they aren't in the 'case' statement.
There could/should have been a wildcard/default (*) case with some error message as the last entry. For example:
Code: Select all
  *) echo "Unknown argument: $1" >&2; exit 1
User avatar
Patsie
 
Posts: 51
Joined: Sun Jun 27, 2010 12:57 am

Re: script for writing new scripts

Postby rootuser » Tue Jul 27, 2010 1:47 pm

thanks dude great post
rootuser
 
Posts: 1
Joined: Tue Jul 27, 2010 1:45 pm


Return to The Sandbox

Who is online

Users browsing this forum: No registered users and 1 guest