linux - tar :cannot stat : No such file or directory when shell script run

标签 linux bash shell sh

我希望这里有更多的专家能发现一些明显的语法错误,或者能够帮助我解决这些错误。

脚本的基本思路: 我有一个 shell 脚本,它将读取 list.txt 并查找我需要的文件或文件夹,然后复制到另一个目录。

工作环境: Ubuntu 12.04

问题/错误: 在我修改之前,我有一个演示脚本,可以复制所有可以正常工作的文件。但是,当我修改脚本时,它给了我这个错误: tar : XXXXX.ko 无法统计 : 没有那个文件或目录 tar : 由于先前的错误而以失败状态退出

我唯一改变的是列表的路径。

Shell脚本代码如下所示

#!/bin/sh
cd /home/leo/Desktop/Script_testing
INSTALL_PATH=./output/.install/
NAND2_PATH=./output/nand1-2_2
if [ ! -d $NAND2_PATH ]; then
mkdir $NAND2_PATH
fi
if [ ! -f /home/leo/Desktop/Script_testing/list2.txt ]; then
echo "error : list_2 not found : "
exit
fi
cd $INSTALL_PATH
tar cf - `cat /home/leo/Desktop/Script_testing/list2.txt` | ( cd ../../../${NAND2_PATH} ; tar xf - )

这是我的 list2.txt :

./app_drvier/led/led.ko
./Desktop/testing/beeper.h
./Desktop/testing/beeper.c
./bin/usb_plug.sh
./etc/hostname

当我运行 ./mycp.sh 目录“nand1-2_2: 将在输出下创建,但是,我提到的错误显示 tar : XXXXX 无法统计 : 没有那个文件或目录 tar :由于先前的错误而以失败状态退出

希望有人能帮帮我。非常感谢

更新 在我运行 sh -x mycp.sh 后,结果如下

+ cd /home/dragon/Desktop/Script_testing
+ INSTALL_PATH=./output/.install/
+ NAND2_PATH=./output/nand1-2_2
+ [ ! -d ./output/nand1-2_2 ]
+ [ ! -f /home/dragon/Desktop/Script_testing/list2.txt ]
+ cd ./output/.install/
nand1-2_2.sh: 12: cd: can't cd to ./output/.install/
+ cd ../../.././output/nand1-2_2
nand1-2_2.sh: 13: cd: can't cd to ../../.././output/nand1-2_2
+ tar xf -
+ cat /home/dragon/Desktop/Script_testing/list2.txt
+ tar cf - ./app_drvier/ontech_led/onetech.led.ko
tar: ./app_drvier/ontech_led/onetech.led.ko: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

最佳答案

错误消息说文件 ./app_drvier/ontech_led/onetech.led.ko 不存在。这并不奇怪,因为之前执行的两个 cd 命令都失败了。

根本原因似乎是目录 ./output/.install 不存在或者您没有权限将 chdir 放入其中。

我建议您始终评估 cd 命令的退出状态。如果 cd 失败,继续运行脚本是没有意义的。

例如,你可以这样写:

cd $INSTALL_PATH || exit 1

关于linux - tar :cannot stat : No such file or directory when shell script run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735626/

相关文章:

linux - 定义带或不带导出的变量

linux - 期望 - 不按要求发送文本

linux - 模式匹配在 bash 脚本中不起作用

windows - 创建 ELF 镜像

linux - Shell Bash 脚本不起作用

php - 错误替换 Bash : how to write a heredoc to a dynamically created file (variable)?

shell - 如何在unix中使用进程ID仅获取进程名称

Linux 驱动程序轮询?

java - 如何从java后端使用sudo执行shell脚本?

bash - 如何将参数传递给 makefile 中的目标?