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
# 1  
Old 12-13-2016
Hammer & Screwdriver Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh.
Code:
more script1.sh
echo "Starting Execution"
./script2.sh
echo "End of Execution"
ln_s(){
  [[ -e $2 ]] && return
  ln -s "$1" "$2"
}

My script1.sh executes another script2.sh which has the following entry
Code:
more script2.sh
ln -s folder1 folder2

How can i automatically change all ln commands to ln_s so my custom function get called instead of the default ln command ?

I tried the adding the following inside the script1.sh but none of them work and it is picking ln command instead of ln_s

Code:
alias ln=ln_s
ln=ln_s
export ln=ln_s

Every time it calls ln instead of ln_s

Ref Thread: Ignore exit status for #!/bin/bash -e

Please suggest.
# 2  
Old 12-13-2016
A quick reference to man bash tells me
Quote:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt...
# 3  
Old 12-13-2016
Hammer & Screwdriver

Quote:
Originally Posted by RudiC
A quick reference to man bash tells me
I did put this line shopt -s expand_aliases; alias ln=ln_s in script1.sh but the output shows that that it is still picking ln instead of ln_s.
Output:
Code:
ln: cannot create folder2: File exists

Can you please explain why ?
# 4  
Old 12-13-2016
Works excellently for me:
without expand_aliases set:
Code:
./shscr: line 13: la: command not found


with expand_aliases set:
Code:
la .
+ ls -la .
total 176
drwxrw-r-- 6 user user 57344 Dez 13 14:35 .
.
.
.

Must be your setup / version?
# 5  
Old 12-13-2016
Hammer & Screwdriver

Quote:
Originally Posted by RudiC
Works excellently for me:
without expand_aliases set:
Code:
./shscr: line 13: la: command not found

with expand_aliases set:
Code:
la .
+ ls -la .
total 176
drwxrw-r-- 6 user user 57344 Dez 13 14:35 .
.
.
.

Must be your setup / version?
Please consider calling a second script i.e script2.sh from the first i.e script1.sh while having the command set in script2.sh while the alias set in script1.sh in your test.

Also find the details below.

Code:
bash-4.1$ bash --version
GNU bash, version 4.1.17(1)-release (sparc-sun-solaris2.11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
bash-4.1$ uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v


Last edited by mohtashims; 12-13-2016 at 09:51 AM..
# 6  
Old 12-13-2016
Sorry? You NEED to set options and aliases in the script they are used in.
# 7  
Old 12-13-2016
Quote:
Originally Posted by RudiC
Sorry? You NEED to set options and aliases in the script they are used in.
I have no write permissions on script2.sh ... i just have ownership of script1.sh.

Can you please suggest ?
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