Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/bin/bash
- # Define editor
- editor=/usr/bin/vi
- # read 2 first bytes
- read_beggining () {
- if [ -e $filename ] && [ $(wc -c $filename | awk -F" " '{ print $1 }') -ge 2 ]; then
- read -r -n 2 beggining < $filename
- fi
- }
- # Function adds bash interpreter at the begining of file
- add_bash () {
- # Check if file is zero-size. Sed doesn't work with empty files
- if [ -s $filename ]; then
- read_beggining
- if [ ! "$beggining"=="#!" ]; then
- sed -i '1i#!\/bin\/bash' $filename
- fi
- else
- # This will also create file if it deos not exist
- echo "#!/bin/bash" > $filename
- fi
- }
- # Check for option
- if [ $# -eq 1 ]; then
- filename=$1
- else
- option=$1
- filename=$2
- # Add interpreter declaration
- case $option in
- -b) add_bash
- exec=true
- ;;
- *) ;;
- esac
- # If any option was used make file executable
- if [ $exec ] && [ ! -x $filename ]; then
- chmod +x $filename
- fi
- fi
- # Start editor function
- start_vi () {
- $editor $filename
- }
- #Start editor
- start_vi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement