Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.15 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # scrip - generate a script and run it
  4. #
  5. # Usage: mksh {-x} [script_name]
  6. #
  7.  
  8. USAGE="Usage: mksh {-x} [script_name]"
  9.  
  10. fpath=""
  11. exec=""
  12.  
  13. case $# in
  14.         0 ) echo -e $USAGE; exit 0 ;;
  15.         1 ) fpath="$1" ;;
  16.         2 ) if [ $1 = "-x" ]
  17.             then
  18.                 exec="Y"
  19.                 fpath="$2"
  20.             else
  21.                 echo -e $USAGE
  22.                 exit 0
  23.             fi ;;
  24. esac
  25.  
  26. if [ -e $fpath ]
  27. then
  28.         echo "File exists, please choose another filename."
  29.         exit 0
  30. else #proceed
  31.  
  32. # Get the filename from the full path.
  33. fname=`echo "${fpath}" |sed 's/.*\///'`
  34.  
  35. # Add #!/bin/bash and a header line.
  36. echo -e "#!/bin/bash\n#\n# ${fname} - ">>$fpath
  37.  
  38. # Get the length of the filename, add 6 to get the length of line 3.
  39. fname_length=6
  40. let fname_length+="${#fname}"
  41.  
  42. # Open in nano with cursor at the end of line 3.
  43. nano -c +3,$fname_length $fpath
  44.  
  45. wait $!
  46.  
  47. if [ -e $fpath ]
  48. then
  49.         chmod +x "${fpath}"
  50.         if [ "$exec" = "Y" ]
  51.         then
  52.                 ./$fpath
  53.         fi
  54. elif [ "$exec" = "Y" ]
  55. then
  56.         echo "File was not written; execution cancelled."
  57. fi
  58.  
  59. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement