Bash脚本为新脚本创建模板
我正在编写一个基本脚本,允许我在终端中这样调用它:Bash脚本为新脚本创建模板,bash,shell,scripting,Bash,Shell,Scripting,我正在编写一个基本脚本,允许我在终端中这样调用它:newscript myscript“这就是我的脚本的内容” 这个脚本有两个参数,第一个是新脚本的名称,第二个是它的描述 该脚本的目的是为任何新脚本生成某种模板,在文件顶部有注释的标准信息,如下所示: #!/bin/bash #: Title : $title #: Date : `date +%m-%d-%Y` #: Author : $user #: Version : 1.0 #: Descrip
newscript myscript“这就是我的脚本的内容”
这个脚本有两个参数,第一个是新脚本的名称,第二个是它的描述
该脚本的目的是为任何新脚本生成某种模板,在文件顶部有注释的标准信息,如下所示:
#!/bin/bash
#: Title : $title
#: Date : `date +%m-%d-%Y`
#: Author : $user
#: Version : 1.0
#: Description : $description
这就是我到目前为止得到的结果,但是当我尝试运行它时,它给了我语法错误:意外的文件结尾
:
#!/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
如果您对如何解决此问题有任何建议,请告诉我。问题在于:
fileExists() { [ -f "${1}/${2}" ] || $? }
{brates}中的命令列表必须以换行符或分号()结尾。你需要
fileExists() { [ -f "${1}/${2}" ]; }
调用命令或函数时的if
语法为:
if ! fileExists ${dest} ${title}; then
没有括号。问题在于:
fileExists() { [ -f "${1}/${2}" ] || $? }
{brates}中的命令列表必须以换行符或分号()结尾。你需要
fileExists() { [ -f "${1}/${2}" ]; }
调用命令或函数时的if
语法为:
if ! fileExists ${dest} ${title}; then
没有括号。问题在于:
fileExists() { [ -f "${1}/${2}" ] || $? }
{brates}中的命令列表必须以换行符或分号()结尾。你需要
fileExists() { [ -f "${1}/${2}" ]; }
调用命令或函数时的if
语法为:
if ! fileExists ${dest} ${title}; then
没有括号。问题在于:
fileExists() { [ -f "${1}/${2}" ] || $? }
{brates}中的命令列表必须以换行符或分号()结尾。你需要
fileExists() { [ -f "${1}/${2}" ]; }
调用命令或函数时的if
语法为:
if ! fileExists ${dest} ${title}; then
没有括号。所以这里是最后的脚本:
#!/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
下面是最后的脚本:
#!/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
下面是最后的脚本:
#!/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
下面是最后的脚本:
#!/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
通过它运行代码将捕获许多常见错误并非常有用。通过它运行代码将捕获许多常见错误并非常有用。通过它运行代码将捕获许多常见错误并非常有用。通过它运行代码将捕获许多常见错误并非常有用。谢谢,这解决了一个问题。但是现在,看起来
${dest}/${title}
部分无法工作,好像脚本无法写入一个尚不存在的文件:第14行:~/bin/mynewscript:没有这样的文件或目录
。我很确定printf能够写入一个不存在的文件,对吗?好的,一切都很好。dest
变量的路径必须是绝对路径。@JimiSpire,一个不存在的文件,是的,但目录必须存在。顺便说一句,命令是什么(printf
或其他)对于重定向来说并不重要——在命令启动之前,重定向是有效的,因此对printf
有效或无效的重定向对其他任何东西都有效。谢谢,这解决了一个问题。但是现在,看起来${dest}/${title}
部分无法工作,好像脚本无法写入一个尚不存在的文件:第14行:~/bin/mynewscript:没有这样的文件或目录
。我很确定printf能够写入一个不存在的文件,对吗?好的,一切都很好。dest
变量的路径必须是绝对路径。@JimiSpire,一个不存在的文件,是的,但目录必须存在。顺便说一句,命令是什么(printf
或其他)对于重定向来说并不重要——在命令启动之前,重定向是有效的,因此对printf
有效或无效的重定向对其他任何东西都有效。谢谢,这解决了一个问题。但是现在,看起来${dest}/${title}
部分无法工作,好像脚本无法写入一个尚不存在的文件:第14行:~/bin/mynewscript:没有这样的文件或目录
。我很确定printf能够写入一个不存在的文件,对吗?好的,一切都很好。dest
变量的路径必须是绝对路径。@JimiSpire,一个不存在的文件,是的,但目录必须存在。顺便说一句,命令是什么(printf
或其他)对于重定向来说并不重要——在命令启动之前,重定向是有效的,因此对printf
有效或无效的重定向对其他任何东西都有效。谢谢,这解决了一个问题。但是现在,看起来${dest}/${title}
部分无法工作,好像脚本无法写入一个尚不存在的文件:第14行:~/bin/mynewscript:没有这样的文件或目录
。我很确定printf能够写入一个不存在的文件,对吗?好的,一切都很好。dest
变量的路径必须是绝对路径。@JimiSpire,一个不存在的文件,是的,但目录必须存在。顺便说一句,命令是什么(printf
或其他)对于重定向来说并不重要——在命令启动之前,重定向是有效的,因此对printf
有效或无效的重定向对其他任何东西都有效或无效。