Register
It is currently Wed Nov 26, 2014 9:02 am

script for writing new scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Thu Jan 21, 2010 7:43 am   

Joined: Thu Jan 21, 2010 7:23 am
Posts: 6
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:
#!/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...


Top
 Profile  
 PostPosted: Fri Jan 22, 2010 5:25 am   
User avatar

Joined: Mon Jan 18, 2010 8:10 pm
Posts: 40
Hi iesaya,

That's a sweet script. Thanks. :)

geo


Top
 Profile  
 PostPosted: Fri Jan 22, 2010 10:15 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
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 ;)


Top
 Profile WWW  
 PostPosted: Sun Jan 24, 2010 2:20 pm   

Joined: Thu Jan 21, 2010 7:23 am
Posts: 6
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:
#!/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:
#!/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


Top
 Profile  
 PostPosted: Sun Feb 07, 2010 8:43 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Thanks :)


Top
 Profile WWW  
 PostPosted: Fri Mar 19, 2010 2:58 pm   

Joined: Tue Mar 16, 2010 11:45 pm
Posts: 2
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:
#!/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


Top
 Profile  
 PostPosted: Wed Jun 30, 2010 1:55 pm   
User avatar

Joined: Wed Jun 30, 2010 12:17 pm
Posts: 3
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:
#!/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:
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?


Top
 Profile  
 PostPosted: Wed Jun 30, 2010 2:45 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
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.


Top
 Profile  
 PostPosted: Wed Jun 30, 2010 4:49 pm   
User avatar

Joined: Wed Jun 30, 2010 12:17 pm
Posts: 3
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:
new -f test3

nope need more:
Code:
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:
#!/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!


Top
 Profile  
 PostPosted: Wed Jun 30, 2010 10:47 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Sector11 wrote:
Code:
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:
$> 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:
  *) echo "Unknown argument: $1" >&2; exit 1


Top
 Profile  
 PostPosted: Tue Jul 27, 2010 1:47 pm   

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


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Alexa [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP