Regex 用变量URL将Bash-CD放入未加密的目录
情况就是这样。我有一个需要提取和设置的URL列表。它都是变量驱动的,但在我提取之后,我不知道我的文件夹将被称为什么。如果我不知道它叫什么,我就不能把它录下来Regex 用变量URL将Bash-CD放入未加密的目录,regex,bash,shell,unix,Regex,Bash,Shell,Unix,情况就是这样。我有一个需要提取和设置的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?&
$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
$DL_DIR=/opt/
$URL=http://nginx.org/download/nginx-1.3.3.tar.gz
$FILE=${URL##*/}
$CONFIG=“--core”
cd“$DL_DIR”
wget$URL
tarxzf$文件
光盘
/配置“$CONFIG”
制作
安装
rm$文件
如果这不能解释,请说。我真的很想克服这个问题,但我很难解释它
因为我想让它对任何一组URL起作用,这些URL可能有两种格式,比如“.tar.gz”或一种格式“.zip”,文件名中可能有“.s”,比如“Python2.3.4”,或者可能没有“Nginx”,这使得它有点棘手
extract_dir=$(tar -tf $FILE | cut -d/ -f1 | uniq)
cd $extract_dir
或
或
如果您知道$DL_DIR中只有一个目录,那么您可以使用: cd `ls -m1` cd`ls-m1` 另一种方法是循环遍历目录中的文件: for filename in "$DL_DIR"/* do echo $filename done; 对于“$DL_DIR”中的文件名/* 做 echo$filename 完成; 您可以根据需要执行文件测试和其他检查。这样如何:
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 中的案例${FILE##*.} (广州) tarxf$文件 cd${FILE%.tar.gz*} ;; tgz) tarxf$文件 cd${FILE%.tgz*} ;; (拉链) 解压缩$FILE cd${FILE%.zip*} ;; 以撒
只有一个小问题:如何知道归档文件中的目录与归档文件本身是否同名?这是一个棘手的问题。文件格式并不总是tar,它可能是.zip或.tar.gz。好主意,但是这个目录中会有很多。我们可以按时间做吗?是否复制最新创建的文件夹的名称?不过这似乎不是一个好方法。@JamesWillson:在这种情况下,您可以循环使用$DL_DIR中的文件。请参阅我编辑的答案。顺便说一下,您的前四行在bash中不是有效的作业。左侧不应以美元符号作为前缀,等号周围不允许有空格。一个更正的示例:
DLDIR=/opt/
。您也可以使用basename:cd$(basename$文件)
顺便说一句,查看/usr/bin/lessipe以了解一个好的归档检测案例。。。以撒
#! /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
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
#! /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