how to grep a value stored in a variable [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working



currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt



users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?










share|improve this question















closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 4




    Single quotes ' vs. double quotes ". The former won't expand variables.
    – Sparhawk
    Dec 4 at 5:58







  • 2




    remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
    – BlackCrystal
    Dec 4 at 6:26







  • 1




    Possible duplicate of When is double-quoting necessary?
    – roaima
    Dec 4 at 8:56














up vote
-1
down vote

favorite












i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working



currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt



users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?










share|improve this question















closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 4




    Single quotes ' vs. double quotes ". The former won't expand variables.
    – Sparhawk
    Dec 4 at 5:58







  • 2




    remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
    – BlackCrystal
    Dec 4 at 6:26







  • 1




    Possible duplicate of When is double-quoting necessary?
    – roaima
    Dec 4 at 8:56












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working



currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt



users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?










share|improve this question















i am writing a bash script in which i am storing current date in a variable and then i am greping that variable . issue is its not working



currentdate= $(date +%b %d)
echo "$currentdate"
last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt



users.txt is showing empty . if i write mannualy the current date then it works
what am i doing wrong?







bash shell-script aix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 6:04

























asked Dec 4 at 5:43









Black Virus

13




13




closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Sparhawk, G-Man, roaima, Kusalananda, Christopher Dec 4 at 16:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Sparhawk, G-Man, Kusalananda
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 4




    Single quotes ' vs. double quotes ". The former won't expand variables.
    – Sparhawk
    Dec 4 at 5:58







  • 2




    remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
    – BlackCrystal
    Dec 4 at 6:26







  • 1




    Possible duplicate of When is double-quoting necessary?
    – roaima
    Dec 4 at 8:56












  • 4




    Single quotes ' vs. double quotes ". The former won't expand variables.
    – Sparhawk
    Dec 4 at 5:58







  • 2




    remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
    – BlackCrystal
    Dec 4 at 6:26







  • 1




    Possible duplicate of When is double-quoting necessary?
    – roaima
    Dec 4 at 8:56







4




4




Single quotes ' vs. double quotes ". The former won't expand variables.
– Sparhawk
Dec 4 at 5:58





Single quotes ' vs. double quotes ". The former won't expand variables.
– Sparhawk
Dec 4 at 5:58





2




2




remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
– BlackCrystal
Dec 4 at 6:26





remove that space between = and $ in currentdate. and date syntax in last is like DEC 1 . that date command returns DEC 01
– BlackCrystal
Dec 4 at 6:26





1




1




Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56




Possible duplicate of When is double-quoting necessary?
– roaima
Dec 4 at 8:56










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










In addition to the quotes, you also need to account for the date format used by the last command, which looks like the following:



Dec 3 # Note the padding to the left of '3'.
Nov 23


This requires a slightly different date command:



date "+%b %_d"
Dec 4


The underscore instructs date to pad the field with spaces. You can also use %e as an alternative.



Putting these together, you can modify your script as shown below:



currentdate=$(date "+%b %_d")
last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt





share|improve this answer



























    up vote
    0
    down vote













    @Sparhawk is correct.



    Double quotes cause the shell to expand variables.



    $ VAR=blahblah
    $ echo "$VAR"
    blahblah


    Single quotes cause the shell to use the text literally.



    $ VAR=blahblah
    $ echo '$VAR'
    $VAR


    You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.



    echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'


    Without single quotes the above message wouldn't output properly.






    share|improve this answer




















    • Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
      – Black Virus
      Dec 4 at 6:29


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    In addition to the quotes, you also need to account for the date format used by the last command, which looks like the following:



    Dec 3 # Note the padding to the left of '3'.
    Nov 23


    This requires a slightly different date command:



    date "+%b %_d"
    Dec 4


    The underscore instructs date to pad the field with spaces. You can also use %e as an alternative.



    Putting these together, you can modify your script as shown below:



    currentdate=$(date "+%b %_d")
    last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt





    share|improve this answer
























      up vote
      0
      down vote



      accepted










      In addition to the quotes, you also need to account for the date format used by the last command, which looks like the following:



      Dec 3 # Note the padding to the left of '3'.
      Nov 23


      This requires a slightly different date command:



      date "+%b %_d"
      Dec 4


      The underscore instructs date to pad the field with spaces. You can also use %e as an alternative.



      Putting these together, you can modify your script as shown below:



      currentdate=$(date "+%b %_d")
      last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt





      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        In addition to the quotes, you also need to account for the date format used by the last command, which looks like the following:



        Dec 3 # Note the padding to the left of '3'.
        Nov 23


        This requires a slightly different date command:



        date "+%b %_d"
        Dec 4


        The underscore instructs date to pad the field with spaces. You can also use %e as an alternative.



        Putting these together, you can modify your script as shown below:



        currentdate=$(date "+%b %_d")
        last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt





        share|improve this answer












        In addition to the quotes, you also need to account for the date format used by the last command, which looks like the following:



        Dec 3 # Note the padding to the left of '3'.
        Nov 23


        This requires a slightly different date command:



        date "+%b %_d"
        Dec 4


        The underscore instructs date to pad the field with spaces. You can also use %e as an alternative.



        Putting these together, you can modify your script as shown below:



        currentdate=$(date "+%b %_d")
        last | grep "$currentdate" >> /usr/IBM/HTTPServer7/logs/alert/users.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 at 9:30









        Haxiel

        851310




        851310






















            up vote
            0
            down vote













            @Sparhawk is correct.



            Double quotes cause the shell to expand variables.



            $ VAR=blahblah
            $ echo "$VAR"
            blahblah


            Single quotes cause the shell to use the text literally.



            $ VAR=blahblah
            $ echo '$VAR'
            $VAR


            You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.



            echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'


            Without single quotes the above message wouldn't output properly.






            share|improve this answer




















            • Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
              – Black Virus
              Dec 4 at 6:29















            up vote
            0
            down vote













            @Sparhawk is correct.



            Double quotes cause the shell to expand variables.



            $ VAR=blahblah
            $ echo "$VAR"
            blahblah


            Single quotes cause the shell to use the text literally.



            $ VAR=blahblah
            $ echo '$VAR'
            $VAR


            You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.



            echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'


            Without single quotes the above message wouldn't output properly.






            share|improve this answer




















            • Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
              – Black Virus
              Dec 4 at 6:29













            up vote
            0
            down vote










            up vote
            0
            down vote









            @Sparhawk is correct.



            Double quotes cause the shell to expand variables.



            $ VAR=blahblah
            $ echo "$VAR"
            blahblah


            Single quotes cause the shell to use the text literally.



            $ VAR=blahblah
            $ echo '$VAR'
            $VAR


            You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.



            echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'


            Without single quotes the above message wouldn't output properly.






            share|improve this answer












            @Sparhawk is correct.



            Double quotes cause the shell to expand variables.



            $ VAR=blahblah
            $ echo "$VAR"
            blahblah


            Single quotes cause the shell to use the text literally.



            $ VAR=blahblah
            $ echo '$VAR'
            $VAR


            You would use this if you wanted to prevent the shell from thinking a dollar sign plus some other text was a variable.



            echo 'This script is terminating because you didn't set $IMPORTANT_VARIABLE'


            Without single quotes the above message wouldn't output properly.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 4 at 6:18









            LawrenceC

            8,42222340




            8,42222340











            • Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
              – Black Virus
              Dec 4 at 6:29

















            • Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
              – Black Virus
              Dec 4 at 6:29
















            Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
            – Black Virus
            Dec 4 at 6:29





            Issue is with this command all though currentdate has correct value which is today Dec 04 but in users.txt the file is empty . last |grep -E '$currentdate'>> /usr/IBM/HTTPServer7/logs/alert/users.txt if i use actual value Dec 04 in the above command the result is fine .
            – Black Virus
            Dec 4 at 6:29



            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?