LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash env vars alias's & .bashrc (https://www.linuxquestions.org/questions/linux-general-1/bash-env-vars-aliass-and-bashrc-101397/)

micxz 10-07-2003 06:36 PM

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;

ToniT 10-07-2003 07:13 PM

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'


micxz 10-07-2003 07:26 PM

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'

ToniT 10-07-2003 07:40 PM

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'

micxz 10-07-2003 07:59 PM

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

micxz 10-07-2003 08:02 PM

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;

Mad Merlin 10-07-2003 08:36 PM

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.

ToniT 10-08-2003 06:20 AM

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. ;)

micxz 10-08-2003 02:09 PM

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 02:05 PM.