Alias not working


 
Thread Tools Search this Thread
Operating Systems Solaris Alias not working
# 1  
Old 09-25-2014
Alias not working

Hi,

I'm on Solaris (SunOS wsp2cm01 5.10 Generic_150400-10 sun4v sparc SUNW,Sun-Fire-T200). Trying to set up an alias like below -
Code:
alias grep="/usr/xpg4/bin/grep"

but when I call grep in my command it ignores my alias & uses /usr/bin/grep instead. The problem with /usr/bin/grep is that it doesn't understand -q flag & that's how I know my command is still using /usr/bin/grep.
e.g. -
Code:
echo "hi" | grep -q "hi" && echo "YES"
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . .

I tried "type grep" which returns me -
Code:
$ type grep
grep is an alias for /usr/xpg4/bin/grep

so I know I'm good there but not sure why it's still ignoring the alias while executing the command.

Any help appreciated.
# 2  
Old 09-25-2014
Why use alias?
Why not point to the absolute path in the first place?
Code:
echo "hi" | /usr/xpg4/bin/grep -q "hi" && echo "YES"

OSX 10.7.5, default bash terminal.
Or perhaps, if you are using bash, use variables:-
Code:
Last login: Thu Sep 25 19:27:25 on ttys000
AMIGA:barrywalker~> x="/bin/echo"
AMIGA:barrywalker~> y="hexdump -C"
AMIGA:barrywalker~> $x "Bazza..." | $y 
00000000  42 61 7a 7a 61 2e 2e 2e  0a                       |Bazza....|
00000009
AMIGA:barrywalker~> _


Last edited by wisecracker; 09-25-2014 at 03:36 PM.. Reason: Added variable method...
# 3  
Old 09-25-2014
The script I'm building is generic is supposed to be used on many platforms e.g. HP, AIX, SOLARIS. For the first two I still need to refer /usr/bin/grep but for SOLARIS I'm setting up aliases like below.
Code:
if [ "$(uname)" = "SunOS" ]; then
    [ -x "/usr/xpg4/bin/grep" ] && alias grep="/usr/xpg4/bin/grep"
    [ -x "/usr/xpg4/bin/egrep" ] && alias egrep="/usr/xpg4/bin/egrep"
    [ -x "/usr/bin/nawk" ] && alias awk="/usr/bin/nawk"
    [ -x "/usr/ucb/ps" ] && alias ps="/usr/ucb/ps"
fi

wisecracker, I could also define a variable say GREP to point to corresponding binary e.g. /usr/bin/grep or /usr/xpg4/bin/grep (depending upon the system) but then I'll have to use ${GREP} everywhere in my code. I know that's my last option if nothing works, but I was wondering if there's something very trivial I'm missing to be able to use alias instead.

Last edited by nexional; 09-25-2014 at 03:38 PM..
# 4  
Old 09-25-2014
Just an idea, again assuming bash...
How about 'source'(ing) your script:-
Code:
. /path/to/script.sh

OR
Code:
source /path/to/script.sh

---------- Post updated at 08:01 PM ---------- Previous update was at 07:46 PM ----------

OSX 10.7.5, default bash terminal:-
Code:
#!/bin/bash
# src.sh
alias echo="/bin/echo"
alias printf="/usr/bin/printf"
alias
echo -e "\x1B[1m Bazza..."
printf "\x1B[0m Walker...\n"

Results:-
Code:
Last login: Thu Sep 25 19:56:47 on ttys000
AMIGA:barrywalker~> chmod 755 src.sh
AMIGA:barrywalker~> ./src.sh
alias echo='/bin/echo'
alias printf='/usr/bin/printf'
 Bazza...
 Walker...
