2017-12-07 10 views
1

スクリプトに最初のループ反復で特定のアクションを実行させる必要があります。bashスクリプトのwhileループの最初の反復で特定のアクションを実行する方法

for file in /path/to/files/*; do 
    echo "${file}" 
done 

私はこの出力をしたい:

First run: file1 
file2 
file3 
file4 
... 
+0

を得ましたか。最初のファイルで '最初の実行:'だけを印刷しますか? – Inian

+0

はい、最初の実行時に何かdiffernetを実行する必要があります。 – mbergmann

答えて

1

非常に一般的な配置は、ループ内の変数を変更することです。

noise='First run: ' 
for file in /path/to/files/*; do 
    echo "$noise$file" 
    noise='' 
done 
1

あなたは、この配列の最初の要素を削除しておく、その後、ファイルで配列を作成することができ、プロセス、それは別にして、残りの要素を処理配列:

# create array with files 
files=(/path/to/files/*) 

# get the 1st element of the array 
first=${files[0]} 

# remove the 1st element of from array 
files=("${files[@]:1}") 

# process the 1st element 
echo First run: "$first" 

# process the remaining elements 
for file in "${files[@]}"; do 
    echo "$file" 
done 
0

私はあなたが何を意味する自分自身のソリューション

counter=1 

for file in /path/to/files/*; do 
    echo -e "$(if [ "${counter}" -eq "1" ]; then echo "First run: "; fi)${file}" 

counter=$((counter +1)) 

done 
関連する問題