Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-07-2003, 06:36 PM
|
#1
|
Senior Member
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131
Rep:
|
bash env vars alias's & .bashrc
Hello all
I'm just trying to do a simple task. I'm making an alias to take a screenshot or screen grab. I don't know why it does not work; In my .bashrc file I have:
if [ -x /usr/bin/import ] ; then
export MYTIME=`/bin/date +%r | /usr/bin/awk '{print $1}'`;
alias grab="/usr/bin/import -window root screenShot-$MYTIME.jpg";
fi
OK, so the test works, I have import (image magick) - line 1
But $MYTIME gets the value of the time of the instance of the shell being created and not the time at whch I execute the alias! So I get:
micxz@mars:~> echo $MYTIME
04:29:08
micxz@mars:~> echo $MYTIME
04:29:08
At any time!
So my screen shot file gets overwritten each time because the time var does not change. I get, "screenShot-04:29:08.jpg" every time;
How would I get the value of $MYTIME to be current when I execute "grab"? Or is there a better way? And how do ", ', and ` differ in shell interpretation? Thanks in advance;
Last edited by micxz; 10-07-2003 at 06:38 PM.
|
|
|
10-07-2003, 07:13 PM
|
#2
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
Other implementation with different quotation
Code:
function mytime { /bin/date +%r | /usr/bin/awk '{print $1}'; }
alias grab='/usr/bin/import -window root screenShot-`mytime`.jpg'
|
|
|
10-07-2003, 07:26 PM
|
#3
|
Senior Member
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131
Original Poster
Rep:
|
if [ -x /usr/bin/import ] ; then
function mytime { /bin/date +%r | /usr/bin/awk '{print $1}' }
alias grab="/usr/bin/import -window root screenShot-'mytime'.jpg";
fi
I get Now:
bash: /home/micxz/.bashrc: line 53: syntax error near unexpected token `fi'
bash: /home/micxz/.bashrc: line 53: `fi'
|
|
|
10-07-2003, 07:40 PM
|
#4
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
You have wrong type of parenthesis. - If I checked correctly, there have to be a semicolon(;) at he end of the function definition.
- Use single quotes for the alias so that it is not being evaluated until at the time it is ran.
- The quotes around the mytime word are not ordinary quotes, but a pair of backquotes (not ' but `).
They mean that instead of placing text "mytime", the shell executes mytime command (the fuction defined above) and backsubstitutes the output of the command back to the commandline.
Other way to write it is
Code:
alias grab='/usr/bin/import -window root screenShot-$(mytime).jpg'
|
|
|
10-07-2003, 07:59 PM
|
#5
|
Senior Member
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131
Original Poster
Rep:
|
Syill get:
bash: /home/micxz/.bashrc: line 52: syntax error near unexpected token `fi'
bash: /home/micxz/.bashrc: line 52: `fi'
With $() style, Backwuotes are checked. I have:
if [ -x /usr/bin/import ] ; then
function mytime { /bin/date +%r | /usr/bin/awk '{print $1}' };
alias grab="/usr/bin/import -window root screenShot-$(mytime).jpg";
fi
|
|
|
10-07-2003, 08:02 PM
|
#6
|
Senior Member
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131
Original Poster
Rep:
|
You know what forget this bashrc stuff. I'm just going to write a small bash script that does this and put it in my bin dir. Thanks for your help anyhow;
|
|
|
10-07-2003, 08:36 PM
|
#7
|
Member
Registered: Aug 2003
Location: Approximately here.
Distribution: Mandrake 9.1
Posts: 86
Rep:
|
I doubt you can define a function inside of an if statement. What you can definitely do is create a function, and then subjectively call it from inside the if statement, like so:
function blah()
{
...
}
if (something)
{
blah();
}
Ignore the formatting, that's C type formatting, but you get the idea I hope.
Edit: those brackets for your if condition look suspicious, try round brackets, also I highly doubt that semicolon should be there after the condition and before the then.
Last edited by Mad Merlin; 10-07-2003 at 08:39 PM.
|
|
|
10-08-2003, 06:20 AM
|
#8
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
Quote:
Originally posted by micxz
Syill get:
bash: /home/micxz/.bashrc: line 52: syntax error near unexpected token `fi'
bash: /home/micxz/.bashrc: line 52: `fi'
With $() style, Backwuotes are checked. I have:
if [ -x /usr/bin/import ] ; then
function mytime { /bin/date +%r | /usr/bin/awk '{print $1}' };
alias grab="/usr/bin/import -window root screenShot-$(mytime).jpg";
fi
|
No no and no!
Just read what i said -- also the two first points -- or copypaste this:
Code:
if [ -x /usr/bin/import ] ; then
function mytime { /bin/date +%r | /usr/bin/awk '{print $1}'; }
alias grab='/usr/bin/import -window root screenShot-$(mytime).jpg';
fi
(Well, to be honest, my mentioning about the semicolon at the end of the function definition is ambiguous. But sample code was correctly in my first post.)
Sorry for getting nerwous, but I do am repeating myself. 
Last edited by ToniT; 10-08-2003 at 09:13 AM.
|
|
|
10-08-2003, 02:09 PM
|
#9
|
Senior Member
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131
Original Poster
Rep:
|
Thanks ToniT
Your right I did not read it closly enough' I see now the ";"'s are not needed. I decided to build a small useless program for practice that does a bit more. I posted it here:
http://www.linuxquestions.org/questi...hreadid=101534
You were right! Thanks again!
|
|
|
All times are GMT -5. The time now is 09:46 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|