0
我有一個shell腳本,我想將其轉換爲可包含在.bashrc
中的函數。除了#!/bin/bash
,shell腳本包含以下功能的內容:將shell腳本轉換爲.bashrc中的函數[shell退出]
pdfMerge() {
## usage
if [ $# -lt 1 ]; then
echo "Usage: `basename $0` infile_1.pdf infile_2.pdf ... outfile.pdf"
exit 0
fi
## main
ARGS=("[email protected]") # 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在調用函數後總是退出(如果沒有給出參數的話)。這怎麼解決?
嗨NCao,感謝幫助。除了在不帶參數的情況下調用'pdfMerge'之後,'if ...'之後的部分也會被執行。這會導致'bash:ARGS:壞數組下標'bash:[0-1]:壞數組下標'****無法打開初始設備,退出。「如何才能」輕輕地「退出該功能'回聲'? – 2013-04-27 08:54:15
@MariusHofert我確實忘了'return'語句,回答更新。 – 2013-04-27 08:56:46
非常好,非常感謝! 'basename $ 0'應該也可以替換爲「pdfMerge」 – 2013-04-27 08:58:54