我正在尝试编写一个脚本,其中有一个 if 语句,该语句必须检查特定文件夹是否包含具有特定扩展名的包。如果是这样,则必须将其拆包。
if [ installation = "1" ]; then
if ls /usr/local/src grep -qF ".tar.gz"; then
tar -zxvf $package #it has to unpack the package
elif ls /usr/local/src grep -qF ".tar.bz2"; then
tar -xvfj $package #it has to unpack the package
fi
./configure
elif [ installation = "2" ]; then
dpkg -i $package #it has to install the deb package
fi
可以这样写吗?
没有$package
使用,但我写它是为了向您展示我的意思。我不知道如何让它知道它必须解压/安装扩展名为 .tar.gz 或 .tar.bz2 或 .deb 的已创建文件夹
答案1
像这样的东西?
#!/bin/bash
cd /usr/local/src
if [ installation = "1" ]; then
for package in *.tar.gz
do
tar -zxvf "${package}"
done
for package in *.tar.bz2
do
tar -xvfj "$package" #it has to unpack the package
done
./configure
elif [ installation = "2" ]; then
dpkg -i "$package" #it has to install the deb package
fi
答案2
你可以使用这样的东西。
if [ installation = "1" ]; then
for package in *.tar.*
do
tar -xvf ${package} # Unpack (Let tar detect compression type)
done
./configure
elif [ installation = "2" ]; then
dpkg -i ${deb_package} #it has to install the deb package
fi
无需通过ls
/ grep
hacks手动检测压缩类型。
读取存档时必须指定解压缩选项的唯一情况是从不支持随机访问的管道或磁带驱动器读取。然而,在这种情况下,GNU tar 将指示您应该使用哪个选项。例如:
$ cat archive.tar.gz | tar tf - tar: Archive is compressed. Use -z option tar: Error is not recoverable: exiting now
如果您看到此类诊断信息,只需将建议的选项添加到 GNU tar 的调用中即可:
$ cat archive.tar.gz | tar tzf -