Sample Header Ad - 728x90

When trying to unzip file from script, I get Segmentation fault (core dumped)

2 votes
1 answer
298 views
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
}
Asked by Chris Pavey (115 rep)
Feb 1, 2025, 11:02 AM
Last activity: Feb 1, 2025, 01:38 PM