加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – Bash – CD到变量URL的Untared目录

发布时间:2020-12-14 06:07:13 所属栏目:百科 来源:网络整理
导读:情况就是这样.我有一个我需要提取和设置的URL列表.它的所有变量驱动,但在我提取后,我不知道我的文件夹将被调用.如果我不知道它叫什么,我就不能把CD放进去. $DL_DIR = /opt/$URL = http://nginx.org/download/nginx-1.3.3.tar.gz$FILE=${URL##*/}$CONFIG = "-
情况就是这样.我有一个我需要提取和设置的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,它们可能有两种格式,如“.tar.gz”或一种格式“.zip”,并且文件名可能有.如“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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读