1

I have a directory with all tgz files. Currently, I have a script that I run that will

  1. take in the directory as first parameter
  2. extract the tgz
  3. cd into the extracted folder
  4. and then run my load script
  5. and go on to the next tgz..

Shown below.

Currently this script works, but it will fail if someone puts in a parameter other than just the directory name (e.g, ./myScript /whole/path/to/directory)

dirname=$1
cur_dir=`pwd`

echo The directory you are about to extract is $dirname

echo $(ls ${cur_dir}/${dirname}/*.tgz)

echo Your current directory is ${cur_dir}

for tgz in $(ls ${cur_dir}/${dirname}/*.tgz) ; do  # <---- I think there is a better way of doing this please advise
        echo the tgz you are about to extract now is ${tgz}
        cd ${dirname};             #cd into input directory 
        tar -xvzf ${tgz};          #extract tgz
        cd ${tgz%%.tgz};           #cd into the folder you extracted 
        loadItExit;                #run my other script to load
        cd ../..;                  #move up two levels back to original path

done
codeforester
  • 39,467
  • 16
  • 112
  • 140
tiger_groove
  • 956
  • 2
  • 17
  • 46

1 Answers1

2

something that looks like this could work

dirname="$1"
for tgz in $(ls ${dirname}/*.tgz) ; do 
    echo "tar -xzf ${tgz}";  
    echo "cd  ${tgz%%.tgz}"; 
    echo "LoadIt"; 
    echo "cd .."; 
done

obviously the echos are just for demonstration

The comment to avoid using ls is fair if you are not in control of your environment. Push your filter for files of interest further into a loop over a less specific collection.

depending on how little control you have, you may need more paranoid tests.

dirname="$1"
for tgz in ${dirname}/* ; do
   if [ "${tgz##*\.}" == "tgz" ] then
        echo "tar -xzf ${tgz}";  
        echo "cd  ${tgz%%.tgz}"; 
        echo "LoadIt"; 
        echo "cd .."; 
   fi
done

EDIT for comment to explain the if statement

first the char [ in bash is the same command as test you want to the keep spaces after [ and before ]

inside the [ ... ] is an expression (a predicate) that evaluates to true or false

In this case we are using variable substitution
where the value of a shell variable, $tgz here,
is being modified before it is used

It is always safest to enclose the name of the variable after the $ in braces { ... }

However doing so also allows you to annotate the variable call
to substitute the normal variable value with a modified value

my quick and dirty mnemonic for remembering the annotations
and their functions is to look to either side of the $ key
(sorry non-english non-qwerty keyboard layouts)
and notice the # hash or pound character proceeds $
so ${name#<regexp>} will delete the first match
of the regular expression <regexp>
after the # and before the }
from the beginning of the variables value.

likewise the % is the key after $ and ${name%<regexp>}
will delete the last match of the regular expression
from the value of the variable

Both annotations can be doubled ## or %% to mean:
delete the longest match of the regular expression
instead of the shortest.

so
"${tgz##*\.}" == "tgz"
means delete everything up to and including the last dot
from the value in the variable $tgz and see if what remains
is exactly equal to the string "tgz"

web searches on "bash variable substitution"
and "bash test" will take you further

tomc
  • 1,146
  • 6
  • 10