The most obvious error I can see is that you're not protecting the i
loop variable. So when you write $i_trip_point
, the shell is looking for a variable named i_trip_point
, which is not set so you get the second type of error, like this for example:
cat: /sys/class/thermal/thermal_zone2/trip_point__temp: No such file or directory
Two underscores between point
and temp
which marks an empty variable being interpolated. You should use the syntax ${i}_trip_point
. The braces protect your variable from adjacent characters being interpreted as part of the variable name.
Change this line:
if [[ "$TEST" == "ON Master" ]];
To this:
if [ "$TEST" == "ON Master" ];
Details
The issue is the use of [[ .. ]]
. The output is showing you the difference. Your value that you're getting for $STATUS
is not simply "On Master". It most likely contains other characters, that are most likely not printable so are not being seen.
[[ .. ]]
++ echo On Master
+ TEST='On Master'
+ [[ On Master == \O\N\ \M\a\s\t\e\r ]]
[ .. ]
++ echo On Master
+ TEST='On Master'
+ '[' 'On Master' == 'ON Master' ']'
The use of double square brackets ([[ .. ]]
) is discussed here on the TLDP Advanced Bash Scripting pages.
echo $STATUS
This line seems a little suspicious to me as well. I'd protect the contents of $STATUS
by wrapping it in double quotes as well.
TEST=`echo "$STATUS"`
Also I'd drop the back ticks ( `...` ) and use the $( ... )
notation instead for executing this command. This change is just a best practice and isn't part of your issue though.
TEST=$(echo "$STATUS")
Control characters in output (^[[92mON^[[0m Master)
Given you're seeing these control characters in your output (^[[92m
& ^[[0m
) I'm suspicious that the grep
command is introducing these into your output in the pipe. It may be that grep
is aliases to always include the --color
switch, I'd temporarily try calling the executable directly, and by pass any aliases that may be there. Just change the grep
to this, /bin/grep
.
The presence of these is what we suspected and is why the text was wrapping when you echo
the variable $STATUS
. These characters are unprintable, and change the color of the terminal to highlight matches that grep
has found.
The presence of these also explains why the =~
operator didn't match too. You were trying to match 'On Master'
with '^[[92mON^[[0m Master'
.
Lastly the colored output with the control characters may be coming from another tool before the grep
. I would need to see the actual output from /root/setup_ha
to confirm this, but I would be suspicious of that tool as well in producing these characters in the pipe stream.
Stripping the control characters
I found this U&L Q&A titled: Program that passes STDIN to STDOUT with color codes stripped?.
Use either of these methods to get rid of the control characters.
Perl
$ cmd-with-colored-output | perl -pe 's/\e\[?.*?[\@-~]//g'
Sed
$ cmd-with-colored-output | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
Best Answer
What is sh
sh
(or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88
,dash
, ...).bash
can also be considered an implementation ofsh
(see below).Because
sh
is a specification, not an implementation,/bin/sh
is a symlink (or a hard link) to an actual implementation on most POSIX systems.What is bash
bash
started as ansh
-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itselfbash
is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.bash
supports a--posix
switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked assh
.sh = bash?
For a long time,
/bin/sh
used to point to/bin/bash
on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.Some popular examples of systems where
/bin/sh
does not point to/bin/bash
(and on some of which/bin/bash
may not even exist) are:sh
todash
by default;initramfs
. It uses theash
shell implementation.pdksh
, a descendant of the Korn shell. FreeBSD'ssh
is a descendant of the original UNIX Bourne shell. Solaris has its ownsh
which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.How can you find out what
/bin/sh
points to on your system?The complication is that
/bin/sh
could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:If it's a hard link, try
In fact, the
-L
flag covers both symlinks and hardlinks, but the disadvantage of this method is that it is not portable — POSIX does not requirefind
to support the-samefile
option, although both GNU find and FreeBSD find support it.Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
E.g.
will use
sh
(and whatever that happens to point to),will use
/bin/bash
if it's available (and fail with an error message if it's not). Of course, you can also specify another implementation, e.g.Which one to use
For my own scripts, I prefer
sh
for the following reasons:bash
, they are required to havesh
There are advantages to using
bash
as well. Its features make programming more convenient and similar to programming in other modern programming languages. These include things like scoped local variables and arrays. Plainsh
is a very minimalistic programming language.