Christopher Barry on 29 May 2016 18:22:25 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Bash question

On Sun, 29 May 2016 17:26:58 +0000 (UTC)
Gregory Deal <> wrote:

>Even after checking the "learning bash" book and searching, I'm having
>a little trouble explaining some bash functioning. I'm trying to
>process some command output, making an array of its output lines. I
>was trying to check for the "no output" case (which goes right back to
>a prompt) by interrogating the final array length. But even with no
>command output, I get a length of 1. Could someone explain this?
># DTD specifies 0 or 1 <rules>, so check if any before processing. 
>echo "Rules content:" 
> ~/program1/sample2.xml "/sync/rules[1]/text()" | 
>while IFS= read -r line; do 
>echo "-->'$line'" 
>echo "IFS='$IFS'" 
>rulesInfo=$( ~/program1/sample2.xml
>"/sync/rules[1]/text()") <<<<<<< raw output shown below if [[ -z
>$rulesInfo ]]; then echo "No rules found." 
>echo "rulesInfo='$rulesInfo'" 
>unset rulesArray 
>readarray -t rulesArray <<< "$rulesInfo" 
>echo "length=${#rulesArray[@]}" 
>if [[ ${#rulesArray[@]} -eq 0 ]]; then 
>echo "empty" 
>echo "not empty" 
>echo "rulesArray(single echo)=${rulesArray[@]}" 
>echo rulesArray from loop= 
>for element in "${rulesArray[@]}" 
>echo "'$element'" 
>gkd@sisko:~/program1$ sample2.xml
>"/sync/rules[1]/text()" gkd@sisko:~/program1$ echo $? 
>Results : 
>gkd@sisko:~/scripts$ ./ 
>Rules content: 
>No rules found. 
>not empty 
>rulesArray(single echo)= 
>rulesArray from loop= 

t=( '' )
echo ${#t[@]}


[[ ${t[@]} ]] && echo yes
<no output>


test for an actual value in ${rulesArray[@]} as well

(( ${#rulesArray[0]} )) && [[ "${rulesArray[@]}" ]] && do stuff

Philadelphia Linux Users Group         --
Announcements -
General Discussion  --