我想创建一个处理特定文件的脚本。到目前为止,它可以使用一个简单的打开文件对话框,但我更喜欢“打开方式”选项,其中使用脚本本身打开文件来执行操作。
那么,这里让文件启动脚本并处理其中的文件名的过程是什么呢?
答案1
这是一个通用的解决方案。它可以为定义的进程处理仅包含 1 个文件或多个文件的列表。
#!/bin/bash
BASE=`basename "${0}" ".sh" `
TMP="/tmp/tmp.$$.${BASE}"
if [ $# -eq 0 ]
then
echo "\n Missing command line parameter to provide name of file containing list of files to process."
exit 1
fi
FILE_CONTAINING_FILENAMES="$1"
if [ ! -s "${FILE_CONTAINING_FILENAMES}" ]
then
echo "\n List file provided is empty. Unable to proceed.\n"
exit 1
fi
displayLog()
{
if [ -s "${file_to_process}.log" ]
then
echo "\t Hit return to view log ...\c" ; read k
more "${file_to_process}.log"
echo "\t Hit return to continue ...\c" ; read k
else
echo "\t Nothing capture in logs ..."
fi
}
displayErr()
{
if [ -s "${file_to_process}.err" ]
then
echo "\t Hit return to view log ...\c" ; read k
more "${file_to_process}.err"
echo "\t Hit return to continue ...\c" ; read k
else
displayLog
fi
}
processLoop()
{
process_command "${file_to_process}" >"${file_to_process}.log" 2>"${file_to_process}.err"
RC=$?
if [ ${RC} -eq 0 ]
then
echo "SUCCESS|${file_to_process}|"
displayLog
else
echo "ERROR|RC=${RC}|${file_to_process}|" >&2
displayErr
fi
}
while [ true ]
do
read file_to_process
if [ -z "${file_to_process}" ] ; then echo "\n Reached end of joblist.\n" ; exit 0 ; fi
if [ -s "${file_to_process}" ]
then
processLoop
else
echo "EMPTY|${file_to_process}|" >&2
fi
done <"${FILE_CONTAINING_FILENAMES}"