Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将shell脚本转换为.bashrc[shell退出]中的函数_Bash_Shell - Fatal编程技术网

将shell脚本转换为.bashrc[shell退出]中的函数

将shell脚本转换为.bashrc[shell退出]中的函数,bash,shell,Bash,Shell,我有一个shell脚本,我想将其转换为可以包含在.bashrc中的函数。除了#/bin/bash,shell脚本包含以下函数的内容: pdfMerge () { ## usage if [ $# -lt 1 ]; then echo "Usage: `basename $0` infile_1.pdf infile_2.pdf ... outfile.pdf" exit 0 fi ## main ARGS=("$@") # determin

我有一个shell脚本,我想将其转换为可以包含在
.bashrc
中的函数。除了
#/bin/bash
,shell脚本包含以下函数的内容:

pdfMerge () {
    ## usage
    if [ $# -lt 1 ]; then
    echo "Usage: `basename $0` infile_1.pdf infile_2.pdf ... outfile.pdf"
    exit 0
    fi
    ## main
    ARGS=("$@") # determine all arguments
    outfile=${ARGS[-1]} # get the last argument
    unset ARGS[${#ARGS[@]}-1] # drop it from the array
    exec gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$outfile "${ARGS[@]}" # call gs
}

它已经运行并将给定的pdf文件与ghostscript相结合。但是,shell总是在调用函数后退出(如果没有给出参数也是如此)。如何修复此问题?

脚本设计为作为独立可执行文件运行,因此在完成后退出。如果要将其用作函数,则需要删除执行此行为的两个元素:
exit 0
(将其替换为
return
)和调用
gs-dBATCH…
前的
exec

pdfMerge () {
    ## usage
    if [ $# -lt 1 ]; then
        echo "Usage: $FUNCNAME infile_1.pdf infile_2.pdf ... outfile.pdf"
        return
    fi
    ## main
    ARGS=("$@") # determine all arguments
    outfile=${ARGS[-1]} # get the last argument
    unset ARGS[${#ARGS[@]}-1] # drop it from the array
    gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$outfile "${ARGS[@]}" # call gs
}

该脚本被设计为作为独立的可执行文件运行,因此在完成时退出。如果要将其用作函数,则需要删除执行此行为的两个元素:
exit 0
(将其替换为
return
)和调用
gs-dBATCH…
前的
exec

pdfMerge () {
    ## usage
    if [ $# -lt 1 ]; then
        echo "Usage: $FUNCNAME infile_1.pdf infile_2.pdf ... outfile.pdf"
        return
    fi
    ## main
    ARGS=("$@") # determine all arguments
    outfile=${ARGS[-1]} # get the last argument
    unset ARGS[${#ARGS[@]}-1] # drop it from the array
    gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOUTPUTFILE=$outfile "${ARGS[@]}" # call gs
}

你好,NCao,谢谢你的帮助。这非常有效,除了在调用没有参数的
pdfMerge
之后,
if…
之后的部分也会执行。这导致
bash:ARGS:bad array subscript
bash:[0-1]:bad array subscript
****无法打开初始设备,退出。
echo
之后,如何“轻轻”退出函数?@mariushofer我忘记了一个
返回
语句确实,答案已更新。非常好,非常感谢
basename$0
可能也应该被“pdfMerge”替换。您也可以使用
$FUNCNAME
。您好,NCao,谢谢您的帮助。这非常有效,除了在调用没有参数的
pdfMerge
之后,
if…
之后的部分也会执行。这导致
bash:ARGS:bad array subscript
bash:[0-1]:bad array subscript
****无法打开初始设备,退出。
echo
之后,如何“轻轻”退出函数?@mariushofer我忘记了一个
返回
语句确实,答案已更新。非常好,非常感谢
basename$0
可能也应该被“pdfMerge”替换,您也可以使用
$FUNCNAME