Results 1 to 10 of 10

Thread: Set variable in script

  1. #1
    Join Date
    Dec 2006
    Location
    Finland
    Beans
    859
    Distro
    Ubuntu 12.04 Precise Pangolin

    Set variable in script

    Hi

    How I can set
    Code:
    grep 'TEST' ~/MY.TEST -c -w
    to variable named MYINT in bash script and then use it in if statement e.g.
    Code:
    if [ $MYINT = 0 ]
    then
    echo 'varianle is null'
    else
    echo $MYINT
    fi
    Last edited by Waappu; January 30th, 2007 at 12:06 AM.
    Regards,
    Jari

  2. #2
    Join Date
    Dec 2006
    Beans
    57

    Re: Set variable in script

    MYINT=`grep 'TEST' ~/MY.TEST -c -w`
    -- Guilherme H. Polo Goncalves <ggpolo@gmail.com>

  3. #3
    Join Date
    Dec 2006
    Location
    Finland
    Beans
    859
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Set variable in script

    Hi

    Thank you =)
    Regards,
    Jari

  4. #4
    Join Date
    Aug 2006
    Beans
    923
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Set variable in script

    gpolo's code is correct. Note carefully that the statement is surrouding not in normal quotes but in backtick characters (usually found on the upper-left of the keyboard). Surrounding a bash command with backtick characters means that the shell will execute the command and turn the return value into a string you can use elsewhere.

    Hope that helps.

  5. #5
    Join Date
    Dec 2006
    Location
    Finland
    Beans
    859
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Set variable in script

    Hi

    Yes this help. I some how missed that backtick.
    Thanks to both of you
    Regards,
    Jari

  6. #6
    Join Date
    Sep 2006
    Beans
    Hidden!

    Re: Set variable in script

    Waappu,

    The special variable '$?' contains the exit code of the last run command. You could also use this instead of assigning to a new variable if you wish. This works from the command line as well as from a script.

  7. #7
    Join Date
    Dec 2006
    Location
    Finland
    Beans
    859
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Set variable in script

    Quote Originally Posted by po0f View Post
    Waappu,

    The special variable '$?' contains the exit code of the last run command. You could also use this instead of assigning to a new variable if you wish. This works from the command line as well as from a script.
    Hi

    Sorry but I don't understand. Could you please post short sample how I should use it ?

    Another question
    How I serch match case for this
    Code:
    "TEST"
    I try this but syntax is wrong
    Code:
    MYINT=`grep '"TEST"' ~/my.test -c -w`
    Regards,
    Jari

  8. #8
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Set variable in script

    when grep runs, it returns 2 things, output and an exit code.

    consider the following:
    Code:
    result = $(grep "TEST" ~/mytest -c -w)
    exitcode $?
    the first line will assign any output from grep to result. if grep output anything, result will be not empty. the exitcode variable will contain the exit code from the previous command, if grep ran perfectly (whether it found anything or not), exitcode will be 0.

  9. #9
    Join Date
    Sep 2006
    Beans
    Hidden!

    Re: Set variable in script

    Waappu,

    Code:
    grep 'TEST' ~/MY.TEST -c -w
    
    if [ $? = 0 ]; then
        echo 'variable is zero'
    else
        echo $?
    fi
    The end result is the same, I was just letting you know.

    BTW, null and zero are two different things.

  10. #10
    Join Date
    Dec 2006
    Location
    Finland
    Beans
    859
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Set variable in script

    Hi

    Thanks very much. Now I get it =)
    Also find answer/my mistake to other queston
    Regards,
    Jari

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •