英文:
IF/ELIF/ELSE Statement Korn Shell
问题
我过去有运行过这些语句的经验,但并不多。
我正在尝试运行一个程序,它将查找目录中的文件,如果文件是 .txt 格式,则发送到 testdir_2,否则如果是 .csv 文件,则发送到 testdir_3。如果是其他文件类型,则输出 "error"。
似乎出现了一个问题,当代码找不到 .txt 文件时,它会报错而不是继续执行语句。
我还收到了一个 "else unexpected" 的消息。
我可能只是漏掉了一些明显的东西,但我似乎无法弄清楚哪里出了问题。在此提前感谢您的帮助。
英文:
Ive had experiences running these statements in the past but not loads.
Im trying to run one that will look in the directory and if the file is a .txt then send it to testdir_2 else if it is a .csv file then send to testdir_3. Else if it is anything else echo "error".
What seems to be happening is when the code doesn't find the .txt file it will error out instead of continuing with the statement.
Im also getting a "else unexpected" message.
I may just be missing something obvious but I cannot seem to work out what is wrong. Thank you in advance.
#!/bin/ksh
#set -x
#
FILEDIR=/export/home/jaqst/training/testdir_1
DIR1=/export/home/jaqst/training/testdir_2
DIR2=/export/home/jaqst/training/testdir_3
TXT=*.txt
CSV=*.csv
#
# Main processing starts here
#
echo `date +%d\ %b\ %H:%M` "Job started"
#
# Go to directory
#
cd ${FILEDIR}
if [[ $? -ne 0 ]]
then echo `date +%d\ %b\ %H:%M` "Failed to cd into ${FILEDIR}"
exit 9
fi
#
# Check files and move to appropriate directories
#
if [ ! -e = ${TXT} ];
then
mv ${TXT} /export/home/jaqst/training/testdir_2
elif [ ! -e = ${CSV} ];
then
mv ${CSV} /export/home/jaqst/training/testdir_3
else
echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
exit 9
fi
#
echo `date +%d\ %b\ %H:%M` "Job completed"
exit 0
答案1
得分: 0
使用子Shell,以以下方式更新代码:
if [[ -n $(ls ${TXT}) ]];
then
mv ${TXT} /export/home/jaqst/training/testdir_2
elif [[ -n $(ls ${CSV}) ]];
then
mv ${CSV} /export/home/jaqst/training/testdir_3
else
echo `date +%d\ %b\ %H:%M` "作业失败,未找到匹配的文件"
exit 9
fi
英文:
Use the subshell, update the codes in this way:
if [[ -n $(ls ${TXT}) ]];
then
mv ${TXT} /export/home/jaqst/training/testdir_2
elif [[ -n $(ls ${CSV}) ]];
then
mv ${CSV} /export/home/jaqst/training/testdir_3
else
echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
exit 9
fi
答案2
得分: 0
A different approach: loop over the files and pattern match on the filename
count=0
for file in *; do
case "$file" in
*.txt)
mv "$file" "$DIR1"
((++count))
;;
*.csv)
mv "$file" "$DIR2"
((++count))
;;
esac
done
if [[ $count -eq 0 ]]; then
echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
exit 9
fi
By the way, ksh's printf
can do date formatting, no need to call out to date:
log() {
printf '%(%d %b %H:%M)T - %s\n' now "$*"
}
log Hello World # => "29 Jun 08:41 - Hello World"
英文:
A different approach: loop over the files and pattern match on the filename
count=0
for file in *; do
case "$file" in
*.txt)
mv "$file" "$DIR1"
((++count))
;;
*.csv)
mv "$file" "$DIR2"
((++count))
;;
esac
done
if [[ $count -eq 0 ]]; then
echo `date +%d\ %b\ %H:%M` "Job failed, no matching file found"
exit 9
fi
By the way, ksh's printf
can do date formatting, no need to call out to date:
log() {
printf '%(%d %b %H:%M)T - %s\n' now "$*"
}
log Hello World # => "29 Jun 08:41 - Hello World"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论