please help me regarding alias

Dear All,
I have given alias to run a script which will calculate memory and cpu utilization as "utils" in my bash shell. Even i have given this entry in .profile and .bashrc in my home directory. It is working fine.
Now my question is that I want to use this alias in another script. i have just given "utils" in that script is say that

utils as not found

Please help me how make alias work in scripts

Thanks & Regards
Naree

As which user are you trying to run the script ? You need to be the same user to use the alias.

It would also be a good idea to check your PATH variable. Hope the path variable contains your utils dir path.

Dear agn/all
I have logged in as same user. And trying to run alias within a script. But it is not working.
Please help me regarding this.

Thanks & Regards
Naree

Is utils listed when you type 'alias' in your shell ?

You ned to have the dir path of the utils script , in your PATH variable. Did you check that?

Dear agn/all,
Yes, When type alias utils is being displayed in that list.

Thanks & Regards
Naree

Please paste your alias here and if possible your script which uses the alias.

Dear agn/all,

naresh@dev1#alias

alias cls='clear'
alias dir='ls -a'
alias utils='sh ~/scripts/utils.sh'

utils.sh

#!/bin/bash

TOP="/opt/sfw/bin/top"

$TOP | grep -i "Memory" | awk '{print $2" " $4}'| sed -e 's/M//g'| awk '{percent=(($1-$2)/$1)*100} { print "Memory Utilization="percent"%"}'

sar -u 1 | tail -1 | awk '{print "CPU Utilization="100-$5"%"}'

The Script in which i m using alias is 123.sh

utils
rm /data/naresh/ican.txt
rm /data/naresh/scripts/tomem
----------------------------------------------------------

# sh 123.sh

utils not found

Thanks & Regards
Naree

Why don`t u use vmstat for memory statistics?

Top is a dynamic page...this script doesnt work I guess

Or another alternative with free command

percentage=`free|grep "Mem:"|awk '{print $3/$2*100}'`

Dear All,

      Thanks for u r replies. But please understand my problem. I want to use the alias name "utils" in a script called 123.sh

But that alias is not working. Please tell me how use the alias name in a script.

Thanks & Regards
Naree

NAree,

How you pretend for the alias to work when utils.sh include a line in the script which doesnt work? Test the utils with simpler examples. Remove TOP and SAR and test it with a simple command.

You will either have to source your .bashrc or .profile in your script or use 'sh -l' to run your script. '-l' is for login shell so that it reads your .bashrc.

$ cat 123.sh
#!/bin/sh -l

. .bashrc
utils
...

OR

$ cat 123.sh
#!/bin/sh -l
utils
..

OR

$ sh -l 123.sh

I do know the soln works but have some suggestions.

  • Try giving absolute path instead ~.
  • You could use workaround by sourcing the the file instead alias.

I do know the soln works but have some suggestions.

  • Try giving absolute path instead ~.
  • You could use workaround by sourcing the the file instead alias.

Dear All,
I have given the alias name with absolute path instead of ~. But even though it is not working. And when i try to run the script with

sh -l 123.sh

It is throwing Error called

-l bad options

Please help reagarding this.

Thanks & Regards
Naree

Hi All,

     I want to use a alias name called utils .. in script.  When i try to run that script is throwing error. utils not found.  Can anyone please suggest me  how to use alias name called "utils" in a script.

Even i have tired using sh -l script1.sh is throwing Error

-l bad option

Please help me Regarding this

Thanks & Regards
Naree