unpack tar не работает в скрипте, но работает в Терминале.

Если возможно, у меня есть вопрос:
вот что я сделал в Терминале:

$ 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
$
Войти в полноэкранный режим Выйти из полноэкранного режима

Все работает нормально — файл jenkinsfile распаковался. и это мой скрипт: ("$1" = compress.tar в этом случае)

...
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)
Войти в полноэкранный режим Выйти из полноэкранного режима

это Терминал:

$ 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 Это весь мой сценарий:

#! /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 $''; 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)"
Войти в полноэкранный режим Выход из полноэкранного режима

Я был бы признателен, если бы кто-нибудь мог помочь мне! Спасибо

Оцените статью
devanswers.ru
Добавить комментарий