Bash脚本返回“找不到"错误

编程入门 行业动态 更新时间:2024-10-13 22:21:09
本文介绍了Bash脚本返回“找不到"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写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脚本返回“找不到"错误

本文发布于:2023-11-24 12:37:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   脚本   错误   Bash   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!