简体   繁体   English

untar 命令在终端中有效,但在 bash 脚本中无效

[英]untar command works in terminal but not in bash script

I am trying to download some public dataset from the inte.net and untar it.我正在尝试从 inte.net 下载一些公共数据集并解压它。 I am doing it in a loop since I would like to download several.tar.gz files, put them in a folder and untar them all.我正在循环执行此操作,因为我想下载几个 .tar.gz 文件,将它们放在一个文件夹中并全部解压。 The download works and the tar command works in the terminal but not in my bash script.下载有效,tar 命令在终端中有效,但在我的 bash 脚本中无效。 I do not understand this inconsistent behavior.我不明白这种不一致的行为。

#!/bin/sh

PATH="$(pwd)/data"
cd $PATH

/usr/bin/curl "https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/myfoodrepo/round-2/val.tar.gz" --output val.tar.gz

for file in *.tar.gz
do
    /bin/tar zxf $file
done

You are attempting to use sh in the script but you seem to have been working with bash in your terminal.您正在尝试在脚本中使用sh但您似乎一直在终端中使用bash Test if this works:测试这是否有效:

#!/bin/bash

directory="$(pwd)/data"
cd $directory

curl "https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/myfoodrepo/round-2/val.tar.gz" --output val.tar.gz

for file in *.tar.gz
do
    tar zxf $file
done

Let me know if it helps!让我知道是否有帮助!

Note:笔记:

I also removed absolute paths as they are not needed and change the name of your PATH local variable to directory since PATH is already a variable in your environment for sure.我还删除了不需要的绝对路径,并将PATH局部变量的名称更改为目录,因为PATH肯定已经是您环境中的变量。

Using "PATH" as variable name overwrites an existing environment variable.使用“PATH”作为变量名会覆盖现有的环境变量。 This causes tar to not find things it needs (gzip or bzip2 etc.)这导致 tar 找不到它需要的东西(gzip 或 bzip2 等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM