command line - Want to write a bash script to automate creating program files - Ask Ubuntu


i want write bash script can:

  1. create file specific name entered user. if user doesnt enter name, should provide default name check if name present.
  2. allow more variables entered user can used populate program file, including filling comments section (for things author_name)

i don't have experience in writing bash scripts please provide small code examples. lot!

ps: guess pretty npm init script creates package.json file. cant find script, if it's open source please provide link if have it.

i have written uptil now:

skeleton:

/*  * url :   * author: john m.  * timestamp:   */  #include <iostream> #include <algorithm> #include <vector>   using namespace std;  int main(int argc, char* argv[]) {     // code } 

skeleton.sh

#!/bin/bash  created=0 while [ $created==0 ];     read -p "enter name of new c++ program file (code.cpp)" name     case $name in          * ) name="code.cpp";;     esac     if [ -e $name ];         echo "file name $name exists!"         echo "please provide different filename"     else          echo >> $name         created=1     fi done  cat skeleton > $name        # there's error in line, dont know wrong here 

this isn't homework. trying automate creating new file. not going c++ file creation (in case tells me use ide).

i able fill in timestamp generating through script , able fill in url using script. if can provide help, that'd appreciated. thanks

edit: added these 2 lines in accepted answer:

  local curr_dir="$( cd "$( dirname "${bash_source[0]}" )" && pwd )"   subl "$curr_dir/$prog_name" 

right using sublime text, works out. if on computer doesn't have sublime text installed, can make such changes script if isn't, opens gedit or else instead of giving error?

examine script below. recommend using cat heredoc structure write out large chunks of text substituting variables. also, break down code functions - far more readable , easier debug.

some of technical aspectes have been mentioned here. i'm using heredoc structure cat write out header , body. tee command used write out info both stdout , file. way can see on command-line if went wrong, don't have open file - delete , make new one. existing filename, that's while loop keeps bugging user until enter name doesn't exist.

script

#!/bin/bash  make_header() {     timestamp=$(date)     echo "/*"     cat << eof  * url:  * author: $1  * timestamp: $timestamp eof echo " */" }  make_body() {     cat << eof  #include <iostream> #include <algorithm> #include <vector>   using namespace std;  int main(int argc, char* argv[]) {     // code }  eof }  check_name_exists() {      while [ -e "$prog_name" ];             echo ">>> $prog_name: file exists, please choose different name:"         read prog_name     done }  main() {     local prog_name=""     local author     local default_name="my_prog.cpp"      echo "enter author name:"     read author     echo "enter program name:"     read prog_name     if [ "x$prog_name" == "x"  ];then         prog_name="$default_name"     fi      check_name_exists      header=$(make_header "$author")     body=$(make_body)     echo "$header" "$body" | tee "$prog_name"  } main 

demo:

$ ./skeleton.sh                                                                                                                   enter author name: john doe enter program name: code.cpp >>> code.cpp: file exists, please choose different name: code.cpp >>> code.cpp: file exists, please choose different name: code1.cpp /*  * url:  * author: john doe  * timestamp: 2016年 11月 06日 星期日 12:42:01 mst  */  #include <iostream> #include <algorithm> #include <vector>   using namespace std;  int main(int argc, char* argv[]) {     // code } 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User