0

When checking $date while using $grep, I am getting an unexpected result. I am using python with the os module to check a bash command's output.

if not

works, creates 1 file of the current date.

if

does not work, creates files for all days BUT current day.

I would expect that if $date contains the string I am seeking, it would only create a file for that day, however the opposite occurs. Here is the function:

def write(self):
    
    #Formatting saved message
    msg = input("Enter a message: ")
    current_time = datetime.now().strftime("%m/%d/%Y | %H:%M")
    x = '\n{0} - {1}\n'.format(current_time, msg)

    try:

        #Checking string contained in $date output
        options = (
            'Mon',
            'Tue',
            'Wed',
            'Thu',
            'Fri',
            'Sat',
            'Sun'
            )
        
        for option in options:
            
            #Here is the "if not" in question.

            if not os.system(f'date | grep -q {option}'):
                y = option

                os.system('var=~/reminder_notes\n'
                         f'if [ -e $var ]; then echo "Writing to directory: ~/reminder_notes."\n'
                        
                          'else\n'
                            'mkdir $var && echo "Created directory: $var"\n'
                          'fi'
                        )
                os.system(f'echo "{x}" >> ~/reminder_notes/{y}.txt')

        print('\nMessage has been recorded.\n')

    except:
        print('\nAn error occured.')
3
  • Is this a bash question? Commented Jan 16, 2021 at 19:35
  • I think it is both bash and python. Python is checking the bash command through the os.system method. Commented Jan 16, 2021 at 19:36
  • 1
    if date does not contain the string you search, the return value is different from 0, and in python it is truth-value. So with if and non-zeror exit code it will create file for all days but one. Commented Jan 16, 2021 at 19:38

2 Answers 2

1

In sh/bash, an exit status of 0 is success (true), and 1+ is failure (false).

In Python, the integer 0 is falsy and 1+ is truthy.

Since the two have the opposite interpretation of the truthiness of integers, the check does the opposite.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a beautiful example of how different languages treat success.

date | grep -q {option} returns 0 if the grep command succeeds, because Bash (and Linux as a whole, and Windows) treat an exit code of 0 as successful and anything other than 0 as some sort of failure. However, in Python, 0 is considered equivalent to False (with anything other than 0 considered equivalent to True). This is why, if you ask Python to consider the truth of the Bash command, it will say "It's 0, and therefore False" if the command succeeds, and will say "It's not 0, and therefore True" if the command fails.

HTH!

1 Comment

Thanks for the explanation! I marked the other as the answer as he beat you to the punch. Much appreciated though, and very interesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.