for loop - How to create a correct array with ls? -
i know can use array=( $( ls . ) )
have next problem code:
array=( $( ls ./cos/cos*.txt ) ) (( = 0 ; <= ${#array[*]}-1; i++ )) sed 's/$'"/`echo \\\r`/" ${array[$i]} > ./cos/temp.txt mv ./cos/temp.txt ${array[$i]} done
i have more loops in whole script differents directories respective instructions of sed , mv no problems, have problem part of code, appears command ls saving entire result in first possition of array, i.e. if cos directory has cos1.txt, cos2.txt , cos3.txt, instead of saving cos1.txt in ${array[0]}, cos2.txt in ${array[1]} , cos3.txt in ${array[2]} saving:
cos1.txt cos2.txt cos3.txt in ${array[0]}, entire list in possition 0 of array. know wrong?
it's not clear actual problem is, should write code this:
# don't use ls. let glob expand list of files array=( ./cos/cos*.txt ) # don't iterate on array indices; iterate on items fname in "${array[@]}"; do # trying add carriage return end of each line? sed "s/\$/$'\r'/" "$fname" > ./cos/temp.txt mv ./cos/temp.txt "$fname" done
you don't need array; can put glob in loop:
for fname in ./cos/cos*.txt; ... done
Comments
Post a Comment