Initialize aliases during shell script execution

Hello,

I am trying to run a shell script that tests the connectivity to all the sftp servers and email if any one of them is down. There are aliases for all the servers with sftp command prefix in a '.alias' file. But when I use the alias name to run it as a command in my shell script to test the connectivity, I get an error saying "not found [No such file or directory]", though I initialize the .alias file at the beginning of my script.
Below is what I m doing. Please help me to resolve this.

.alias file have entries like:

  
 alias abc = "sftp username1@Host1"
 alias xyz = "sftp username2@Host2"
  

In the script I am doing the below:

 source .alias
 while read i
do
 abc << EOF
 exit
 EOF
 done < Alias_List.txt

#Where Alias_List.txt has the list of all sftp server alias names, i.e abc, xyz

My question is: why is it "source .alias" NOT setting/initializing the alias names in the session? is adding these alias names to /etc/bashrc file the only solution? Can't I handle it within the script?

Any help is much appreciated. Thanks in advance

Regards,
Dippu

I think this is the culprit:

If your alias-file holds this content as stated, then it won't work because the alias-definition doesn't work. Remove the the superfluous blanks and - maybe not necessary, but i like to play it safer than necessary - quote in the strictest form.

Furthermore I'd include the path to the sftp-executable into the alias:

alias abc='/path/to/sftp username1@Host1'
alias xyz='/path/to/sftp username2@Host2'

I hope this helps.

bakunin

Thanks for your quick response Bakunin.
Sorry the alias command I gave have extra spaces but in original file, they aren't there. So yes the alias itself works fine, as I sourced it in my local session and ran the alias as a command and it works fine as it logs on to the sftp server.
But the problem is only when I run it in a script.
Also I just tried with absolute path of the sftp executable in the alias definition and even that doesn't work.
I wonder how can I make aliases visible in my shell script! OR the way I am executing the alias command, by just calling the alias: abc in my script not the correct way?

There seems to be a lot of disagreement on this, but as I understand it, aliases are for interactive use -- not generally for scripted use. For scripts, you'd use functions. It's possible to enable aliases in noninteractive scripts in some shells, but if you feel the need to do so, there may be design problems underlying your work.

Your "here document" in particular has me worried that you are expecting alias substitutions to happen in a place they never will, however -- inside things like here-documents, dynamically generated code, etc. It'd take an eval to shoehorn them in there, which is a class-9 bad idea for a variety of reasons. Could you tell me exactly how you are trying to use these aliases?