bash set -o nounset und unbound variable

Vom einfachen Programm zum fertigen Debian-Paket, Fragen rund um Programmiersprachen, Scripting und Lizenzierung.
Antworten
ren22

bash set -o nounset und unbound variable

Beitrag von ren22 » 22.10.2016 19:16:31

hi,

ich habe ein kleines script was eigentlich läuft solange ich "set -o nounset" weglasse, sobald ich "set -o nounset" benutze erhalte ich
als Fehlermeldung "./f_find_dependencies: line 115: result[@]: unbound variable" , wo liegt das Problem ?

Code: Alles auswählen

#!/bin/bash
###############################################################################
#																																										#
#																																										#
#																																										#
#							f_unload_extension   FUNCTION										        											#
#																																										#
# try to find of a give extension all its dependencies																				      	#
#																																										#
##############################################################################
set -o errtrace
# Trigger error when expanding unset variables.  Same as `set -u'.
#
set -o nounset
#TinyCoreLinux Extensions Folder
TINYCOREEXTENSIONSFOLDER=/etc/sysconfig/tcedir/optional
#Root Directory
ROOTDIR=$(pwd)
#Buildding Directory
BUILDDIR=/mnt/xvda1/tmp
#////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function f_finddepfile() {

var_path_to_file=""

var_depfile_found=false
#	if [ -f $TINYCOREEXTENSIONSFOLDER/$var_extension.tcz.dep ]; then 
#		var_path_to_file=$TINYCOREEXTENSIONSFOLDER
#		var_depfile_found=true
#	fi
}


#/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#///MAIN-FUNCTION.////////////////////////////////////////////////////////////////////////////////////////////////
#/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function f_find_dependencies () {
#set -x
declare -a EXTENSIONS  # indexed array for the extensions
declare -a ANKER	   # indexed helper array for the extensions
EXTENSIONS=(mc)
#
#--- Check for required Stuff to run this script --------------------------
#
#echo "Check for required Stuff to run this script"
#
#[ ! -f /usr/local/tce.installed/util-linux ] && tce-load -wi util-linux
#[ ! -f /usr/local/tce.installed/bc ] && tce-load -wi bc


while [ $(echo ${#EXTENSIONS[@]}) -gt 0 ] ; do
		for var_extension in ${EXTENSIONS[@]}; do
			
			f_finddepfile
			
			echo "START====$var_extension====>"
			echo "var_depfile_found: $var_depfile_found $var_extension.tcz.dep"
		
			if [ "$var_depfile_found" == "true" ]; then

				#case
				case "$var_path_to_file " in
				/*)

					for var_dep_file in $var_extension; do
						
						while read line ; do
							#check for empty line 
							[ -z "$line" ] && continue
								echo "add depencie: ${line%%.*}"
								#add extension to extensionsarray
								EXTENSIONS+=(${line%%.*})
								ANKER+="${var_extension}_@_${line%%.*} "
						done <	$var_path_to_file/$var_dep_file.tcz.dep;
						
						#remove current array-element from array EXTENSIONS[0] 
						#http://stackoverflow.com/a/34199081
						echo "<====removing array element: ${EXTENSIONS[0]} ";
						
						EXTENSIONS_TEMP=("${EXTENSIONS[0]}")   
						result=("${EXTENSIONS[@]}")

						for element in "${EXTENSIONS_TEMP[@]}";	do
							 result=(${result[@]/*${element}*/}) #/*${element}*/
						done

						#(re)set the array EXTENSIONS=( "${result[@]}" ) 
						EXTENSIONS=( "${result[@]}" )
						#echo -e "extensions@ ==ende=============================${EXTENSIONS[@]}\n"
					done;;

				esac
				#echo -e "extensions@ ==ende=============================${EXTENSIONS[@]}\n"	
				#echo -e "extensions@ ==anker============================${ANKER[*]}\n"	

			fi
		
			if [ "$var_depfile_found" == "false" ]; then
				#remove current array-element from array EXTENSIONS[0] 
				#http://stackoverflow.com/a/34199081
				echo "<====removing array element: ${EXTENSIONS[0]} ";
						
				EXTENSIONS_TEMP=("${EXTENSIONS[0]}")   
				result=("${EXTENSIONS[@]}")

				for element in "${EXTENSIONS_TEMP[@]}"; do
					result=(${result[@]/*${element}*/}) #/*${element}*/
					#[[ -z $result ]] && echo "$result" || echo "eeeemmmmppptttyyyyy"
				done
set -x
				#(re)set the array EXTENSIONS=( "${result[@]}" ) 
				#[[ -z $result ]] && echo "$result" || echo "eeeemmmmppptttyyyyy"
				EXTENSIONS=( "${result[@]}" )
				#EXTENSIONS[${#EXTENSIONS[*]}]
				#echo -e "extensions@ ==ende=============================${EXTENSIONS[@]}\n"

			fi
		done
done
echo -e "extensions@ ==ende=============================${EXTENSIONS[@]}\n"	
echo -e "extensions@ ==anker============================${ANKER[*]}\n"


}


#}

f_find_dependencies
danke ren22

newdeb
Beiträge: 134
Registriert: 03.02.2011 11:11:21
Lizenz eigener Beiträge: MIT Lizenz
Wohnort: Frankfurt

Re: bash set -o nounset und unbound variable

Beitrag von newdeb » 22.10.2016 21:40:15

Bash-Manpage sagt (Abschnitt "Arrays"):
An array variable is considered set if a subscript has been assigned a value. The null string is a valid value.
Also:

Code: Alles auswählen

$ set -o nounset
$ ARR=()
$ VAR=${ARR[0]}
bash: ARR[0] ist nicht gesetzt.
$ ARR[0]=
$ VAR=${ARR[0]}
$
Siehe auch http://stackoverflow.com/questions/7577 ... with-set-u

Antworten