为什么我的脚本不能写新文件?

时间:2018-06-08 16:50:19

标签: bash scripting

我的脚本是:

#!/bin/bash
for n in {1..10}; do
    echo $n
    dd if=/dev/urandom bs=1 count=$(( RANDOM + 1024 )) > /testingscript/file${n}.txt
done

我希望在/ testingscript目录中创建一个名为file'n'.txt的文件,其中n是文件的编号。相反,我收到了这个错误:

1
./create.sh: line 5: /testingscript/file1.txt: No such file or directory
2
./create.sh: line 5: /testingscript/file2.txt: No such file or directory
3
./create.sh: line 5: /testingscript/file3.txt: No such file or directory
4
./create.sh: line 5: /testingscript/file4.txt: No such file or directory
5
./create.sh: line 5: /testingscript/file5.txt: No such file or directory
6
./create.sh: line 5: /testingscript/file6.txt: No such file or directory
7
./create.sh: line 5: /testingscript/file7.txt: No such file or directory
8
./create.sh: line 5: /testingscript/file8.txt: No such file or directory
9
./create.sh: line 5: /testingscript/file9.txt: No such file or directory
10
./create.sh: line 5: /testingscript/file10.txt: No such file or directory

我做错了什么导致我的脚本不写新文件?我应该补充一点,这个脚本在运行Ubuntu 16.04的VM上运行完美。我现在正试图在运行Ubuntu 18.04的物理计算机上运行它。

1 个答案:

答案 0 :(得分:3)

没有目录

 /testingscript

如果要在当前目录文件夹中创建文件

yourCurrentDir/testingscript

您应该删除第一个“/”并使用testingscript / file $ {n} .txt

#!/bin/bash

for n in {1..10}; do
        echo $n
        dd if=/dev/urandom bs=1 count=$(( RANDOM + 1024 )) > testingscript/file${n}.txt
done

检查是否有目录/ testingscript

ls / | grep testingscript

预期输出

testingscript