シェル/bashスクリプトを使用してファイルを開き、ファイル名を取得します。

シェル/bashスクリプトを使用してファイルを開き、ファイル名を取得します。

特定のファイルを処理するスクリプトを作成したいと思います。これまでは、単純なファイルを開くダイアログボックスとして機能しますが、スクリプト自体を使用してファイルを開いて操作を実行する「次へ開く」オプションを好みます。

それでは、ファイルにスクリプトを起動してその中のファイル名を処理させるプロセスは何ですか?

ベストアンサー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}"

おすすめ記事