将当前文件名回显到文本文件ubuntu



我通常只是四处浏览,不想发布问题,但我目前正在学习如何编写shell脚本,我陷入了困境。

目前我有一个结构:

Main
---Hub1
---Hub2
---Hub3
---Hub4

一直到Hub20。

我想要的结构是:

Main
---Hub1
--------Notes_1.txt >> "The <file_name> belongs to <user> and was created in Hub1"
---Hub2
--------Notes_2.txt >> "The <file_name> belongs to <user> and was created in Hub2"

等等。目前我的代码看起来像这个

i=1
until (($i>20))
filename="~/Main/Hub${i}/Notes_$i.txt"
do 
touch ~/Main/Hub${i}/Notes_$i.txt
echo "The $(basename -- "$filename") belongs to $USER was created in the Hub${i}" >> ~/Main/Hub${i}.txt
((i++))
done

它没有做它需要做的事情,我只是不明白我做错了什么。我知道它的格式不对。这可能是一个简单的解决方案,也可能是我完全放弃了。我想在开始移动它之前让它工作起来。任何建议都很好。非常感谢。

您可以使用大括号范围而不是until

不要将路径名放在引号中,这会阻止扩展~

echo语句中,需要先$filename来展开变量。

for i in {1..20}; do
filename=~/Main/Hub${i}/Notes_$i.txt
touch "$filename"
echo "The $(basename -- "$filename") belongs to $USER was created in the Hub${i}" >> ~/Main/Hub${i}.txt
done

最新更新