Bash脚本为新脚本创建模板

时间:2015-07-17 19:08:28

标签: bash shell scripting

我正在编写一个基本脚本,允许我在终端中调用它:newscript myscript "This is what my script is about"

所以这个脚本有两个参数,第一个是新脚本的名称,第二个是它的描述。

此脚本的目的是为文件顶部带有注释标准信息的任何新脚本生成某种模板,如下所示:

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

所以这就是我到目前为止所得到的但当我尝试运行时它给了我syntax error: unexpected end of file

#!/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

如果您对如何解决此问题有任何建议,请告诉我。

2 个答案:

答案 0 :(得分:3)

问题在于:

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

{braces}中的命令列表必须以换行符或分号(documentation)结尾。你需要

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

调用命令或函数时的if语法是:

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

没有括号。

答案 1 :(得分:0)

所以这是最后的脚本:

#!/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