Advertisement
mike_papa

Bash script making bash scripts

Aug 28th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.03 KB | None | 0 0
  1. #/bin/bash
  2.  
  3. # Define editor
  4. editor=/usr/bin/vi
  5.  
  6. # read 2 first bytes
  7. read_beggining () {
  8. if [ -e $filename ] && [ $(wc -c $filename | awk -F" " '{ print $1 }') -ge 2 ]; then
  9.   read -r -n 2 beggining < $filename
  10. fi
  11. }
  12.  
  13. # Function adds bash interpreter at the begining of file
  14. add_bash () {
  15.   # Check if file is zero-size. Sed doesn't work with empty files
  16.   if [ -s $filename ]; then
  17.     read_beggining
  18.     if [ ! "$beggining"=="#!" ]; then
  19.       sed -i '1i#!\/bin\/bash' $filename
  20.     fi
  21.   else
  22.     # This will also create file if it deos not exist
  23.     echo "#!/bin/bash" > $filename
  24.   fi
  25. }
  26.  
  27. # Check for option
  28. if [ $# -eq 1 ]; then
  29.   filename=$1
  30. else
  31.   option=$1
  32.   filename=$2
  33.   # Add interpreter declaration
  34.   case $option in
  35.     -b) add_bash
  36.         exec=true
  37.         ;;
  38.     *)  ;;
  39.   esac
  40.   # If any option was used make file executable
  41.   if [ $exec ] && [ ! -x $filename ]; then
  42.     chmod +x $filename
  43.   fi
  44. fi
  45.  
  46.  
  47. # Start editor function
  48. start_vi () {
  49.   $editor $filename
  50. }
  51.  
  52. #Start editor
  53. start_vi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement