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.')
bashquestion?datedoes 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.