How to get date as a run time variable in Linux [closed]

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











up vote
-2
down vote

favorite












I Tried these following, but failed. I need a date format in output.



--------1st try--------



#!/bin/bash
echo "read date yyyymmdd"

read $temp

dd=$(date -j -f '%Y%m%d' "$temp" +'%Y%m%d')

echo $dd


Output: invalid option -- 'j'



--------2nd try--------



#!/bin/bash

echo "read date yyyymmdd"

read $temp

dd=$(date -d "$temp" +'%Y%m%d')

echo $dd


Output: it prints today's date.







share|improve this question













closed as unclear what you're asking by steve, schily, Jesse_b, G-Man, Romeo Ninov Jun 8 at 13:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Question is not clearly asked, throw some examples.
    – Naushad Ahmad
    Jun 7 at 7:52










  • According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
    – ctrl-alt-delor
    Jun 7 at 9:29










  • What do you get if you do echo $temp?
    – ctrl-alt-delor
    Jun 7 at 9:33














up vote
-2
down vote

favorite












I Tried these following, but failed. I need a date format in output.



--------1st try--------



#!/bin/bash
echo "read date yyyymmdd"

read $temp

dd=$(date -j -f '%Y%m%d' "$temp" +'%Y%m%d')

echo $dd


Output: invalid option -- 'j'



--------2nd try--------



#!/bin/bash

echo "read date yyyymmdd"

read $temp

dd=$(date -d "$temp" +'%Y%m%d')

echo $dd


Output: it prints today's date.







share|improve this question













closed as unclear what you're asking by steve, schily, Jesse_b, G-Man, Romeo Ninov Jun 8 at 13:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Question is not clearly asked, throw some examples.
    – Naushad Ahmad
    Jun 7 at 7:52










  • According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
    – ctrl-alt-delor
    Jun 7 at 9:29










  • What do you get if you do echo $temp?
    – ctrl-alt-delor
    Jun 7 at 9:33












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I Tried these following, but failed. I need a date format in output.



--------1st try--------



#!/bin/bash
echo "read date yyyymmdd"

read $temp

dd=$(date -j -f '%Y%m%d' "$temp" +'%Y%m%d')

echo $dd


Output: invalid option -- 'j'



--------2nd try--------



#!/bin/bash

echo "read date yyyymmdd"

read $temp

dd=$(date -d "$temp" +'%Y%m%d')

echo $dd


Output: it prints today's date.







share|improve this question













I Tried these following, but failed. I need a date format in output.



--------1st try--------



#!/bin/bash
echo "read date yyyymmdd"

read $temp

dd=$(date -j -f '%Y%m%d' "$temp" +'%Y%m%d')

echo $dd


Output: invalid option -- 'j'



--------2nd try--------



#!/bin/bash

echo "read date yyyymmdd"

read $temp

dd=$(date -d "$temp" +'%Y%m%d')

echo $dd


Output: it prints today's date.









share|improve this question












share|improve this question




share|improve this question








edited Jun 7 at 7:44









Kusalananda

101k13199312




101k13199312









asked Jun 7 at 7:13









Sumit Kumar

1




1




closed as unclear what you're asking by steve, schily, Jesse_b, G-Man, Romeo Ninov Jun 8 at 13:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by steve, schily, Jesse_b, G-Man, Romeo Ninov Jun 8 at 13:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • Question is not clearly asked, throw some examples.
    – Naushad Ahmad
    Jun 7 at 7:52










  • According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
    – ctrl-alt-delor
    Jun 7 at 9:29










  • What do you get if you do echo $temp?
    – ctrl-alt-delor
    Jun 7 at 9:33
















  • Question is not clearly asked, throw some examples.
    – Naushad Ahmad
    Jun 7 at 7:52










  • According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
    – ctrl-alt-delor
    Jun 7 at 9:29










  • What do you get if you do echo $temp?
    – ctrl-alt-delor
    Jun 7 at 9:33















Question is not clearly asked, throw some examples.
– Naushad Ahmad
Jun 7 at 7:52




Question is not clearly asked, throw some examples.
– Naushad Ahmad
Jun 7 at 7:52












According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
– ctrl-alt-delor
Jun 7 at 9:29




According to what you say you want. I see the output of today's date, and say test passed. If that test did not pass, then you have to re-specify what it is that you are trying to do. You can not give broken code as a spec.
– ctrl-alt-delor
Jun 7 at 9:29












What do you get if you do echo $temp?
– ctrl-alt-delor
Jun 7 at 9:33




What do you get if you do echo $temp?
– ctrl-alt-delor
Jun 7 at 9:33










2 Answers
2






active

oldest

votes

















up vote
1
down vote













The main issue is read $temp. To read something into a variable, don't prefix it with $ (this would access the variable's value):



read temp


The second issue is that you don't seem to know what implementation of the date command you are using. GNU date does not have -j flag. This flag is however available with other implementations of date (e.g. on BSD systems, where it causes the utility to parse the given datestamp and do output, but not to set the date).



The third issue is that you seem to want to convert $temp into YYYYMMDD format using date, but you are already asking the user to input the date on this format, so the conversion is a no-op.



A corrected version of your script (written for GNU date, and not doing anything about the third issue as it's unclear what you actually want to do):



#!/bin/bash

read -p 'Enter date (YYYYMMDD): ' indate
date -d "$indate" +'%Y%m%d'


If all you want to do is to get the date into a variable and then output it, then there is no reason to ask the user for the date:



#!/bin/sh

thedate=$( date +'%Y%m%d' )
printf 'The date is %sn' "$thedate"


or, if you don't need to store it in a variable for later,



#!/bin/sh

date +'The date is %Y%m%d'





share|improve this answer






























    up vote
    -2
    down vote













    So you're trying to read some input into the $temp variable, then print it next to todays date?



    You don't want the $ in front of the temp variable for the read command:



    #!/bin/bash
    echo "read date yyyymmdd"
    read temp
    echo "$temp + $(date '+%Y%m%d')"





    share|improve this answer























    • You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
      – Inian
      Jun 7 at 7:33











    • You fixed what was broken, and broke what was working.
      – ctrl-alt-delor
      Jun 7 at 9:35










    • copy and paste missed the last quote, fixed it now
      – rusty shackleford
      Jun 7 at 10:14

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    The main issue is read $temp. To read something into a variable, don't prefix it with $ (this would access the variable's value):



    read temp


    The second issue is that you don't seem to know what implementation of the date command you are using. GNU date does not have -j flag. This flag is however available with other implementations of date (e.g. on BSD systems, where it causes the utility to parse the given datestamp and do output, but not to set the date).



    The third issue is that you seem to want to convert $temp into YYYYMMDD format using date, but you are already asking the user to input the date on this format, so the conversion is a no-op.



    A corrected version of your script (written for GNU date, and not doing anything about the third issue as it's unclear what you actually want to do):



    #!/bin/bash

    read -p 'Enter date (YYYYMMDD): ' indate
    date -d "$indate" +'%Y%m%d'


    If all you want to do is to get the date into a variable and then output it, then there is no reason to ask the user for the date:



    #!/bin/sh

    thedate=$( date +'%Y%m%d' )
    printf 'The date is %sn' "$thedate"


    or, if you don't need to store it in a variable for later,



    #!/bin/sh

    date +'The date is %Y%m%d'





    share|improve this answer



























      up vote
      1
      down vote













      The main issue is read $temp. To read something into a variable, don't prefix it with $ (this would access the variable's value):



      read temp


      The second issue is that you don't seem to know what implementation of the date command you are using. GNU date does not have -j flag. This flag is however available with other implementations of date (e.g. on BSD systems, where it causes the utility to parse the given datestamp and do output, but not to set the date).



      The third issue is that you seem to want to convert $temp into YYYYMMDD format using date, but you are already asking the user to input the date on this format, so the conversion is a no-op.



      A corrected version of your script (written for GNU date, and not doing anything about the third issue as it's unclear what you actually want to do):



      #!/bin/bash

      read -p 'Enter date (YYYYMMDD): ' indate
      date -d "$indate" +'%Y%m%d'


      If all you want to do is to get the date into a variable and then output it, then there is no reason to ask the user for the date:



      #!/bin/sh

      thedate=$( date +'%Y%m%d' )
      printf 'The date is %sn' "$thedate"


      or, if you don't need to store it in a variable for later,



      #!/bin/sh

      date +'The date is %Y%m%d'





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        The main issue is read $temp. To read something into a variable, don't prefix it with $ (this would access the variable's value):



        read temp


        The second issue is that you don't seem to know what implementation of the date command you are using. GNU date does not have -j flag. This flag is however available with other implementations of date (e.g. on BSD systems, where it causes the utility to parse the given datestamp and do output, but not to set the date).



        The third issue is that you seem to want to convert $temp into YYYYMMDD format using date, but you are already asking the user to input the date on this format, so the conversion is a no-op.



        A corrected version of your script (written for GNU date, and not doing anything about the third issue as it's unclear what you actually want to do):



        #!/bin/bash

        read -p 'Enter date (YYYYMMDD): ' indate
        date -d "$indate" +'%Y%m%d'


        If all you want to do is to get the date into a variable and then output it, then there is no reason to ask the user for the date:



        #!/bin/sh

        thedate=$( date +'%Y%m%d' )
        printf 'The date is %sn' "$thedate"


        or, if you don't need to store it in a variable for later,



        #!/bin/sh

        date +'The date is %Y%m%d'





        share|improve this answer















        The main issue is read $temp. To read something into a variable, don't prefix it with $ (this would access the variable's value):



        read temp


        The second issue is that you don't seem to know what implementation of the date command you are using. GNU date does not have -j flag. This flag is however available with other implementations of date (e.g. on BSD systems, where it causes the utility to parse the given datestamp and do output, but not to set the date).



        The third issue is that you seem to want to convert $temp into YYYYMMDD format using date, but you are already asking the user to input the date on this format, so the conversion is a no-op.



        A corrected version of your script (written for GNU date, and not doing anything about the third issue as it's unclear what you actually want to do):



        #!/bin/bash

        read -p 'Enter date (YYYYMMDD): ' indate
        date -d "$indate" +'%Y%m%d'


        If all you want to do is to get the date into a variable and then output it, then there is no reason to ask the user for the date:



        #!/bin/sh

        thedate=$( date +'%Y%m%d' )
        printf 'The date is %sn' "$thedate"


        or, if you don't need to store it in a variable for later,



        #!/bin/sh

        date +'The date is %Y%m%d'






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 7 at 9:19


























        answered Jun 7 at 7:41









        Kusalananda

        101k13199312




        101k13199312






















            up vote
            -2
            down vote













            So you're trying to read some input into the $temp variable, then print it next to todays date?



            You don't want the $ in front of the temp variable for the read command:



            #!/bin/bash
            echo "read date yyyymmdd"
            read temp
            echo "$temp + $(date '+%Y%m%d')"





            share|improve this answer























            • You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
              – Inian
              Jun 7 at 7:33











            • You fixed what was broken, and broke what was working.
              – ctrl-alt-delor
              Jun 7 at 9:35










            • copy and paste missed the last quote, fixed it now
              – rusty shackleford
              Jun 7 at 10:14














            up vote
            -2
            down vote













            So you're trying to read some input into the $temp variable, then print it next to todays date?



            You don't want the $ in front of the temp variable for the read command:



            #!/bin/bash
            echo "read date yyyymmdd"
            read temp
            echo "$temp + $(date '+%Y%m%d')"





            share|improve this answer























            • You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
              – Inian
              Jun 7 at 7:33











            • You fixed what was broken, and broke what was working.
              – ctrl-alt-delor
              Jun 7 at 9:35










            • copy and paste missed the last quote, fixed it now
              – rusty shackleford
              Jun 7 at 10:14












            up vote
            -2
            down vote










            up vote
            -2
            down vote









            So you're trying to read some input into the $temp variable, then print it next to todays date?



            You don't want the $ in front of the temp variable for the read command:



            #!/bin/bash
            echo "read date yyyymmdd"
            read temp
            echo "$temp + $(date '+%Y%m%d')"





            share|improve this answer















            So you're trying to read some input into the $temp variable, then print it next to todays date?



            You don't want the $ in front of the temp variable for the read command:



            #!/bin/bash
            echo "read date yyyymmdd"
            read temp
            echo "$temp + $(date '+%Y%m%d')"






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 7 at 9:38


























            answered Jun 7 at 7:21









            rusty shackleford

            1,135115




            1,135115











            • You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
              – Inian
              Jun 7 at 7:33











            • You fixed what was broken, and broke what was working.
              – ctrl-alt-delor
              Jun 7 at 9:35










            • copy and paste missed the last quote, fixed it now
              – rusty shackleford
              Jun 7 at 10:14
















            • You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
              – Inian
              Jun 7 at 7:33











            • You fixed what was broken, and broke what was working.
              – ctrl-alt-delor
              Jun 7 at 9:35










            • copy and paste missed the last quote, fixed it now
              – rusty shackleford
              Jun 7 at 10:14















            You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
            – Inian
            Jun 7 at 7:33





            You don't need to use separate echo, read can provide a banner with the -p flag as read -p "read date yyyymmdd"$'n' temp
            – Inian
            Jun 7 at 7:33













            You fixed what was broken, and broke what was working.
            – ctrl-alt-delor
            Jun 7 at 9:35




            You fixed what was broken, and broke what was working.
            – ctrl-alt-delor
            Jun 7 at 9:35












            copy and paste missed the last quote, fixed it now
            – rusty shackleford
            Jun 7 at 10:14




            copy and paste missed the last quote, fixed it now
            – rusty shackleford
            Jun 7 at 10:14


            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?