Score:0

How to append values to the list in shell script..?

us flag
#!/bin/bash
cd /pg
file=`ls -l |awk '{print $9}'`
list=()
for i in $a
do
   echo $i
   a=`cat /pg/$i | head  -n  1 |awk '{print $8}' ` #output: first interation 15 & 2nd values 60
   list+=($a)
   
   #a1=`cat /pg/$i | head -n 2 |awk '{print $8}'`
done
echo $list

through this a=`ls -l |awk '{print $9}'` I'm getting two file and iterating through for loop and appending values to list() the list should contains "15","16" but list contains only one value is "15" Please help me to fix the same.

cn flag
Re-defining `$a` in the loop will not alter what `for` is iterating over: demo: `a="1 2 3 4 5"; for i in $a; do a="6 7 8 9"; echo $i; done`. If that is not your intention, pick a different variable name: using "a" for 2 different purposes is confusing.
Score:2
tm flag

To access the whole array, use

echo "${list[@]}"

Using just $list is equivalent to ${list[0]}, i.e. it only shows the first element.

Score:0
kr flag
# create list
hosts1=()
hosts2=()
# add hosts
hosts1+=( host1 )
hosts1+=( host2 )
hosts2+=( host3 )
hosts2+=( host4 )
# combine 2 lists
hosts1+=( ${hosts2[@]} )
# add some more
echo ${hosts1[@]}
hosts1+=( host5 )
hosts1+=( host6 )
echo ${hosts1[@]}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.