Bash - CD变成不可变目录的可变URL
这就是这种情况。我有一个需要提取和设置的URL列表。它的所有变量驱动,但在我解压后,我不知道我的文件夹将被调用。如果我不知道它叫什么,我不能将CD放入它。Bash - CD变成不可变目录的可变URL
$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
如果这不解释,请说。我真的想要通过这个问题,但我很难解释它。
因为我希望它适用于任何可能有两种格式(如“.tar.gz”或一种格式为“.zip”)的URL,并且可能具有。的文件名如“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
顺便说一句,看看/ usr/bin/lesspipe一个好的归档检测案例... esac – 2012-09-22 12:46:42
extract_dir=$(tar -tf $FILE | cut -d/ -f1 | uniq)
cd $extract_dir
或
extract_dir=$(tar -tf $FILE | head -1 | cut -d/ -f1)
cd $extract_dir
或
ls > .dir_list_1 # save current directory listing as hidden file
tar xzf $FILE # extract the $FILE
ls > .dir_list_2 # save the directory listing after extraction...
# ...as another hidden file
# diff two lists saved in hidden files, this will help you get the created dir
# grep '>' symbol, to get the inserted line
# use head to get the dir in case there are multiple lines (not necessary)
# use cut to remove the '>' and get the actual dir name, store in extract_dir
extract_dir=$(diff .dir_list_1 .dir_list_2 | grep '>' | head -1 | cut -d' ' -f2)
# remove temporary files
rm .dir_list_*
cd $extract_dir
这是棘手的地方。文件格式不会总是焦油它可能是.zip或.tar.gz – 2012-07-15 11:34:48
我对这个东西真的很陌生,所以你能介意解释它的功能吗?我无法绕过它。 – 2012-07-15 12:14:55
非常感谢你 – 2012-07-16 09:29:21
如果你知道有将是在$ DL_DIR只有一个目录,那么你可以使用:
cd `ls -m1`
另一种方法是循环遍历文件目录:
for filename in "$DL_DIR"/* do echo $filename done;
您可以根据需要执行文件测试和其他检查。
好主意,但在这个目录中会有很多。我们可以根据时间来做吗?复制最新创建的文件夹的名称?看起来好像是一种糟糕的做法。 – 2012-07-15 11:37:00
@JamesWillson:在这种情况下,您可以遍历$ DL_DIR中的文件。看到我编辑的答案。 – 2012-07-15 11:46:39
如何:
rm -rf tmpdir
mkdir tmpdir && cd tmpdir || exit
wget "$URL" || exit 1
case "$(ls)" in
*.tar.gz|*.tgz)
tar xzf $(ls)
;;
*.zip)
unzip $(ls)
;;
esac
for d in $(ls -d)
do
(cd "$d" 2>/dev/null && ./configure && make && make install;)
done
我会说,用$剥离文件的扩展名{FILE ## *},并使用$使用目录名左右做其他的方式{FILE% .ext *}:
case ${FILE##*.} in gz) tar xf $FILE cd ${FILE%.tar.gz*} ;; tgz) tar xf $FILE cd ${FILE%.tgz*} ;; zip) unzip $FILE cd ${FILE%.zip*} ;; esac
只有一个小问题:如何知道存档中的目录是否与存档本身具有相同的名称?
你也可以使用basename作为这个: 'cd $(basename $ FILE)' – HighKing 2012-09-01 09:44:16
顺便说一下,前四行不是bash中的有效赋值语句。左侧不应以美元符号为前缀,等号周围不得有空格。一个更正的例子:'DLDIR =/opt /'。 – chepner 2012-07-15 14:42:43