Bash - a CD in the Untared directory with a variable URL

This is the situation. I have a list of URLs that I need to extract and configure. Its all variables are controlled, but after I extracted it, I don’t know how my folder will be called. I can’t burn a CD if I don’t know what his name is.

$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

If this does not explain it, tell me. I really want to overcome this problem, but it's hard for me to explain it.

Since I want this to work for any set of URLs that can have two formats like ".tar.gz" or one format ".zip" and can have. in the file name, for example "Python2.3.4" or cannot "Nginx", this is a bit complicated.

+5
source share
5 answers
#! /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
+1
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
0

, $DL_DIR , :

cd `ls -m1`

:

for filename in "$DL_DIR"/*
do
    echo $filename
done;

If necessary, you can perform file checks and other checks.

0
source

I would say split the file extension by $ {FILE ## *.} And do the opposite with the directory name using $ {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

Only one small problem: how do you know if the directory in the archive has the same name in the archive?

0
source

How about this:

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
-1
source

All Articles