Score:0

Why fails the comparsion between two identical version numbers in this script?

cx flag

I try to compare in a Script two different version numbers of Drupal via drush status. The result should be printed out in a if-else. I made a minimal example:

# put paths in vars
WEBSITE_ROOT_LIVE=/PATH/TO/LIVE
WEBSITE_ROOT_TEST=/PATH/TO/DEV

# get the drupal version line from drupal status output
cd $WEBSITE_ROOT_TEST
test=$(drush status | grep "Drupal version")
cd $WEBSITE_ROOT_LIVE
live=$(drush status | grep "Drupal version")

# count chars in output
wc -c <<< $live
wc -c <<< $test

# print variables
echo $live
echo $test

# compare
if [ "$test" != "$live" ]
then
    echo "not equal"
else
    
    echo "equal"
fi

Strangely this is the output:

84
88
Drupal version : 9.5.0
Drupal version : 9.5.0
not equal

The difference in characters is the explanation, but why are they different? On one side the string has not so much chars, on the other side they are different length. Why is that so, and how can i fix this? I suspect the substitution within the shells $() syntax.

Score:2
cn flag

The standard output of Drush is for humans.

For scripts use the Drush output options to select the field and output a string value:

drush status --fields=drupal-version --format=string

or shorter with --field=FIELD to select just one field, and force format to string.

drush status --field=drupal-version

See drush help status.

gzwg avatar
cx flag
Perfect that does the trick, thank you very much! But do you know why the grepped output is diffrent, seems wired...
4uk4 avatar
cn flag
I think different Drupal/Drush version have different whitespaces. The human readable output is not an API.
gzwg avatar
cx flag
Thanks for the explanation and the hint for fixing!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.