Bash 如何使来自文件{1-3}变量的文件在for循环中具有值?
我有一些文件,比如:Bash 如何使来自文件{1-3}变量的文件在for循环中具有值?,bash,Bash,我有一些文件,比如: FILE1="apple.txt" FILE2="grapes.txt" FILE3="strawberry.txt" 如何在bash中生成这些文件 我试过了,但我错了 for f in {1..3} do echo hello > $(FILE$f) done 错误: ./make_files.sh: line 48: FILE1: command not found ./make_files.sh: line 48: $(FILE$f): ambigu
FILE1="apple.txt"
FILE2="grapes.txt"
FILE3="strawberry.txt"
如何在bash中生成这些文件
我试过了,但我错了
for f in {1..3}
do
echo hello > $(FILE$f)
done
错误:
./make_files.sh: line 48: FILE1: command not found
./make_files.sh: line 48: $(FILE$f): ambiguous redirect
我需要3个TXT,苹果,葡萄和草莓,这是包含哈罗 IIUC,你想要这个:
for f in {1..3}
do
echo hello > FILE"$f"
done
for f in {1..3}
do
echo hello > $(< FILE"$f")
done
编辑:
根据你对另一个答案的评论,我认为你真的想要:
for f in {1..3}
do
echo hello > FILE"$f"
done
for f in {1..3}
do
echo hello > $(< FILE"$f")
done
例如:
$ ls
FILE1 FILE2 FILE3
$ cat FILE1 FILE2 FILE3
apple.txt
grapes.txt
strawberry.txt
$ for f in {1..3}
> do
> echo hello > $(< FILE"$f")
> done
$ cat apple.txt grapes.txt strawberry.txt
hello
hello
hello
IIUC,你想要这个:
for f in {1..3}
do
echo hello > FILE"$f"
done
for f in {1..3}
do
echo hello > $(< FILE"$f")
done
编辑:
根据你对另一个答案的评论,我认为你真的想要:
for f in {1..3}
do
echo hello > FILE"$f"
done
for f in {1..3}
do
echo hello > $(< FILE"$f")
done
例如:
$ ls
FILE1 FILE2 FILE3
$ cat FILE1 FILE2 FILE3
apple.txt
grapes.txt
strawberry.txt
$ for f in {1..3}
> do
> echo hello > $(< FILE"$f")
> done
$ cat apple.txt grapes.txt strawberry.txt
hello
hello
hello
或者一次完成所有工作:
for f in FILE{1..3}; do
echo hello >"${!f}"
done
代码中的主要错误是$…,这是命令替换:它试图将namesFILE1、FILE2等作为命令运行,以便将这些命令的输出用作要写入的文件的名称
您要做的是将它们用作参数名。要间接检索名称存储在另一个参数中的参数的值,请使用!,就像在${!f}中一样
但大多数情况下,当您执行类似操作时,最好使用阵列:
FILES=("apple.txt" "grapes.txt" "strawberry.txt")
for f in "${FILES[@]}"; do
echo hello >"$f"
done
或者一次完成所有工作:
for f in FILE{1..3}; do
echo hello >"${!f}"
done
代码中的主要错误是$…,这是命令替换:它试图将namesFILE1、FILE2等作为命令运行,以便将这些命令的输出用作要写入的文件的名称
您要做的是将它们用作参数名。要间接检索名称存储在另一个参数中的参数的值,请使用!,就像在${!f}中一样
但大多数情况下,当您执行类似操作时,最好使用阵列:
FILES=("apple.txt" "grapes.txt" "strawberry.txt")
for f in "${FILES[@]}"; do
echo hello >"$f"
done
我想您要做的是创建具有以下三个名称的文件: apple.txt、grapes.txt和草莓.txt 所以你应该:
for f in {1..3}
do
TMP="FILE$f"
echo hello > "${!TMP}"
done
我想您要做的是创建具有以下三个名称的文件: apple.txt、grapes.txt和草莓.txt 所以你应该:
for f in {1..3}
do
TMP="FILE$f"
echo hello > "${!TMP}"
done
它写出hello,到FILE1,FILE2,FILE3。但是我需要向apple.txt等写hello,它向FILE1,FILE2,FILE3写hello。但我需要写下对apple.txt的问候,等等。。