Using shell-script,
How do I read files from another server (user@192.168.10.x:/home/admin/data) and store it in "files" array? (4th line of code)
1 reportTypes=(0001 0102 8902)
2
3 # collect all files matching expression into an array
4 files=(Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv)
5
6 # take only the first hundred
7 files=( "${files[@]:0:100}" )
8
9 shopt -s nullglob # allow a glob to expand to zero arguments
10
11 # echo ${files[@]}
12
13 for i in ${reportTypes[@]}; do
14 printf -v val '%04d' "$i"
15 groupFiles=( $( for j in ${files[@]} ; do echo $j ; done | grep ${val} ) )
16
17 # Generate sequence file for EACH Report Type
18 forqlift create --file="Report${val}.seq" "${groupFiles[@]}"
19 done
EDIT:
I tried replacing line 4 with:
while IFS= read -rd '' file; do
files+=( "$file" )
done < <(ssh user@host "cd /home/admin/data && printf '%s\0' Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv")
but when I am to generate hadoop sequence file (using forqlift, line 18), it fails.
ERROR forqlift.ui.Driver - java.io.FileNotFoundException: Rep_0001_20150102_0.csv (No such file or directory)
So rewriting this answer now that some of the ambiguities of the question has been cleared up.
You want to copy files, matching a pattern, from a remote host to localhost, then iterate them. Then use scp (or rsync), to download them, then iterate them locally afterwards.
# this copies the matching filenames from the remote host to the current dir. The quotes are important.
scp "user@host:/home/admin/data/Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv" ./
# Now that the files are accessible locally, you may iterate them with a for-loop
for file in Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv; do
printf 'Do something with %s\n' "$file"
done
Ok, now I change my answer to a working script:
declare -a ARRAY
count=0;
for F in $(ssh localhost find ./ -name "\*.sh" | xargs basename -a) ; do
ARRAY[$count]=$F
((count++))
done
ELEMENTS=${#ARRAY[@]}
for (( i=0;i<$ELEMENTS;i++)); do
echo ${ARRAY[${i}]}
done
The files you are searching for (in this script *.sh) will be saved into the bash array named ARRAY.
This one is tested, and it works.
user@user-virtual-machine:~$ ./filenames2array.sh
user@localhosts password:
deleteme1.sh
deleteme9.sh
deleteme2.sh
deleteme7.sh
deleteme8.sh
deleteme10.sh
deleteme4.sh
deleteme3.sh
deleteme5.sh
filenames2array.sh
deleteme6.sh
user@user-virtual-machine:~$
This one is written to connect to localhost via SSH. You can change the localhost to whatever remote server you want.