我正在尝试在 bash 中创建我的第一个 shell 脚本。我已经创建了代码,并设法将脚本保存在我的主目录中,但它不会运行。起初,我尝试从主目录运行它: ./testscript.sh
以">权限被拒绝"作为响应,然后我尝试sudo ./testscript.sh
然后">找不到命令"。
这是我的脚本:
#!/bin/bash
mkdir -p/home/filer
touch /home/filer/fil1
touch /home/filer/fil2
touch /home/filer/fil3
tar-zcvf file.tar.gz /home/filer
所以我尝试创建一个脚本,在我的主目录中创建一个名为"filer"的目录,使用 touch 在"filer"目录中创建 3 个单独的文件,然后从整个"filer"目录中创建一个 tar.archive。我认为脚本是正确的,我可以用手运行脚本。
除了几个拼写错误(mkdir -p/path
-> mkdir -p /path
, tar-zcvf
... -> tar -zcvf
...(,您应该使用环境变量引用$HOME
主目录。 /home/filer
是一个绝对目录路径,我假设它不是您的实际主目录。
#!/bin/bash
mkdir -p $HOME/filer
touch $HOME/filer/fil1
touch $HOME/filer/fil2
touch $HOME/filer/fil3
tar -zcvf file.tar.gz $HOME/filer
您可以执行脚本,./testscript.sh
bash testscript.sh
或./testscript.sh
。
在第二种情况下,脚本需要具有适当的可执行权限。 chmod +x ./testscript.sh
为其提供完全可执行权限。