AMIGA:barrywalker~> # WRONG!!!
AMIGA:barrywalker~> . ./src.sh
alias echo='/bin/echo'
alias printf='/usr/bin/printf'
-e \x1B[1m Bazza...
x1B[0m Walker...
AMIGA:barrywalker~> # CORRECT!!!

# 5  
Old 09-25-2014
Aliases set in a shell affect the current shell and any subshell execution environments started by that shell; they are not exported to other shell execution environments (such as when you invoke another shell script).

Most shells have several startup files that are invoked under various conditions (such as when you login, when you start a new interactive shell, when you start a new non-interactive shell, etc. but these vary from shell to shell). If you place your alias in the appropriate initialization file for your shell, you may be able to set up aliases that affect all scripts you start that use that shell.

Another possibility is to add symbolic links in your $HOME/bin directory (assuming that $HOME/bin appears in your PATH environment variable before /bin and /usr/bin. And, if you would just put /usr/xpg4/bin in your PATH before /bin and /usr/bin that would get rid of the need for awk, egrep, grep, (and fgrep) aliases. Are there other utilities in /usr/xpg4/bin that would cause you problems if you used them instead of the utilities of the same name in /usr/bin?

But, if you are writing shell scripts that other users will be invoking, that shell script needs to set up its own environment and not depend on the user calling it to set up a specific (possibly different) environment for each command they execute.
# 6  
Old 09-25-2014
Yes Don, there are other utilities in there (/usr/xpg4/bin) which are causing problem so I can't just set the PATH to prioritize this path.

Also, I don't have permissions to change any of the startup files e.g. .profile, .kshrc etc. Whatever required changes are there have to be done in this script.

Also, wisecracker, sorry can't source the script. I tried it didn't seem to work for me.
# 7  
Old 09-25-2014
Use envvals instead of aliases:

Code:
if [ "$(uname)" = "SunOS" ]; then
     [ -x "/usr/xpg4/bin/grep" ] && GREP="/usr/xpg4/bin/grep"
     [ -x "/usr/xpg4/bin/egrep" ] && EGREP="/usr/xpg4/bin/egrep"
     [ -x "/usr/bin/nawk" ] && NAWK="/usr/bin/nawk"
     [ -x "/usr/ucb/ps" ] && PS="/usr/ucb/ps" 
fi

${GREP} [grep args]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Lost with this alias why it is not working

Hi, I have the following aliases: $: alias | grep "^du=" du='du -s * 2>/dev/null | awk '\''{ printf "%4.2f-KB ==> %s \n", $1/1024 , $2 }'\'' | sort -rn' $: alias | grep "^dutop10=" dutop10='du -s * 2>/dev/null | awk '\''{ printf "%4.2f-KB ==> %s \n", $1/1024 , $2 }'\'' | sort -rn | head... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Alias not working

I have several shell scripts which contain the nawk command. Here is what i m doing assign the correct value to nawk as nawk is not found on a new systems. Here is what i did. more test.sh ] && alias nawk=/usr/bin/gawk ] && alias nawk=/usr/bin/nawk ] && alias nawk=/usr/bin/awk... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Solaris

Alias is not working under Bourne Shell

Hi, Please assist me why HC alias is not working under this shell? root@singapore # grep HC /.profile alias HC='cd /var/tmp/HC/2015/`date +%B`' root@singapore # . /.profile Sourcing //.profile-EIS..... root@singapore # echo $HC root@singapore # HC HC: not found root@singapore # echo... (18 Replies)
Discussion started by: tprabhu1983
18 Replies

4. Red Hat

[SOLVED] How the alias is working?

Iam facing some strange issue with alias. I have an alias file in which i have created lot of aliases as given below. export BUILD_HOME=/apps/psr/build export DB_HOME=/apps/psr/database export LOGS_HOME=/apps/psr/logs export BUILD_TEST=/apps/psr/build_dev/build_test export... (0 Replies)
Discussion started by: Vikram_Tanwar12
0 Replies

5. UNIX for Dummies Questions & Answers

cp command not working with alias

Hi Friends, I have added some aliases in .bash_profile file under the root folder. Its works fine and very useful, but does not go well with cp command. alias deploy="cd /usr/local/tomcat/webapp" alias artifacts="cd /usr/local/artifacts" but when i try to cp from artifacts folder... (2 Replies)
Discussion started by: prashdeep
2 Replies

6. Solaris

Simple question: alias not working for root

OS = Solaris 8 Issue: alias not working for root, but working for regular users # grep root /etc/passwd root:x:0:1:Super-User:/:/sbin/sh # alias dir=ls # dir dir: not found # alias dir="ls -l" # dir dir: not found # alias dir='ls -l' # dir dir: not found # alias... (2 Replies)
Discussion started by: aixlover
2 Replies

7. Solaris

Alias not working

Hello, I am trying to set an alias in my .kshrc or .profile and when I do it is not setting. If I do it manually it works fine. Is there another file I should put this in? Here is the alias I am using. alias ll='ls -ltr' I am using solaris 9. When I type alias it does not show these... (5 Replies)
Discussion started by: dkranes
5 Replies

8. UNIX for Dummies Questions & Answers

alias command not working after re-login

i create some alias in .cshrc file then i run source .cshrc to refresh it, the alias command work fine but when i relogin again, the alias command is not working... i need to re-run the source .cshrc command so that the alias only workable. any idea on it? (7 Replies)
Discussion started by: lsy
7 Replies

9. UNIX for Dummies Questions & Answers

alias not working in scripts

Hi All, PF below details, > cat run.sh #!/usr/bin/ksh alias ll="ls -l" > ./run.sh > ll ksh: ll: not found. Pls help on this? Thanks in Advance, Naga :cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

10. UNIX for Advanced & Expert Users

host alias not working: host not found

Hello, I am working on HP-UX , and in the /etc/hosts file we have setup an alias: aa.bb.cc.dd devmach2.unix.org devmach2 devma2v The alias devma2v does not work. Error when pinging devma2v ping: unknown host devma2v For devmach2 the ping works fine , returning the correct IP... (4 Replies)
Discussion started by: FunnyCats
4 Replies
Login or Register to Ask a Question