gpt4 book ai didi

regex - Bash - 使用可变 URL CD 进入 Untared 目录

转载 作者:行者123 更新时间:2023-11-29 09:06:31 26 4
gpt4 key购买 nike

情况是这样的。我有一个需要提取和设置的 URL 列表。它都是变量驱动的,但在我提取后,我不知道我的文件夹将被称为什么。如果我不知道它叫什么,我就不能 CD 进去。

$DL_DIR = /opt/
$URL = http://nginx.org/download/nginx-1.3.3.tar.gz
$FILE=${URL##*/}
$CONFIG = "-- core"

cd "$DL_DIR"
wget $URL
tar xzf $FILE
cd <HOW DO I GO INTO IT?>
./configure "$CONFIG"
make
make install
rm $FILE

如果这不能解释,请说明。我真的很想解决这个问题,但我很难解释它。

因为我希望它适用于任何一组 URL,这些 URL 可能有两种格式,如“.tar.gz”或一种格式“.zip”,并且文件名中可能有 .'s,如“Python2.3.4”,或者可能不是“Nginx”,这让它有点棘手。

最佳答案

#! /bin/bash
#
# Problem:
# find the path of the "root" folder in an archive
#
# Strategy:
# list all folders in the archive.
# sort the list to make sure the shortest path is at the top.
# print the first line
#
# Weak point:
# assumes that tar tf and unzip -l will list files in a certain way
# that is: paths ending with / and that the file-list of unzip -l
# is in the fourth column.
#

LIST_FILES=
FILE=$1
case ${FILE##*.} in
gz)
LIST_FILES="tar tf $FILE"
;;
tgz)
LIST_FILES="tar tf $FILE"
;;
zip)
LIST_FILES='unzip -l '$FILE' | awk "{print \$4}"'
;;
esac
ARCHIVE_ROOT=$(
echo $LIST_FILES | sh |\
grep '/$'|\
sort |\
head -n1
)

# we should have what we need by now, go ahead and extract the files.
if [ -d "$ARCHIVE_ROOT" ]; then
cd "$ARCHIVE_ROOT"
else
# there is no path (whoever made the archive is a jerk)
# ...or the script failed (see weak points)
exit 1
fi

关于regex - Bash - 使用可变 URL CD 进入 Untared 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com