Bash 如何创建shell别名以创建新脚本并在编辑器中打开它?
Shell脚本快捷键或别名对于在很少的按键操作中自动执行重复任务非常有用 “快捷方式”命令的一个示例是Bash 如何创建shell别名以创建新脚本并在编辑器中打开它?,bash,shell,alias,Bash,Shell,Alias,Shell脚本快捷键或别名对于在很少的按键操作中自动执行重复任务非常有用 “快捷方式”命令的一个示例是takefromzsh,它执行mkdir$dir&&cd$dir 我想做一些类似的(medit)来实现这一点:调用medit script.sh: 创建脚本 使其可执行(chmod+x) 用shebang行填充它 在默认编辑器中打开文件 我是否可以使用别名来执行此操作,以避免编写实现此功能的bash脚本?您最好编写如下函数: function medit(){ echo "#!/bi
take
fromzsh
,它执行mkdir$dir&&cd$dir
我想做一些类似的(medit
)来实现这一点:调用medit script.sh
:
- 创建脚本
- 使其可执行(chmod+x)
- 用shebang行填充它
- 在默认编辑器中打开文件
我是否可以使用别名来执行此操作,以避免编写实现此功能的bash脚本?您最好编写如下函数:
function medit(){
echo "#!/bin/bash" > "$1"
chmod +x "$1"
if [ -n "$VISUAL" ]; then
"$VISUAL" "$1"
elif [ -n "$EDITOR" ]; then
"$EDITOR" "$1"
else
vi "$1"
fi
}
将它放到.bashrc并使用medit script.sh调用它
它将首先尝试运行$VISUAL
和$editor
中指定的编辑器,如果没有指定标准编辑器,则返回到vi
。您最好编写如下函数:
function medit(){
echo "#!/bin/bash" > "$1"
chmod +x "$1"
if [ -n "$VISUAL" ]; then
"$VISUAL" "$1"
elif [ -n "$EDITOR" ]; then
"$EDITOR" "$1"
else
vi "$1"
fi
}
将它放到.bashrc并使用medit script.sh调用它
它将首先尝试运行$VISUAL
和$editor
中指定的编辑器,如果没有指定标准编辑器,它将返回到vi
。我会这样写:
# Usage: just like 'builtin printf'
fatal_error() {
builtin printf "$@" >&2
exit 1
}
# Usage: medit [file] [more arguments]
medit() {
# If we have arguments
if [ $# -gt 0 ]; then
# If $1 file does not exist, or is empty,
# put a shebang line into it. See `help test`.
if [ ! -s "$1" ]; then
printf "%s\n" "#!/bin/bash -" > "$1" || \
fatal_error "Failed to write to %s\n" "$1"
fi
# Make $1 executable
chmod +x "$1" || fatal_error "failed to chmod %s\n" "$1"
fi
# Run the default editor with the arguments passed to this function
"${EDITOR:-${VISUAL:-vi}}" "$@"
}
可以像默认编辑器一样调用medit
命令,即使没有参数
如果您不知道如何重复使用上面的脚本,请将代码放入一些~/scripts/medit
,然后从相应的初始化脚本(如~/.bashrc
):源代码~/scripts/medit
)中,我会这样写:
# Usage: just like 'builtin printf'
fatal_error() {
builtin printf "$@" >&2
exit 1
}
# Usage: medit [file] [more arguments]
medit() {
# If we have arguments
if [ $# -gt 0 ]; then
# If $1 file does not exist, or is empty,
# put a shebang line into it. See `help test`.
if [ ! -s "$1" ]; then
printf "%s\n" "#!/bin/bash -" > "$1" || \
fatal_error "Failed to write to %s\n" "$1"
fi
# Make $1 executable
chmod +x "$1" || fatal_error "failed to chmod %s\n" "$1"
fi
# Run the default editor with the arguments passed to this function
"${EDITOR:-${VISUAL:-vi}}" "$@"
}
可以像默认编辑器一样调用medit
命令,即使没有参数
如果您不知道如何重新使用上面的脚本,请将代码放入一些~/scripts/medit
,然后从相应的初始化脚本(如~/.bashrc
):源代码~/scripts/medit中,您需要的是一个别名。
如果不使用一个或多个功能,就无法完成所有要求。
但是有一种方法可以让别名定义一些函数并调用它们:
alias medit='
SayError(){ local a=$1; shift; printf "%s\n" "$0: $@" >&2; exit "$a"; }
medit(){
[[ $# -lt 1 ]] &&
SayError 1 "We need at least the name of the file as an argument"
[[ ! -s $1 ]] && echo "#!/bin/bash" > "$1" ||
SayError 2 "File $1 already exists"
chmod u+x "$1" ||
SayError 3 "File $1 could not be made executable"
${VISUAL:-${EDITOR:-emacs}} "$1" ||
SayError 4 "File $1 could not be open in the editor"
}
\medit'
您需要执行别名的上述定义medit
,或者将其放置在~/.bashrc
中,或者将其源代码放在运行的shell中以使其存在
然后,当调用别名时,它定义了两个函数:SayError
和medit
是,与别名同名的函数:medit
函数定义后,别名将使用技巧调用函数:
\medit
由于(严格地说)a\medit
不完全是别名medit
,bash一直在搜索并找到函数medit
,该函数当时已经定义并执行
当然,您可以定义函数并使用它们,而无需借助别名来定义函数,这是您的选择
有选择才好。:)
这就是如何定义源文件中的所有内容:
alias medit='\medit'
SayError(){ local a=$1; shift; printf "%s\n" "$0: $@" >&2; exit "$a"; }
medit(){
[[ $# -lt 1 ]] &&
SayError 1 "We need at least the name of the file as an argument"
[[ ! -s $1 ]] && echo "#!/bin/bash" > "$1" ||
SayError 2 "File $1 already exists"
chmod u+x "$1" ||
SayError 3 "File $1 could not be made executable"
${VISUAL:-${EDITOR:-emacs}} "$1" ||
SayError 4 "File $1 could not be open in the editor"
}
您需要的是别名。
如果不使用一个或多个功能,就无法完成所有要求。
但是有一种方法可以让别名定义一些函数并调用它们:
alias medit='
SayError(){ local a=$1; shift; printf "%s\n" "$0: $@" >&2; exit "$a"; }
medit(){
[[ $# -lt 1 ]] &&
SayError 1 "We need at least the name of the file as an argument"
[[ ! -s $1 ]] && echo "#!/bin/bash" > "$1" ||
SayError 2 "File $1 already exists"
chmod u+x "$1" ||
SayError 3 "File $1 could not be made executable"
${VISUAL:-${EDITOR:-emacs}} "$1" ||
SayError 4 "File $1 could not be open in the editor"
}
\medit'
您需要执行别名的上述定义medit
,或者将其放置在~/.bashrc
中,或者将其源代码放在运行的shell中以使其存在
然后,当调用别名时,它定义了两个函数:SayError
和medit
是,与别名同名的函数:medit
函数定义后,别名将使用技巧调用函数:
\medit
由于(严格地说)a\medit
不完全是别名medit
,bash一直在搜索并找到函数medit
,该函数当时已经定义并执行
当然,您可以定义函数并使用它们,而无需借助别名来定义函数,这是您的选择
有选择才好。:)
这就是如何定义源文件中的所有内容:
alias medit='\medit'
SayError(){ local a=$1; shift; printf "%s\n" "$0: $@" >&2; exit "$a"; }
medit(){
[[ $# -lt 1 ]] &&
SayError 1 "We need at least the name of the file as an argument"
[[ ! -s $1 ]] && echo "#!/bin/bash" > "$1" ||
SayError 2 "File $1 already exists"
chmod u+x "$1" ||
SayError 3 "File $1 could not be made executable"
${VISUAL:-${EDITOR:-emacs}} "$1" ||
SayError 4 "File $1 could not be open in the editor"
}
相当出色的实现。我确实看到了一个bug:它应该只在文件不存在时创建文件,因为我们不希望发生意外。另一条评论只是个人偏好,即不定义新的实用程序函数,只保存两行代码,但其中一行只是个人偏好。@sorin添加了一个[!-s“$1”]
test相当不错的实现。我确实看到了一个bug:它应该只在文件不存在时创建文件,因为我们不希望发生意外。另一句话只是个人偏好,即不定义新的实用程序函数,只保存两行代码,但其中一行只是个人偏好。@sorin添加了一个[!-s“$1”]
testI将test-f
文件,然后再创建一个新文件。我将test-f
文件,然后再创建一个新文件。