LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 10-07-2003, 06:36 PM   #1
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
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.
 
Old 10-07-2003, 07:13 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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'
 
Old 10-07-2003, 07:26 PM   #3
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
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'
 
Old 10-07-2003, 07:40 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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'
 
Old 10-07-2003, 07:59 PM   #5
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
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
 
Old 10-07-2003, 08:02 PM   #6
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
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;
 
Old 10-07-2003, 08:36 PM   #7
Mad Merlin
Member
 
Registered: Aug 2003
Location: Approximately here.
Distribution: Mandrake 9.1
Posts: 86

Rep: Reputation: 15
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.
 
Old 10-08-2003, 06:20 AM   #8
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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.
 
Old 10-08-2003, 02:09 PM   #9
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Original Poster
Rep: Reputation: 75
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rbscrobbler not queueing/setting vars, then missing /bin/env kiwibird Linux - Software 1 10-04-2005 09:39 PM
N00b: Simple Question about Bash Func and Vars: TylerD75 Programming 6 04-03-2005 06:03 AM
env vars on Slackware 10 lvella Linux - General 1 11-01-2004 04:55 PM
bash shell env var script ejbest Linux - Newbie 4 09-29-2004 10:23 AM
init on boot programs that need env vars The_Spider Linux - General 3 04-29-2004 07:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration