Alias not working


 
Thread Tools Search this Thread
Operating Systems Solaris Alias not working
# 8  
Old 09-25-2014
You could go absolutely global if that is a requirement...
OSX 10.7.5, default bash terminal:-
Code:
#/bin/bash
# gsrc.sh
printf "/bin/echo" > /tmp/ECHO
printf "/usr/bin/printf" > /tmp/PRINTF
ECHO=`< /tmp/ECHO`
PRINTF=`< /tmp/PRINTF`
$ECHO -e "\t\tBazza...\n"
$PRINTF "\x1B[0m Walker...\n"

Results:-
Code:
Last login: Thu Sep 25 22:20:12 on ttys000
AMIGA:barrywalker~> chmod 755 gsrc.sh
AMIGA:barrywalker~> ./gsrc.sh
-e \t\tBazza...\n
x1B[0m Walker...
AMIGA:barrywalker~> printme=`< /tmp/ECHO`
AMIGA:barrywalker~> text="My name is Baz..."
AMIGA:barrywalker~> $printme "$text"
My name is Baz...
AMIGA:barrywalker~> _

HTH, good night all.
# 9  
Old 09-25-2014
Quote:
Originally Posted by nexional
The script I'm building is generic is supposed to be used on many platforms
Aliases in a script?

Aliases don't work in scripts, just interactive shell sessions. Try a variable.

Code:
# defaults to 'grep' unless GREP variable is set
${GREP-grep} -q ...

# 10  
Old 09-25-2014
Quote:
Originally Posted by Corona688
Aliases in a script?

Aliases don't work in scripts, just interactive shell sessions. Try a variable.

Code:
# defaults to 'grep' unless GREP variable is set
${GREP-grep} -q ...

Huh?

If I have a script (tester) containing:
Code:
#!/bin/ksh
alias myecho=echo
myecho $0

and make it executable using:
Code:
chmod +x tester

and execute it using:
Code:
./tester

it prints:
Code:
./tester

Aliases should work just fine in a shell script as long as the aliases being used are defined in that shell script.

The problem the OP was seeing is that aliases aren't (and, for obvious security reasons, should not be) exported.
# 11  
Old 09-25-2014
Quote:
Originally Posted by wisecracker
You could go absolutely global if that is a requirement...
OSX 10.7.5, default bash terminal:-
Code:
#/bin/bash
# gsrc.sh
printf "/bin/echo" > /tmp/ECHO
printf "/usr/bin/printf" > /tmp/PRINTF
ECHO=`< /tmp/ECHO`
PRINTF=`< /tmp/PRINTF`
$ECHO -e "\t\tBazza...\n"
$PRINTF "\x1B[0m Walker...\n"

Results:-
Code:
Last login: Thu Sep 25 22:20:12 on ttys000
AMIGA:barrywalker~> chmod 755 gsrc.sh
AMIGA:barrywalker~> ./gsrc.sh
-e \t\tBazza...\n
x1B[0m Walker...
AMIGA:barrywalker~> printme=`< /tmp/ECHO`
AMIGA:barrywalker~> text="My name is Baz..."
AMIGA:barrywalker~> $printme "$text"
My name is Baz...
AMIGA:barrywalker~> _

HTH, good night all.
I STRONGLY suggest that you NEVER do this!

If this convention were used by more than one script or if this script is run twice, they could wipe each other out if they ran at close to the same time. (There is a minuscule time period between when the shell wipes out the data in a file due to the redirection and the time when the printf command fills it with the name of the desired executable.) On a single processor system, this might not happen often, but on a multi-processor system, it will eventually bite you right where it hurts the most. And if there are any miscreants on your system that notice these files in /tmp, it is easy for them to replace the utilities you thought you had specified with other utilities of their choosing.
# 12  
Old 09-25-2014
Quote:
Originally Posted by Don Cragun
And if there are any miscreants on your system that notice these files in /tmp, it is easy for them to replace the utilities you thought you had specified with other utilities of their choosing.
I agree the race condition risk however, it isn't easy or even possible for regular users to replace the utilities already created in /tmp. What they might do is anticipate and create hostile alternatives before the gsrc.sh script is ever executed.
# 13  
Old 09-25-2014
Quote:
Originally Posted by jlliagre
I agree the race condition risk however, it isn't easy or even possible for regular users to replace the utilities already created in /tmp. What they might do is anticipate and create hostile alternatives before the gsrc.sh script is ever executed.
That depends on what system you're using. Some systems create a per user /tmp directory (although that is not allowed by the standards); others have a single /tmp directory shared by everyone on the system. Some system administrators set up /tmp such that anyone can remove any file in that directory and create a new file with that name anytime they want; others set the permissions on that directory so that only the owner of the file or someone running "with appropriate privileges" can remove a file. The umask setting at the time the file was created will determine who can read and write that file (if they don't have permission to just remove and replace it).

On many systems, if I create /tmp/ECHO as user dwc and set the mode to 0600 and you try to run wisecracker's script that ignores any error when using printf to set the contents of that file, then the later attempt to execute
Code:
ECHO=`< /tmp/ECHO`

will fail with a permission denied error trying to read the file I created.

Utilities belong in "safe" places like /bin and $HOME/bin; not in places like /tmp.
# 14  
Old 09-26-2014
I would go with linkage in $HOME/bin or /opt/myapp/bin.

Also, check out your code, perhaps there is a way to make in run on every os using standard toolset, therefor avoiding linking, aliases and case / if clauses per OS.

If you can paste the code, folks here might be able to clear it up a bit so it works with standard tools.

For instance :
Code:
echo "hi" | grep -q "hi" && echo "YES"

Can be replaced with something like :
Code:
echo "hi" | grep "hi" > /dev/null && echo "YES"

Regards
Peasant.
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