我正在编写bash脚本以从目录中获取最新文件以进行备份.这是脚本:
I'm writing a bash script to get the latest file from a directory for backup purposes. Here is the script:
#!/bin/sh set -u set -e backup_dir=/media/backup cd $backup_dir tar_file= $(ls -Art | tail -n 1) #ls -Art | tail -n 1 echo $tar_file当我运行脚本时,它会获取正确的文件,但还会返回未找到的错误,我也不知道为什么:
When I run the script it gets the right file but also returns a not found error and I don't know why:
./backup: 10: 20130403-120001.tar.gz: not found我用注释掉的行对其进行了测试,而不是将其作为变量使用,并且在不引发错误的情况下可以正常工作.
I tested it with the line that's commented out, not putting it as a variable and that works without throwing an error so it should work.
推荐答案删除 = 后的空格:
tar_file=$(ls -Art | tail -n 1)使用空格,该行将被解释为对 $ tar_file 不分配任何内容,并运行作为 $(...)的输出获得的命令.找不到该命令.
With the space, the line is interpreted as assigning nothing to $tar_file and running the command that is obtained as the output of the $(...). That command is not found.
更多推荐
Bash脚本返回“找不到"错误
发布评论