我有大約 10k(大約 180x50x2)CSV 文件,我想將它們連接在一起,如下所示,但內部 for 循環由於某些原因而失敗syntax error
;我看不到其中的錯誤lastFile
#!/bin/bash
dir='/home/masi/CSV/'
targetDir='/tmp/'
ids=(118 119 120)
channels=(1 2)
for id in ids;
do
for channel in channels;
# example filename P209C1T720-T730.csv
lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"
# show warning if last file does not exist
if [[ -f $lastFile ]]; then
echo "Last file "$lastFile" is missing"
exit 1
fi
filenameTarget="$targetDir'P'$id'C'$channel'.csv'"
cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget
done;
done
錯誤
makeCSV.sh: line 12: syntax error near unexpected token `lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"'
makeCSV.sh: line 12: ` lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"'
作業系統:Debian 8.5
Linux 核心:4.6 向後移植
答案1
do
你的第二個 for 迴圈中缺少一個:
for id in ids;
do
for channel in channels; do # <----- here ----
# example filename P209C1T720-T730.csv
lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"
# show warning if last file does not exist
if [[ -f $lastFile ]]; then
echo "Last file "$lastFile" is missing"
exit 1
fi
filenameTarget="$targetDir'P'$id'C'$channel'.csv'"
cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget
done;
done
根據評論中的討論,我發現您對循環語法感到困惑for
。
這是循環的粗略語法for
:
for name in list; do commands; done
do
命令之前始終必須有一個命令,命令之後必須有一個;
(或換行符) 。done
這是帶有更多換行符的變體:
for name in list
do
commands
done
答案2
它工作正常:
#!/bin/bash
dir='/home/masi/CSV/'
targetDir='/tmp/'
ids=(118 119 120)
channels=(1 2)
for id in ids ; do
# Add do after ';'
for channel in channels ; do
# example filename P209C1T720-T730.csv
lastFile="$dir'P'$id'C'$channel'T1790-T1800.csv'"
# show warning if last file does not exist
if [[ -f $lastFile ]] ; then
echo "Last file "$lastFile" is missing"
exit 1
fi
filenameTarget="$targetDir'P'$id'C'$channel'.csv'"
cat $dir'P'$id'C'$channel'T'*'.csv' > $filenameTarget
done
done
為了將來使用 bash 調試器:bash -x /路徑/到/你的/腳本。