Initialize aliases during shell script execution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Initialize aliases during shell script execution
# 1  
Old 04-27-2016
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:
Code:
  
 alias abc = "sftp username1@Host1"
 alias xyz = "sftp username2@Host2"

In the script I am doing the below:
Code:
 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

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data, thanks
# 2  
Old 04-27-2016
I think this is the culprit:

Quote:
Originally Posted by Dippu
.alias file have entries like:
Code:
  
 alias abc = "sftp username1@Host1"
 alias xyz = "sftp username2@Host2"

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:

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

I hope this helps.

bakunin
# 3  
Old 04-27-2016
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?
# 4  
Old 04-27-2016
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Initialize file name bash shell - Linux

I am trying to initialize a file name in bash but not having much luck. For example, one of my bash scripts outputs a file named "FILE_1000G.vcf". I would like to rename FILE to match with the user's name. This is my code: set -e echo "Please enter your filename:" read filename rename... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh. more script1.sh echo "Starting Execution" ./script2.sh echo "End of Execution" ln_s(){ ] && return ln -s "$1" "$2" } My script1.sh executes another script2.sh which has the following entry more script2.sh... (12 Replies)
Discussion started by: mohtashims
12 Replies

3. Solaris

Script on Solaris spawning 2 processes for one shell script execution

Hi, I am having a shell script on Solaris 10 which has a while loop as shown below. #!/usr/bin/ksh # while do sleep 60 done Name of the shell script is coldcentric.sh. I executed script /DATAWAREHOUSE/LOAD/Scripts/coldcentric.sh from a command task in Informatica worklow as... (3 Replies)
Discussion started by: chekusi
3 Replies

4. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

5. Shell Programming and Scripting

Execution problem with shell script

Hi all, I want to use perl string manipulation commands in my shell script. I have written following script. echo "enter name" read name perl -e '$m=length($name); echo $m it gives an error: unrecognized token in perl command line. do not suggest me an equivalent command of shell... (3 Replies)
Discussion started by: admc123
3 Replies

6. Shell Programming and Scripting

execution of aliases from shell script

Hi I have to execute the commands in .aliases file from a shell script I tried 1.giving the alias directly in shell script 2.Giving the actually "value of alias" in the shell script 3. I tried giving both steps 1 and 2 inside ` quotes Still nothing is working . It says command... (3 Replies)
Discussion started by: ssuresh1999
3 Replies

7. Shell Programming and Scripting

Accessing aliases within a shell script

I am not able to access the aliases in my environment within a Python script. If I pass the alias to os.system(), I get a message saying "sh: x: not found". I've tried sourcing my .bashrc file in the script, but this does not work. I would perfer not to source my or any rc file because this... (9 Replies)
Discussion started by: cooldude
9 Replies

8. Shell Programming and Scripting

problem with shell script execution

Hi All, i am running a shell script in which there is a command `ps -ef | grep smon > db` When i execute this command in the command prompt i am getting the desired output..but when the script is executed..the db file is getting created but with no values...I could not find the reason for... (2 Replies)
Discussion started by: anju
2 Replies

9. UNIX for Dummies Questions & Answers

Sequential execution in shell script?

I've a shell script that invokes a URL of an application to do some work, e.g., http://www.abc.com/myservlet?action=helloworld.Does the shell wait for a return value from the URL call before proceeding to the next line of command? (6 Replies)
Discussion started by: chengwei
6 Replies

10. Shell Programming and Scripting

execution of shell script

How can I execute another shell script from one? Malay (5 Replies)
Discussion started by: malaymaru
5 Replies
Login or Register to Ask a Question