我正在尝试在 bash 脚本中解压缩文件,但是在重复输出“解压缩代理文件”消息后,我收到“段错误(核心转储)”错误。
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)
请参阅下面的脚本:
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
输出函数只是为了使输出更容易而编写的,下面仅供参考:
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
}