2

I'm trying to run this simple script that succesfully run on CentOS but not on Ubuntu. The script start succesfully but fails to get into the if condition and straight moving to the else.

Is there any syntax error with the if loop? The idea is to get the content of filename output2.txt into a variable named value (is that command OK?) and check if subtext 512.00GB include in the text (in value)...

sudo msecli -L > output2.txt
value=`cat output2.txt`
check="512.00GB"
#echo "$value"
if [[ "$value" == *"$check"* ]]; then
  echo "The SSD is 512GB"
sudo msecli -L >> output.txt
sudo nvme delete-ns -n 1 /dev/nvme0
sudo nvme delete-ns -n 1 /dev/nvme1
sleep 1
sudo msecli -N -q 100 -n /dev/nvme0 -r
sleep 1
sudo msecli -N -q 100 -n /dev/nvme1 -r
sleep 2
sudo reboot
else
sudo msecli -L >> output.txt
echo "***** The SSD is READY!!! *****"
fi

The error we get is in line 5 [[, which seems the if command is not working.

I used the following example I found on the web for searching subtext in a text:

#example
#STR='GNU/Linux is an operating system'
#SUB='Linux'
#if [[ "$STR" == *"$SUB"* ]]; then
#  echo "It's there."
#fi
1
  • Consider using grep (especially grep -F) to search a file for a pattern (string). – D. Ben Knoble Apr 20 at 22:01
10

If your script doesn't start with a shebang line like

#!/bin/bash

it may be run by /bin/sh (see "Which shell interpreter runs a script with no shebang?" for details).

Ubuntu uses dash for /bin/sh and dash doesn't support Bash-specific syntax like [[...]] so you get an error.

CentOS may use Bash for /bin/sh so this works there.

Add

#!/bin/bash

as the first line of your script to make sure it is run by Bash.

3
  • 2
    Or run bash script.sh – pLumo Apr 20 at 11:37
  • @steeldriver Thanks. I've added that to my answer. – Florian Diesch Apr 20 at 13:25
  • Thank you @FlorianDiesch! – Or Lapid Apr 22 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.