unpack tar doesn't work for me in the script but it does in Terminal
Published: 2022-07-21 20:19:00

If possible I have a question:
this what I did in the Terminal:

```
$ tar cvf compress.tar jenkinsfile
jenkinsfile
$ ls
compress.tar  jenkinsfile
$ rm jenkinsfile
$ ls
compress.tar
$ tar --overwrite -xvf compress.tar
jenkinsfile
$ ls
compress.tar  jenkinsfile
$
```

It works fine- the file jenkinsfile unpacked. and this is my script: (`"$1"` = compress.tar in this case)

```
...
function func_decompress(){
    check=0
    case $1 in
    *.tar.gz | *.tar | *.tar.bz2)
        check=1
        tar --overwrite -xvf "$1" 
        rm "$1" ;;
    *.zip)
        check=1
        unzip -o "$1"
        rm "$1" ;;
    *.bz2)
        check=1
        bunzip2 "$1" ;;
    *.gz)
        check=1
        gunzip "$1" ;;
    esac
    if [ $check == 0 ]
    then
        uncompressPrintFunc "$1"
    else
        compressPrintFunc "$1"
    fi
}
...
```

```
$ ./bcd.sh -v -r bcd
Ignoring 3077ed8635670985d31fdbf043bd3d60.jpg ...
jenkinsfile
Unpacking compress.tar ...
Ignoring jenkinsfile ...
Ignoring file.txt ...
Ignoring file1.txt ...
Ignoring file2.txt ...
Decompressed 1
(failure for 5 files)
```

this is the Terminal:

```
$ ls
compress.tar  jenkinsfile
$ rm jenkinsfile
$ ls
compress.tar
$ cd ../..
bcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcd$ ./bcd.sh -v -r bcd
Ignoring 3077ed8635670985d31fdbf043bd3d60.jpg ...
jenkinsfile
Unpacking compress.tar ...
Ignoring file.txt ...
Ignoring file1.txt ...
Ignoring file2.txt ...
Decompressed 1
(failure for 4 files)
bcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcdbcd$ cd bcd/bcdbcdbcd/
$ ls
$
```

ps This is all my script:

```
#! /usr/bin/bash

array=($@)
new_array=()
for value in "${array[@]}"
do
    [[ $value != '-r' && $value != '-v' ]] && new_array+=($value)
done
array=("${new_array[@]}")
unset new_array

count_compress_files=0
count_uncompress_files=0
option_r=false
option_v=false

while getopts "rv" opt; do
    case $opt in
        r)  
            option_r=true
            ;;  
        v)  
            option_v=true
            ;;  
    esac
done

function func_recursive() 
{
            array=()
            while IFS=  read -r -d $'\0'; do
                array+=("$REPLY")
            done < <(find $1 -type f -follow -print0)
            for i in "${array[@]}"
            do
                func_decompress "$i"
            done   
}

function func_decompress(){
    check=0
    case $1 in
    *.tar.gz | *.tar | *.tar.bz2)
        check=1
        tar -c -xvf "$1" 
        rm "$1" ;;
    *.zip)
        check=1
        unzip -o "$1"
        rm "$1" ;;
    *.bz2)
        check=1
        bunzip2 "$1" ;;
    *.gz)
        check=1
        gunzip "$1" ;;
    esac
    if [ $check == 0 ]
    then
        uncompressPrintFunc "$1"
    else
        compressPrintFunc "$1"
    fi
}

function uncompressPrintFunc(){
    if [ $option_v == true ]
        then
            file=$(extraction_func "$1")
            echo "Ignoring $file ..."
        fi
    count_uncompress_files=$(( count_uncompress_files+1 ))
}

function compressPrintFunc(){
    if [ $option_v == true ]
        then
            file=$(extraction_func "$1")
            echo "Unpacking $file ..."
        fi
    count_compress_files=$(( count_compress_files+1 ))
}

function extraction_func(){
    filepath="$1"
    FILE=${filepath##*/}
    echo "$FILE"
}

function func_not_recursive_folder(){
    echo "I am not_recursive_folder"
    echo "check 1: $1"
    i=0
    filepath
    while read line
    do
        filepath=$(realpath -s "$line")
        array[ $i ]="$filepath"        
        (( i++ ))
    done < <(ls $1)
    for packedfile in "${array[@]}"
    do
        func_decompress "$packedfile"
    done
    
}

for i in "${array[@]}"
do
    bcd=$(find ~+ -name "$i")
    if [[ -d $bcd ]]
    then
        if [ $option_r == true ]
        then
            func_recursive $bcd
        else
            func_not_recursive_folder $bcd
        fi
    elif [[ -f $bcd ]]
    then
        func_decompress $bcd
    else
        echo "This input: $i is not valid!"
        exit 1
    fi
done

echo "Decompressed $count_compress_files"
[[ "$count_uncompress_files" -gt 0 ]] && echo "(failure for $count_uncompress_files files)" || echo "(success)"
```

I would appreciate it if someone could help me! Thanks