'bash script fail with more then one parameters

I have the following script:

function unpack(){
echo "starting";
SaveFolder=/home/"$USER"/Desktop/folde

for var in "$@"
do

if [ -f "$var" ] || [ -d "$var" ]  ; then

case [ file -b in "$var"   ]

  *directory*)
    echo  "file is directory"

    ;;

  *Zip*)
    echo  "file is Zip, use unZip"
    unzip -o $1 -d "$SaveFolder"
    ;;


   *gzip*)
   echo   "file is gzip, use gunzip"
   cp $1   "$SaveFolder"
   gunzip -N -d -f  "$SaveFolder"/$1;

   ;;

    *bzip2*)
     echo   "file is bunzip2, use bunzip"
     bzip2 -dk $1
    cp $1 "$SaveFolder"/
    bzip2 -d -f "$SaveFolder"/$1;
    ;;

   *compress*)
   echo  "uncompress file ending, use ncompress"
    ;;

  *)
      echo "not a valid file"
    ;;
esac
fi
done
}

It is working well when I am calling the function with one argument, for example:

unpack test.zip

but if I try:

unpack test.zip test.bz2

I get this error:

End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in one of archive.bz2 or archive.bz2.zip, and cannot find archive.bz2.ZIP, period.



Solution 1:[1]

Try this Shellcheck-clean modified version of the code:

#! /bin/bash -p

function unpack
{
    echo "starting"

    local -r SaveFolder=$HOME/Desktop/folde

    local var
    for var in "$@"; do
        if [[ ! -e $var ]]; then
            printf "WARNING: '%s' does not exist\\n" "$var" >&2
            continue
        fi

        case $(file -bi -- "$var") in
            'inode/directory;'*)
                printf "'%s' is a directory\\n" "$var"
                ;;

            'application/zip;'*)
                printf "'%s' is a Zip file, use unZip\\n" "$var"
                unzip -o -d "$SaveFolder" -- "$var"
                ;;

            'application/gzip;'*)
                printf "'%s' is a gzip file, use gunzip\\n" "$var"
                cp -- "$var" "$SaveFolder"
                gunzip -N -d -f -- "$SaveFolder/$var"
                ;;

            'application/x-bzip2;'*)
                printf "'%s' is a bzip2 file, use bunzip\\n" "$var"
                bzip2 -dk -- "$var"
                cp -- "$var" "$SaveFolder"
                bzip2 -d -f -- "$SaveFolder/$var"
                ;;

            'application/x-compress;'*)
                printf "'%s' is a compress file, use uncompress\\n" "$var"
                ;;

            *)
                printf "'%s' is not a supported file\\n" "$var"
                ;;
        esac
    done
}
  • I localized the SaveFolder and var variables to avoid potential clashes with variables used in other functions. It's generally a good idea to localize variables that are only used within a function.
  • The test for existence of the file now uses just -e. The old test was not reliable because there are are things that may be found in filesystems that are neither files nor directories (e.g. fifos, sockets, devices).
  • Use the -i (--mime) option to file to get output that is easier and more reliable to process with a program.
  • Replaced most uses of echo with printf. See the accepted, and excellent, answer to Why is printf better than echo?.
  • Fixed the broken case line.
  • Replaced uses of $1 with "$var". I guess the code wasn't fully updated when the function was updated to take more than one argument.
  • Generally added quoting (as recommended by Shellcheck) go avoid breakages if input files contain whitespace or shell globbing metacharacters in their names. See the When Should You Quote? section of Quotes - Greg's Wiki
  • Added -- to commands as necessary to prevent problems if an input file has a name that begins with -. See Bash Pitfalls #2 (cp $file $target).
  • Note that I've done only very cursory testing on the commands for unpacking files. There may be problems with some of them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pjh