0

I am writing a basic script that will allow me to call it like so in the terminal: newscript myscript "This is what my script is about"

So this script is taking 2 arguments, first is the name of the new script, second is its description.

And the purpose of this script is to generate some kind of template for any new scripts with commented standard infos at the top of the file like so:

#!/bin/bash
#: Title       : $title
#: Date        : `date +%m-%d-%Y`
#: Author      : $user
#: Version     : 1.0
#: Description : $description

So this is what i've got so far but it gives me the syntax error: unexpected end of file when i try to run it:

#!/bin/bash

#### EDITABLES ####
user="user"       #
dest="~/bin"      #
## END EDITABLES ##

title="$1"
desc="$2"
date=`date +%m-%d-%Y`

## Checks if  a file with the same name already exists and returns the exit status (0=true, non-0=false)
fileExists() { [ -f "${1}/${2}" ] || $? }

## If file does not exist, create and populate it, otherwise exit script
if [ ! fileExists ${dest} ${title} ]; then
    printf "%b-13 %b\n" "#!/bin/bash" "" "#: Title" ":${title}" "#: Date" ":${date}" "#: Author" ": ${user}" "#: Version" ": 1.0" "#: Description" ": ${desc}" >> ${dest}/${title}
    chmod +x ${dest}/${title}
    vi ${dest}/${title}
else
    echo "The name chosen for your new script is already used. Please choose another name."
    exit
fi

unset fileExists

If you have any suggestions on how to fix this, please let me know.

1
  • Run your code through shellcheck.net it will catch many common errors and be quite helpful. Commented Jul 17, 2015 at 19:36

2 Answers 2

3

The problem is here:

fileExists() { [ -f "${1}/${2}" ] || $? }

A command list in {braces} must end with a newline or a semi-colon (documentation). You need

fileExists() { [ -f "${1}/${2}" ]; }

The if syntax when calling a command or function is:

if ! fileExists ${dest} ${title}; then

without the brackets.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that fixed a problem. But now, looks like the > ${dest}/${title} part does not work as though the script could not write to a file that does not exist yet: line 14: ~/bin/mynewscript: No such file or directory. I am pretty sure printf is able to write to a file that doesn't exist, right?
Ok, its all good. The path for the dest variable has to be absolute.
@JimiSpire, a file that doesn't exist, yes, but the directory must exist. BTW, what the command is (printf or otherwise) doesn't matter for purpose of redirections -- redirections are honored before the command is started, so a redirection that does or doesn't work for printf would work, or not, for literally anything else as well.
0

So here is the final script:

#!/bin/bash   
################################################       
#: Title             : newscript
#: Creation date     : 07-17-2015
#: Author            : Jean Michel Cote
#: Github            : https://github.com/jeanmichelcote
#: Version           : 1.0
#: Description       : Use to create fresh new scripts with commented out general infos at the top.
################################################
#
#: $param1           : Script name
#: $param2           : Script description
#
# EDITABLES ####################
user="Name"                    #
github="repo.com"              #
dest="$HOME/bin"               # Will automatically be created if non-existant
# END EDITABLES ################

title=$1 
desc=$2  
date=`date +%m-%d-%Y`
# Styles
RED="\033[0;31m"
WHITE="\033[1;37m"                               
NC="\033[0m"                       


## If destination directory does not exist, create it
if [[ ! -d "${dest}" ]]; then 
  echo -e "Creating ${WHITE}${dest}${NC} directory..." && sleep 1.5 && mkdir ${dest} 
fi

# If file does not exist, create and populate it, otherwise exit script with error message
if [[ ! -f "${dest}/${title}" ]]; then
  printf "%-20s %b\n" \
    "#!/bin/bash" "" \
    "################################################" "" \
    "#: Title" ": ${title}" \
    "#: Creation date" ": ${date}" \
    "#: Author" ": ${user}" \
    "#: Github" ": ${github}" \
    "#: Version" ": 1.0" \
    "#: Description" ": ${desc}" \
    "################################################" "" \
    "#" "" \
    "#: \$param1" ": -set param name-" \
    "#" "" \
    "# EDITABLES ######################" "" \
    "myVariable = \"myValue\"" "" \
    "# END EDITABLES ##################" "" > ${dest}/${title}
  chmod +x ${dest}/${title}
  $EDITOR ${dest}/${title}
else
  echo -e "${RED}The name chosen for your new script is already used.\nPlease choose another name."
  exit
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.