简体   繁体   English

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

[英]Echo current filename into a text file ubuntu?

I normally just browse around and not looking to post questions however I am currently learning how to shell script and I've fallen into a pickle.我通常只是四处浏览,而不是想发布问题,但是我目前正在学习如何编写 shell 脚本,但我陷入了困境。

At the moment I have a structure of:目前我有一个结构:

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

All the way to Hub20.一直到Hub20。

What i want the structure to be is:我想要的结构是:

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"

and so on.等等。 At the moment my code is looking like this目前我的代码看起来像这样

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

Its not doing what it needs to be doing and I just cant figure out what I am doing wrong.它没有做它需要做的事情,我只是无法弄清楚我做错了什么。 I am aware it's not formatted right.我知道它的格式不正确。 It may be a simply solution or maybe I am completely off.这可能是一个简单的解决方案,或者我可能完全离开了。 I want to get it working before I start moving it around.我想在我开始移动它之前让它工作。 Any advice would be great.任何建议都会很棒。 Thank you!谢谢!

You can use a brace range instead of until .您可以使用大括号范围而不是until

Don't put the pathname in quotes, that prevents expanding ~ .不要将路径名放在引号中,以免扩展~

You need $ before filename in the echo statement to expand the variable.您需要在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM