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
如何按照上面程式碼中的註釋進行健全性檢查?
我建議你下一個解決方案:
#!/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"