Bashスクリプトの入力パラメータの処理

Bashスクリプトの入力パラメータの処理

ファイルを処理するためにbashスクリプトを使用しています$ processh.sh file。入力には、file処理するファイルに関する情報が含まれています。

# id path type
id1 filename1 csv
id2 filename2 
id3 filename3 json
...

process.sh何ですか?

#!/bin/bash
parse(x) {
    # get idX filenameX typeX
    # check if filenameX exists on disk
    # if typeX is missing, set to csv
}
a=`grep -vE '^(\s*$|#)' $1`  # remove comment and empty lines
echo $a | while IFS= read -r l; do
    [id, filename, type]=parse(l)
    process filenameX ...
done

上記のコードの説明に従って、完全性チェックをどのように実行できますか?

ベストアンサー1

次の解決策を提案します。

#!/bin/bash

parse() {
        # get idX filenameX typeX
        id=$1
        filename=$2
        typo=$3

        # if typeX is missing, set to csv
        [ -z "$typo" ] && typo="csv"

        # check if filenameX exists on disk
        [ -f "$filename.$typo" ] && echo "Filename $filename exists"
}

while IFS= read -r line
do
        parse_line=`echo $line | grep -vE '^(\s*$|#)'`
        [ ! -z "$parse_line" ] && parse $parse_line
done < "$1"

おすすめ記事