Probably what you want is:
#!/bin/bash
#Note: using common directory saves me characters later!
usrDir=/usr2/c/subscription;
datFl=${usrDir}/userdata
#No need to read to variable just use subshell for loop input.
for u in $( cat ${usrDir}/users ); do
subFl=~${u}/.subscriptions
if [ -a $subFl ]; then
cat $subFl >> $datFl
else
echo "didn't run"
fi
done
Notice as others pointed out, test
is not the command you want to
check for the file. You could use test -e
, but [ -a $flName ]
is the preferred syntax for BASH
.
You probably don't want to use less
... an interactive command on
~{u}/.subscriptions... you probably want
cat`.
- It's unnecessary to
cd
to the ~${u}
directory.
Also if for some reason you expand the code and it makes sense to store the users
as an array variable your syntax is a bit off. In that case you would use:
#!/bin/bash
usrDir=/usr2/c/subscription;
datFl=${usrDir}/userdata
users=( $( cat ${usrDir}/users ) )
for u in ${users[@]}; do
...
done
The outer ()
corresponds to array assignment. i.e. to assign an array of string literals you can do
users=( jim bob jones )
The inner $()
which is also used in my solution above is a subshell.
See (Advanced Bash-Scripting Guide):
7.2. File test operators
Chapter 21. Subshells
Chapter 27. Arrays
How to properly use test
:
The classic test command