linux - 将文件的文件名写入 Linux/fedora 自身。

标签 linux shell fedora

我现在有点进退两难,我正在编写一个小 shell 脚本来创建 10 个文件,然后在每个文件中写入其文件名。

这是我到目前为止所拥有的:

for f in {0..9}.txt
do
    echo Hello, this is the first line of '#$f' > "File${f}"

done

因此每个文件的输出应该是:

Hello, this is the first line of file0 

Hello, this is the first line of file1

等等

最佳答案

当您使用单引号时,shell 不会扩展其中的内容。因此,'#$f' 被视为字面上的字符串 #$f,而不是扩展为 $f 的值。试试这个:

for f in {0..9}.txt; do
    echo "Hello, this is the first line of file #$f" > "File${f}"
done

示例:

$ cat File3.txt
Hello, this is the first line of file #3.txt

关于linux - 将文件的文件名写入 Linux/fedora 自身。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529391/

相关文章:

c - linux中的signal,int 0x80是谁调用的?

c - 了解进程内存的 shmat 和附件?

arrays - 如何在shell中将字符串拆分为两个单独的变量

java - 使用 JSch 在 SSH 服务器上搜索文件以获取模式(使用 less 命令)

c++ - 网络正常吗? C++ 软组织/Unix

linux - 命令 ps 带选项 -l

linux - R 在 Fedora 中安装在哪里?

linux - bash 脚本 - 循环中的 2 个变量

linux - BASH for循环在调用函数后退出

linux - Linux 上的高分辨率进程监控/日志记录