First off, apologies if this has been asked before, I searched both here and StackOverflow, tried the man pages, and I'm still drawing a blank.

I'm trying to write a script that will auto-mount our server's archive drives on booting (for security reasons, nothing attached to the machine is automatically mounted by the system).

What I have so far is this:

#! /bin/bash
archives=( `ls /dev/disk/by-label/ | sed -rn 's/.*archive\\x20(.*)/\1/Ip'|sort -d`)

echo "Output of the Commands piped to array:"
for arcNum in ${archives[@]}; do
    echo "mounting Archive: $arcNum"

echo "Desired Output of Command:"
ls /dev/disk/by-label/ | sed -rn 's/.*archive\\x20(.*)/\1/Ip'|sort -d

Running it yields this in the terminal:

user@machine:~$ autoLoadArchives
Output of the Commands piped to array:
Desired Output of Command:

If I remove the the -n flag and p command from sed:

ls /dev/disk/by-label/ | sed -r 's/.*archive\\x20(.*)/\1/I'|sort -d

I end up with the raw unfiltered list in the array but a properly substituted and sorted unfiltered list in the command line version:

user@machine:~$ autoLoadArchives
output of the Commands piped to array:
mounting Archive: Archive\x206
mounting Archive: MY-USB
mounting Archive: PFI\x20ARCHIVE\x202
mounting Archive: PFI\x20Archive\x204
Desired Output of Command:

Output from $ ls /dev/disks/by-label/:

user@machine:~$ ls /dev/disk/by-label/
Archive\x206  MY-USB  PFI\x20ARCHIVE\x202  PFI\x20Archive\x204

I have the horrible feeling that my problem is probably something idiotically noobish, but I'm honestly drawing a blank as to what's going on here.

  • 1
    Can you include the output of ls /dev/disk/by-label/ so we can reproduce the behaviour? – choroba Sep 21 '15 at 11:07
  • No Problem! Thanks for appearing so quickly! – Minothor Sep 21 '15 at 11:16

Backquotes interpret some special characters (e.g. backslashes). Use $( ... ) instead.

archives=( $(ls /dev/disk/by-label/ \
             | sed -rn 's/.*archive\\x20(.*)/\1/Ip' \
             | sort -d) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.