我有几个zip文件内提交文件夹每个包含多个文件夹和文件在他们。我需要从zip文件的名称中提取学生ID,并根据其扩展名创建一个目标文件夹,该文件夹将包含提取的zip文件中的C,Python和Java文件。我写了一个递归算法visit(),它获取源目录,并在解压缩后识别文件扩展名,将它们重命名为main.c/main.py/Main.java,并将它们分别移动到target/C,target/Python和target/Java,但我的代码只将内容解压缩到submissions文件夹中,其他什么也不做。我不知道如何解决这个问题。
#!/bin/bash
# Create target directory
mkdir targets
# Create subdirectories for C, Python, and Java
mkdir targets/C
mkdir targets/Python
mkdir targets/Java
# Loop through all files in submissions directory
for file in submissions/*.zip
do
# Extract the contents of the zip file
unzip "$file" -d /home/goat/Desktop/Jaid/'CSE 314'/'Offline 1'/Workspace/submissions
# Get the student ID from the file name
student_id=${file: -11:7}
#echo $student_id
visit()
{
if [ -d $1 ]
then
for i in $1/*
do
visit "$i"
done
elif [ -f $1 ]
then
#echo $1
# Get the file extension
file_extension=${1: -1}
# Check if the file extension is C, Python, or Java
# for c
if [ "$file_extension" == "c" ]
then
# Copy the C file to the targets/C directory
cp "$1" targets/C/"$student_id"/main.c
#for py
elif [ "$file_extension" == "y" ]
then
# Copy the Python file to the targets/Python directory
cp "$1" targets/Python/"$student_id"/main.py
#for java
elif [ "$file_extension" == "a" ]
then
# Copy the Java file to the targets/Java directory
cp "$1" targets/Java/"$student_id"/Main.java
fi
fi
}
visit submissions
# Remove the extracted files
#rm -rf "$file"
done
1条答案
按热度按时间euoag5mw1#
我终于可以解决我的问题了。
1.我为扩展名file_extension=${1:-1}应为file_extension="${source##*.}"
这两个补丁解决了我的问题。