LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   using 'watch' on an alias in .bashrc (https://www.linuxquestions.org/questions/linux-general-1/using-watch-on-an-alias-in-bashrc-272480/)

slackist 01-01-2005 12:30 AM

using 'watch' on an alias in .bashrc
 
Hi,

I have a line in ~.bashrc thus:

alias temp='cat /proc/acpi/thermal_zone/THM0/temperature'

which works fine for giving me the cpu temp. but if I try 'watch temp' (without quotes) it spits up error messages.

The watch man page is not actually very informative but does mention something about using sh -c, and the need for quotes with some examples. I found sh in /bin and it is a link to bash.

I have tried various different quotes ie watch 'temp' watch "'temp'" etc but nothing has worked.

It is not that big of a deal because watch cat/proc/acp..........etc works fine, but it would save some typing if I knew how to get watch temp to work.

Thanks for any ideas,

mark

The error message is:
Code:

mark@~/$ watch temp
sh: line 1: temp: command not found


320mb 01-01-2005 01:24 AM

try connecting the 2 words with an underscore "_"

IE..........
Code:

watch_temp

slackist 01-01-2005 04:02 AM

Thanks 320mb but it didn't work, gave me a command not found error.


Anybody have any other suggestions?

mark

bigrigdriver 01-01-2005 09:58 AM

The only suggestion I can make is tis:
1) You have not rebooted since you made the change to ~/.bashrc.
2) You have not logged out/in since making the change.
3) You did not run 'source ~/.bashrc' after making the change.
4) You're still working in the same shell instance in which you made the change to .bashrc, and bash doesn't know about it yet.
Do any one of the first three steps to reload .bashrc (step three is the easiest to apply), then try again.

slackist 01-01-2005 11:09 PM

Thanks Bigrigdriver, but that is not it.

Typing temp in a terminal works fine, the alias in ~/.bashrc produces a figure for the cpu temperature.

The problem is that I cannot watch the command.

I am moving room fans around and I want to see which position causes the temperature to drop the fastest, hence having the temp alias run every two seconds will be very useful.

Thanks for trying though, anybody have any more ideas?

mark

bigrigdriver 01-02-2005 11:59 AM

OK, try this then. Instead of trying to use an alias in .bashrc, make a small bash script to run the command. Something like:
#!/bin/bash
watch 'cat /proc/acpi/thermal_zone/THM0/temperature'

Give the script a name (watchtemp.sh, or whatever you prefer) and make it executable. Then run watch from the script. If one of your objectives is to save some typing, that'll do it.

Tim Retout 01-02-2005 04:35 PM

Yes, because the word 'temp' is passed to 'sh -c', aliases will not work. Although 'sh' is in fact bash, bash tries to be compatible with the old 'sh' command. Writing a script is your best bet.

[EDIT: I'm wrong. See below.]

trickykid 01-02-2005 05:01 PM

Quote:

Originally posted by Tim Retout
Yes, because the word 'temp' is passed to 'sh -c', aliases will not work. Although 'sh' is in fact bash, bash tries to be compatible with the old 'sh' command. Writing a script is your best bet.
Yes, that is correct. You can't make a command understand an alias. Writing a script with the path to the /proc file and such should work just fine.

And I have no earthly ides why anyone would tell you to put an underscore between a command and a variable/value, etc? Of course its going to give you a command not found! :rolleyes:

Tim Retout 01-02-2005 06:13 PM

Well... what you can do is put
Code:

alias watch = 'watch '
alias temp='cat /proc/some/file'

into .bashrc and then run 'watch temp'. But it's a bit fiddly. Only useful if there's more than one thing you want to watch, then you can throw in five or six aliases and run 'watch thing1', 'watch thing2', etc...

Sorry if this seems to totally contradict my last post. :) Actually, I've tried it now, and sh (that is, bash acting as sh) can in fact use aliases, if you tell it to. Well, you learn something new every day. Of course, I'd still recommend writing a script.

slackist 01-03-2005 12:52 AM

Thanks everybody,

Bigrigdriver, that little script is just what I needed.

Cheers,

mark

Nicolas Pineau 01-29-2016 09:37 AM

Hello,

Question is a bit old now, but in case someone's still looking for another solution -> might be fixed by adding watch in another alias :

Code:

alias tempwatch="watch 'cat /proc/acpi/thermal_zone/THM0/temperature'"

Habitual 01-29-2016 01:09 PM

Welcome to LQ, Nicolas!

11 year old necro post! :)


All times are GMT -5. The time now is 02:13 PM.