Help with basic scripting error


Recommended Posts

I hope this is the right place to ask this ...

I'm not new to scripting but definitely out of practice by about 20 years.

I've written a half decent BASH script to rsync two unRAID 6 servers on a disk by disk basis.

I can make this work for one disk or for all disks, using 'case' statements.

However, I want to extend this to selecting multiple disks (thus permitting me to skip certain disks) using this line:

 

#DEV version:-

#for i in `ls -d /mnt/d*|grep -v disks|grep -E '$disknum'`; do find $i -maxdepth 1 -not -empty -type d|grep -vx $i;done > /boot/scripts/syncup.list

 

I'm obviously passing 'disknum' via a $n Argument

 

I can make this work on the command line using " grep -E  'a|b|c' "  ...  but I can't seem to get it working within my script. My issue seems to be my inability to pass this string < 'a|b|c' > via a variable into the grep syntax such as " grep -E $list ".

Here's my simple test which replicates what I can/can't do:

 

--------------------------------------

user@svr1:/boot/scripts/dev# cat testlist

1

2

3

4

5

6

7

8

9

10

11

12

 

user@svr1:/boot/scripts/dev# cat testlist|grep -E '2|8'                   

2

8

12

 

Success !

... but if I try and pass it as a variable I get ...

user@svr1:/boot/scripts/dev# list="'2|8'";echo $list;cat testlist|grep -E $list

'2|8'

--------------------------------------

The last line echo's the correct value ... but causes the above test script to just ends as shown.

I've tried other combinations of parentheses but it generally error's out such as:

 

-bash: 2|8: command not found

Usage: grep [OPTION]... PATTERN [FILE]...

Try 'grep --help' for more information.

 

Any ideas/suggestions anyone ? 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.