naree
May 22, 2008, 2:07am
1
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
agn
May 22, 2008, 2:25am
2
As which user are you trying to run the script ? You need to be the same user to use the alias.
nua7
May 22, 2008, 2:28am
3
It would also be a good idea to check your PATH variable. Hope the path variable contains your utils dir path.
naree
May 22, 2008, 2:30am
4
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
agn
May 22, 2008, 2:31am
5
Is utils listed when you type 'alias' in your shell ?
nua7
May 22, 2008, 2:38am
6
You ned to have the dir path of the utils script , in your PATH variable. Did you check that?
naree
May 22, 2008, 2:39am
7
Dear agn/all,
Yes, When type alias utils is being displayed in that list.
Thanks & Regards
Naree
agn
May 22, 2008, 2:48am
8
Please paste your alias here and if possible your script which uses the alias.
naree
May 22, 2008, 3:06am
9
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
elthox
May 22, 2008, 3:13am
10
Why don`t u use vmstat for memory statistics?
Top is a dynamic page...this script doesnt work I guess
elthox
May 22, 2008, 3:21am
11
Or another alternative with free command
percentage=`free|grep "Mem:"|awk '{print $3/$2*100}'`
naree
May 22, 2008, 3:33am
12
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
elthox
May 22, 2008, 3:42am
13
naree:
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.
agn
May 22, 2008, 3:46am
14
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.
naree
May 23, 2008, 1:14am
17
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
naree
June 3, 2008, 4:14am
18
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