[英]Accessing files in folders by defining multiple variables in bash script
I am trying to access files in many folders to copy them into a new folder.我正在尝试访问许多文件夹中的文件以将它们复制到新文件夹中。
I define the folders using variables as below.我使用如下变量定义文件夹。
q="Q1-students-answer2" # this is a folder
achari1="AHMAD_JAUHARI_BIN_NAZAIDI_._1587851_assignsubmission_file_" #this is folder
achari2="AINUL_SUHAILAH_BINTI_MAIL_._1587856_assignsubmission_file_" #this is folder
achari3="AMIRAH_NURAZIZAH_._1587875_assignsubmission_file_" #this is folder
achari4="AMIRAH_RADHIANIE_BINTI_RINING_._1587865_assignsubmission_file_" #this is folder
Below is the "for" block to recursively access the folders that I defined using variables as achari1, achari2, achari3, and achari4下面是递归访问我使用变量定义的文件夹的“for”块,如 achari1、achari2、achari3 和 achari4
for i in {1..4};do
echo $q/$achari$i
done
This code gives the output as此代码给出 output 为
Q1-students-answer2/1
Q1-students-answer2/2
Q1-students-answer2/3
Q1-students-answer2/4
Instead I want the output as相反,我想要 output 作为
Q1-students-answer2/AHMAD_JAUHARI_BIN_NAZAIDI_._1587851_assignsubmission_file_
Q1-students-answer2/AINUL_SUHAILAH_BINTI_MAIL_._1587856_assignsubmission_file_
Q1-students-answer2/AMIRAH_NURAZIZAH_._1587875_assignsubmission_file_
Q1-students-answer2/AMIRAH_RADHIANIE_BINTI_RINING_._1587865_assignsubmission_file_
I have tried many other ways like我尝试了许多其他方法,例如
echo "$q/achari$i"
or
(cd $q/achari$i;
echo $q/$(achari)$i;
cd ../)
or
echo "$q/$(achari)$i"
but all these above seems not working.但以上所有这些似乎都不起作用。
Appreciate if someone could show what is the wrong in my code.感谢是否有人可以显示我的代码中有什么问题。
Use an array, not separate variables:使用数组,而不是单独的变量:
achari=("AHMAD_JAUHARI_BIN_NAZAIDI_._1587851_assignsubmission_file_" #this is folder
"AINUL_SUHAILAH_BINTI_MAIL_._1587856_assignsubmission_file_" #this is folder
"AMIRAH_NURAZIZAH_._1587875_assignsubmission_file_" #this is folder
"AMIRAH_RADHIANIE_BINTI_RINING_._1587865_assignsubmission_file_") #this is folder
for dir in "${achari[@]}"; do
echo $q/$dir
done
Either use an array, or use indirect expansion as in:要么使用数组,要么使用间接扩展,如下所示:
#!/bin/bash
q="Q1-students-answer2" # this is a folder
achari1="AHMAD_JAUHARI_BIN_NAZAIDI_._1587851_assignsubmission_file_"
achari2="AINUL_SUHAILAH_BINTI_MAIL_._1587856_assignsubmission_file_"
achari3="AMIRAH_NURAZIZAH_._1587875_assignsubmission_file_"
achari4="AMIRAH_RADHIANIE_BINTI_RINING_._1587865_assignsubmission_file_"
for i in {1..4}; do
t=achari$i
echo "$q"/"${!t}"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.