LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-05-2008, 09:54 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
Calling alias command in script


I created an alias that is called "temp" and I am trying to call that in a script I wrote. I don't think it is in the path though. Does anyone know how I would go about either putting it in the path or calling it directly from wherever it is?
 
Old 04-05-2008, 10:59 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Aliases aren't in the path. They're in the environment.

Run the command alias with no arguments to get a list of currently defined aliases. Or run the command type temp to verify that temp really is aliased.

Are you calling the script from the command line? From cron? Via SSH? What error message do you get?
 
Old 04-06-2008, 10:33 AM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
The type temp command gives me this output
Code:
temp is aliased to `cat /home/jason/Desktop/scripts/test'
I'm trying to call it in a script with this line
Code:
var2=$(temp | awk '{print $4}')
And I get this error
Code:
line 6: temp: command not found
 
Old 04-06-2008, 12:07 PM   #4
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
That's curious. I just tried a very similar thing here and it worked.

Are you executing the var2 assignment from the command line? Or is it in a script? If in a script, are you calling it from the command line?

What's even more curious is the reference to line 6 when 'temp' has but one line. It makes me wonder if there is some other 'temp' script somewhere that's getting called instead.
 
Old 04-06-2008, 02:39 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It seems to me that aliases cannot be inherited from the environment into shell scripts. However you can use aliases in a shell script if the alias definition is in your $HOME/.bashrc file and if you make your shell act as a login shell (in this case the .bashrc file is sourced). For example
Code:
#!/bin/bash -l
shopt -s expand_aliases
var2=$(temp | awk '{print $4}')
echo $var2
In the first line you have to add the -l option to bash. Furthermore the second line uses the shopt command to enable the expand_aliases option, otherwise aliases are not interpreted. Anyway there are a lot of reasons to not use aliases inside scripts and use functions instead. See Chapter 24 of the Advanced Bash Scripting Guide for a full treatment.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 08:01 AM
calling from command line lilili Linux - Newbie 8 01-22-2006 05:09 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
calling another script to be run. sonesay Programming 2 05-23-2004 06:08 AM
Calling All Script Guru's Shinobi Linux - General 5 04-02-2003 03:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration