Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comparing strings

8 views
Skip to first unread message

ELCV

unread,
Jul 13, 2021, 1:46:44 PM7/13/21
to
Hello

I can't get this right. What I have is a script to backup VM guests. The command is called with an argument, for example:

/usr/local/bin/vm-backup.sh FOO

Some of the VMs have two disks and I want to back them both up. Two of the VMs have vert large data partitions that are backed up separately and I want to exclude them.

If I do this:

if [ "$1" != "FOO" ];
then
echo "I can backup the 2nd volume";
else
exit;
fi

It works. But if I add an "or" condition as follows:

if [ "$1" != "FOO" ] || [ "$1" != "BAR" ] ;
then
echo "I can backup the 2nd volume";
else
exit;
fi

It fails. I have tried this a number of different ways and I can't get it. Any guidance is appreciated. Thanks.

Grant Taylor

unread,
Jul 13, 2021, 4:29:33 PM7/13/21
to
On 7/13/21 11:46 AM, ELCV wrote:
> Hello

Hi,

> I can't get this right. What I have is a script to backup VM
> guests. The command is called with an argument, for example:
>
> /usr/local/bin/vm-backup.sh FOO
>
> Some of the VMs have two disks and I want to back them both up. Two of
> the VMs have vert large data partitions that are backed up separately
> and I want to exclude them.

Okay. That sounds like some VMs can use a default, some need slightly
different, and still others need something special.

> If I do this:
>
> if [ "$1" != "FOO" ];
> then
> echo "I can backup the 2nd volume";
> else
> exit;
> fi
>
> It works. But if I add an "or" condition as follows:
>
> if [ "$1" != "FOO" ] || [ "$1" != "BAR" ] ;
> then
> echo "I can backup the 2nd volume";
> else
> exit;
> fi
>
> It fails.

ORed negative logic is ... tricksy.

> I have tried this a number of different ways and I can't get it. Any
> guidance is appreciated. Thanks.

I would be tempted to try a case statement:

case ${1} in
FOO)
# FOO's specific backup.
BAR|BOB)
# specific backup for BAR and BOB.
*)
# default backup for all other systems.
esac

I also find that such a case statement is a lot easier to maintain long
term as systems are added / changed / removed.

Depending on how the backups work, you might consider breaking out the
actual backup actions to their discrete steps and call them as a
function. E.g. FOO would call functions 1 and 2, while BAR & BOB call
functions 2 and 3, and then everyone else would only call functions 1
and 3. Use the case as a method to choosoe which pieces to run and then
call the necessary common pieces.



--
Grant. . . .
unix || die

Lewis

unread,
Jul 13, 2021, 5:00:30 PM7/13/21
to
In message <sckt5u$e63$1...@tncsrv09.home.tnetconsulting.net> Grant Taylor <gta...@tnetconsulting.net> wrote:
>> if [ "$1" != "FOO" ];

if [[ $1 != "FOO" ]];

work for me.

> I would be tempted to try a case statement:

> case ${1} in
> FOO)
> # FOO's specific backup.
> BAR|BOB)
> # specific backup for BAR and BOB.
> *)
> # default backup for all other systems.
> esac

Case is what I would do as well.

> I also find that such a case statement is a lot easier to maintain long
> term as systems are added / changed / removed.

And that's why

> Depending on how the backups work, you might consider breaking out the
> actual backup actions to their discrete steps and call them as a
> function. E.g. FOO would call functions 1 and 2, while BAR & BOB call
> functions 2 and 3, and then everyone else would only call functions 1
> and 3. Use the case as a method to choosoe which pieces to run and then
> call the necessary common pieces.

Agreed.

--
The more you tighten your grip, Tarkin, the more star systems will
slip through your fingers.

Michael F. Stemper

unread,
Jul 13, 2021, 5:24:01 PM7/13/21
to
On 13/07/2021 12.46, ELCV wrote:

> If I do this:
>
> if [ "$1" != "FOO" ];
> then
> echo "I can backup the 2nd volume";
> else
> exit;
> fi
>
> It works. But if I add an "or" condition as follows:
>
> if [ "$1" != "FOO" ] || [ "$1" != "BAR" ] ;

I've never seen this syntax. I always thought that the "or"
needed to be within the test, as in:

if [[ "$1" != "FOO" ||$1 != "BAR" ]]


--
Michael F. Stemper
Psalm 94:3-6

Lewis

unread,
Jul 13, 2021, 8:07:15 PM7/13/21
to
There's a difference between [[ ]] and [ ] ([[ ]] is superior and you
should probably just get used to using [[ ]] exclusively).

But the syntax used in the sample above is incorrect, i believe, as
single brackets require that you use -o for or.

you really only need to deal with single [ ] if you want to be
compatible with sh.

--
"Are you pondering what I'm pondering?"
"I think so, but where is a fish?"

ELCV

unread,
Jul 13, 2021, 9:53:17 PM7/13/21
to
Thank you for the replies. I did go with the case statement and functions.

ELCV

unread,
Jul 14, 2021, 11:42:22 AM7/14/21
to
A new issue. I am evaluating my script so I am using echo to display the commands.

My function is as follows:
singleVolume(){
# OS Volumes
echo "virsh suspend $ARG"
echo "dd if=/dev/vg0/$ARG_0 | ssh us...@111.111.111.111 dd of=/backup/images/$ARG_0.img"
echo "virsh resume $ARG"
}

When I run my command the $ARG variable in the second line is not echoed:

virsh suspend FOO
dd if=/dev/vg0/ | ssh us...@111.111.111.111 dd of=/backup/images/.img
virsh resume LIMS2

So the variable and the _0 are being dropped. I've tested without "echo" and obtained the same result.

If I create a new variable, SUFF="_0" and then use echo "dd if=/dev/vg0/$ARG$SUFF | ssh us...@111.111.111.111 dd of=/backup/images/$ARG$SUFF.img"

It works. Is this expected? It only seems an issue using functions.

Thanks.

Grant Taylor

unread,
Jul 14, 2021, 11:58:17 AM7/14/21
to
On 7/14/21 9:42 AM, ELCV wrote:
> A new issue. I am evaluating my script so I am using echo to display
> the commands.
>
> My function is as follows:
> singleVolume(){
> # OS Volumes
> echo "virsh suspend $ARG"
> echo "dd if=/dev/vg0/$ARG_0 | ssh us...@111.111.111.111 dd of=/backup/images/$ARG_0.img"
> echo "virsh resume $ARG"
> }
>
> When I run my command the $ARG variable in the second line is not echoed:
>
> virsh suspend FOO
> dd if=/dev/vg0/ | ssh us...@111.111.111.111 dd of=/backup/images/.img
> virsh resume LIMS2
>
> So the variable and the _0 are being dropped. I've tested without
> "echo" and obtained the same result.
>
> If I create a new variable, SUFF="_0" and then use echo
> "dd if=/dev/vg0/$ARG$SUFF | ssh us...@111.111.111.111 dd
> of=/backup/images/$ARG$SUFF.img"
>
> It works. Is this expected? It only seems an issue using functions.

I don't know if I would /expect/ it per se or not. But I am not at all
surprised by it.

This hints at an escaping problem to me. Maybe. Maybe not.

Try changing your ssh command to be:

ssh us...@111.111.111.111 dd of=/backup/images/${ARG}_0.img

Avoid the abiguity of what is variable vs appended string.

Aside: That might not be the escaping that I was referring to as
escaping would usually be how your local shell interprets $ARG, or not,
prior to sending it to the remote shell / sub-command. Escaping is more
about where the expansion should happen. And I think your primary /
current issue is what is the variable vs data.

> Thanks.

You're welcome.

ELCV

unread,
Jul 14, 2021, 1:09:18 PM7/14/21
to
> Try changing your ssh command to be:
>
> ssh us...@111.111.111.111 dd of=/backup/images/${ARG}_0.img
>

That works! Thank you again.

Grant Taylor

unread,
Jul 14, 2021, 1:37:06 PM7/14/21
to
On 7/14/21 11:09 AM, ELCV wrote:
> That works!

Good. :-)

> Thank you again.

Lewis

unread,
Jul 14, 2021, 7:34:11 PM7/14/21
to
In message <2e499cd2-6522-4901...@googlegroups.com> ELCV <clsamc...@gmail.com> wrote:
> A new issue. I am evaluating my script so I am using echo to display the commands.

> My function is as follows:
> singleVolume(){
> # OS Volumes
> echo "virsh suspend $ARG"
> echo "dd if=/dev/vg0/$ARG_0 | ssh us...@111.111.111.111 dd of=/backup/images/$ARG_0.img"
> echo "virsh resume $ARG"
>}

> When I run my command the $ARG variable in the second line is not echoed:

Because you are specifying a variable named $ARG_0 try

echo "if=/dev/vg0/${ARG}_0 ..."


--
You know what they say about paradigms: Shift happens.
0 new messages