-5

I am a new user to unix that is trying to do a simple script in bash that will unzip several files in the location that I list. I can't figure out why it keeps crashing. I'm inserting the script below. I hope to later move the old .tar.7z zipped files into another directory (hence my creating the new directory in the script below), but I am just trying to get the main part working first.

#!~/bash
# My program to try to unzip several files with ending of tar.7z
# I have inserted the ability to enter the directory where you want this to be done 

echo "What file location is required for unzipping?"

read dirloc

cd $dirloc
mkdir oldzippedfiles
for directory in $dirloc
        do
                if
                [ $directory=*.tar.7z ]
                then
                cat $directory | 7za x -an -txz -si -so | tar -xf -
        fi
        done

echo "unzipping of file is complete"

exit 0
3
  • 4
    What do you mean by crashing? You have a wrong shebang line is that a typo? What error are you getting? Are you on Unix or Linux? If Linux, which Linux? Why are you using cat? Could you give an example of a directory you want to run this script on?
    – terdon
    Mar 5, 2013 at 13:01
  • Sorry, I am on a linux machine. When I mentioned that it was crashing, the terminal window shut down. I was using the cat command to read the contents of the file. So, this leads me to the obvious thing I was doing wrong and that was directory (above) should be replaced with filename - sorry to be so unclear, but I've gotten the script to run. Thanks Mar 5, 2013 at 13:16
  • 3
    How did you get it to run?
    – slhck
    Mar 5, 2013 at 13:22

1 Answer 1

2

There are a lot of errors in your script:

  1. #!~/bash won't work since it's bash that expands ~ to your home directory. You have to specify the full path to bash (usually /bin/bash).

  2. for directory in $dirloc won't work because of two reasons:

    • To go through the files inside $dirloc, you'd need $dirloc/*.

    • You already changed directories, so it has to be plain *.

    Also, since you're looping through files, I suggest renaming your variable.

  3. test ([) doesn't support glob matching.

    Use for file in *.tar.7z instead.

  4. There is no need to use cat. It makes things more difficult, since 7zip can't guess the archive format from the extension. -txz won't work, unless you actually created an .xz file fith a .7z extension (bad practice).

    Use 7za a -so $directory instead.

  5. I'm not sure what the -an switch is supposed to do, but it throws errors and I'm pretty certain it's not needed.

A working version of your script should look like this:

#!/bin/bash

read dirloc

cd $dirloc

for file in *.tar.7z
        do
                7za x -so $file | tar -xf -
        done
1
  • 1
    This is barely legible as a comment. You can post your corrected approach as an answer if it is working.
    – Dennis
    Mar 5, 2013 at 16:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.