Aliases NOT working inside bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Aliases NOT working inside bash shell script
# 8  
Old 12-13-2016
Can you source script2 from within script1?
# 9  
Old 12-13-2016
Hammer & Screwdriver

Quote:
Originally Posted by RudiC
Can you source script2 from within script1?
Yes, i did source in script1.sh as shown below.

Code:
more script1.sh
shopt -s expand_aliases; 
alias ln=ln_s 
echo "Starting Execution"
source  ~/script2.sh
~/script2.sh 
echo "End of Execution" 
ln_s()
{   
[[ -e $2 ]] && return   
ln -s "$1" "$2" 
}

But i get this error:

Code:
/opt/user1/script2.sh: line 1: ln_s: command not found

Why the error ?
# 10  
Old 12-13-2016
What is this:
Code:
source  ~/script2.sh
~/script2.sh

?
And, where do you define the function, and where do you call it?
# 11  
Old 12-13-2016
Quote:
Originally Posted by RudiC
What is this:
Code:
source  ~/script2.sh
~/script2.sh

?
And, where do you define the function, and where do you call it?
In Post# 9 i have shown that the function ln_s() is define in script1.sh.

I am sourcing script2.sh inside script1.sh here ->
source ~/script2.sh and then i am executing script2.sh from within script1.sh here -> ~/script2.sh with the expectation that the ln command in script2.sh should be executed as ln_s thus calling the function.

Last edited by mohtashims; 12-13-2016 at 11:05 AM..
# 12  
Old 12-13-2016
Quote:
Originally Posted by mohtashims
In Post# 9 i have shown that the function ln_s() is define in script1.sh.
Yes, but unfortunately too late in the script.


Quote:
I am sourcing script2.sh inside script1.sh here -> source ~/script2.sh
OK, very good, the alias should be known, but not the function

Quote:
and then i am executing script2.sh from within script1.sh here -> ~/script2.sh
Why this second execution? And, neither alias nor function will be available

Quote:
with the expectation that the ln command in script2.sh should be executed as ln_s thus calling the function.
No, as explained.


Let me comment on your script snippet:

Code:
shopt -s expand_aliases;        # setting the necessary shell option
alias ln=ln_s                   # defining the alias IN THE CURRENT ENVIRONMENT in which the function IS NOT KNOWN YET
echo "Starting Execution" 
source  ~/script2.sh            # sourcing (=reading & executing) the script IN THE CURRENT ENVIRONMENT; alias known, function not known
~/script2.sh                    # running the script (again!) in a NEW subshell IN A NEW ENVIRONMENT; alias not known, function not known 
echo "End of Execution" 
ln_s()                          # defining the function for LATER USE!
{   
[[ -e $2 ]] && return   
ln -s "$1" "$2" 
}
.                               # from here on, ln will correctly call the function
.
.

I strongly recommend you get your head around shells and subshells, the difference between sourcing a collection of commands and executing them in a subshell, environment inheritance, and function definition.
This User Gave Thanks to RudiC For This Post:
# 13  
Old 12-14-2016
Quote:
Originally Posted by mohtashims
I have no write permissions on script2.sh ... i just have ownership of script1.sh.

Can you please suggest ?
Instead of an alias, you could maybe use an exported function:

Code:
ln() {   ln_s "$@" }
export -f ln

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nslookup on a variable inside script not working

I am new at scripting. I have a file that each line is the shortname for a server. ie - Server1 Server2 Server3 I want to read in that file, do a nslookup and print the FQDN to a file. I added an echo statement to make sure my variable was being read and changing. But the nslookup... (5 Replies)
Discussion started by: kellyhkc
5 Replies

2. Shell Programming and Scripting

Need Multiple checks inside if condition in a bash shell script

Hi, I need to perform the untar and rm operation if the file found is a .tar and does not have test.tar or hello.tar as the file names. Below is the loop to check the same. for tf in *.tar do if ] then found=1 ... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

Hi, maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt? #! /bin/bash set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin date >>... (3 Replies)
Discussion started by: beislhur
3 Replies

4. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: Dippu
3 Replies

5. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

6. Shell Programming and Scripting

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows: #!/bin/bash HOST=$1 /usr/bin/expect -c " spawn ssh -i /root/.ssh/id_rsa root@$HOST expect -exact "Enter... (3 Replies)
Discussion started by: John Wilson
3 Replies

7. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

8. Shell Programming and Scripting

[SOLVED] how to use my aliases in a bash script

Hi, I confess to be stuck ... as explained here Aliases - Bash Reference Manual I added shopt -s expand_aliasesto my script ... but it didn't work. Later I've read I had to add this at the begning of my my script : . ~/.bash_aliases(of course my aliases are in this file) but again, the... (0 Replies)
Discussion started by: xib.be
0 Replies

9. 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

10. 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
Login or Register to Ask a Question