Re: Comparing strings

Grant Taylor <[email protected]>
Newsgroups alt.comp.lang.shell.unix.bourne-bash
Organization TNet Consulting
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.