2

I'm trying to unzip a file in a bash script, but I get a 'Segmentation fault (core dumped)' error following a repeated output message of 'Unzip agent files.'

Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Unzip agent files.
Segmentation fault (core dumped)

Please see script below:

cat /usr/local/bin/release-agent.sh
#! /bin/bash -e

source functions.sh

arg1=$1

check_arg (){
        if [[ -z ${arg1} ]]; then
                output "ERROR! Usage: Agent-unzip.sh [local zip file]..." red
                exit 0
        else
                arg=${arg1}
                if [[ ! -f ${arg} ]]; then
                        output "No local file found!" blue
                        exit 0
                else
                        file=${arg}
                        ext=${file##*.}
                        if [[ ${ext} != "zip" ]]; then
                                output "File specified does not appear to be a zip." red
                                confirm "Would you like to proceed."  #yes no question

                                if [[ $? == "1" ]]; then  #if no
                                        exit 0
                                fi
                        else
                                output "Local file, ${file} found." blue
                        fi
                fi
                zip="${file##*/}"
        fi
}


select_version (){
        prompt="Enter agent version number: "
        read -p "${prompt}" version

        while [ -z ${version} ]; do
                output "No input entered" red
                read -p "${prompt}" version
        done

        output "You entered: ${version}" blue
}

create_dir (){
        path="Agent/agent-${version}"
        output "Creating working directory ${path}" blue
        mkdir -p "${path}"
}

unzip (){
        output "Unzip agent files." blue
        unzip -uq "$zip" -d "${path}"
}

conf_details (){
        output "See details entered below:" blue
        output "VERSION = ${version}" green
        output "FILE PATH = ${zip}" green
        confirm "Would you like to proceed? press 'Y' to initiate the release of the agent image, press 'N' to edit any details:"  #yes no question
        if [[ $? != "0" ]]; then  #if anything but yes is returned
                $0 ${arg}
                exit 0
        fi
}

check_arg
select_version
conf_details
create_dir
unzip

## If script has't crashed out ##
output "Success! All operations completed" green
exit 0

The output function is just something I've written to make output easier, this is below for reference:

output() {
#
# Useage output "string" [colour] {flash}
#

if [[ $3 == "flash" ]]; then
        _blink=";5"
else
        _blink=""
fi

case $2 in
        grey)
                echo -e "\e[30;1${_blink}m${1}\e[0m"
                ;;
        red)
                echo -e "\e[31;1${_blink}m${1}\e[0m"
                ;;
        green)
                echo -e "\e[32;1${_blink}m$1\e[0m"
                ;;
        yellow)
                echo -e "\e[33;1${_blink}m$1\e[0m"
                ;;
        blue)
                echo -e "\e[34;1${_blink}m$1\e[0m"
                ;;
        magenta)
                echo -e "\e[35;1${_blink}m$1\e[0m"
                ;;
        lightblue)
                echo -e "\e[36;1${_blink}m$1\e[0m"
                ;;
        white)
                echo -e "\e[37;1${_blink}m$1\e[0m"
                ;;
        *)
                echo -e "\e[0mNo formatting option!"
                ;;
esac
}
2
  • 2
    try to exec the script like this: bash -x script_name Commented Feb 1 at 11:44
  • 2
    If it's unzip that's crashing then try in unzip -l file.zip, unzip -t file.zip, unzip -d dir file.zip.
    – Fravadona
    Commented Feb 1 at 12:46

1 Answer 1

6

Your unzip function is calling itself, which ends up causing a segmentation fault (in the shell, not unzip).

To fix that, call

command unzip

in the function instead of plain unzip.

2
  • 5
    You could also not name your function, which I'm guessing is only used inside your script, the same name as a commonly used existing program.
    – JonathanZ
    Commented Feb 1 at 19:29
  • 1
    Thanks. This solved my issue. I feel so stupid. Commented Feb 1 at 22:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.