LinuxQuestions.org
Help answer threads with 0 replies.
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, 10:54 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 237

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, 11:59 PM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, PCLinuxOS
Posts: 590

Rep: Reputation: 73
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, 11:33 AM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 237

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, 01:07 PM   #4
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, PCLinuxOS
Posts: 590

Rep: Reputation: 73
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, 03:39 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:01 AM
calling from command line lilili Linux - Newbie 8 01-22-2006 06:09 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
calling another script to be run. sonesay Programming 2 05-23-2004 07:08 AM
Calling All Script Guru's Shinobi Linux - General 5 04-02-2003 04:24 PM


All times are GMT -5. The time now is 10:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration