Re: Comparing strings

ELCV <[email protected]> Wed, 14 Jul 2021 08:42:21 -0700 (PDT)
Newsgroups alt.comp.lang.shell.unix.bourne-bash
Message-ID <[email protected]>
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 [email protected] 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 [email protected] 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 [email protected] dd of=/backup/images/$ARG$SUFF.img"

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

Thanks.

On Tuesday, July 13, 2021 at 9:53:17 PM UTC-4, ELCV wrote:
> Thank you for the replies. I did go with the case statement and functions.
> On Tuesday, July 13, 2021 at 4:29:33 PM UTC-4, Grant Taylor wrote: 
> > 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