Score:2

Why "mount | grep sdb" displays no output, when "sdb" drive is a [swap] partition?

in flag

I'm learning about lsblk and mount commands in Ubuntu. I'm using Linode vps server, 2CPU, 4GB ram with 1 additional block storage volume attached.

I have 2 questions related with this subject.

At first I've run lsblk command:

michal@ubuntu:~$ lsblk
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda    8:0    0 79.5G  0 disk /
sdb    8:16   0  512M  0 disk [SWAP]
sdc    8:32   0   50G  0 disk /mnt/movies

Here I understand everything.

sda and sdb are both the default partitions I had, when I first created my linode and sdc is the block storage volume I've attached later.

Then I've run mount command with grep, like this:

michal@ubuntu:~$ mount | grep sda
/dev/sda on / type ext4 (rw,relatime,errors=remount-ro)
michal@ubuntu:~$ mount | grep sdb
michal@ubuntu:~$ mount | grep sdc
/dev/sdc on /mnt/movies type ext4 (rw,relatime)
michal@ubuntu:~$
  1. Why mount | grep sdb returns no output?? This is my [SWAP] partition.

Then I've run:

michal@ubuntu:~$ mount | grep sd?
michal@ubuntu:~$
michal@ubuntu:~$
  1. Why mount | grep sd? returns no output??
Score:3
cn flag

swap is not a filesystem, it's not mounted, it's activated or deactivated (see man swapon). A mountpoint does not exist. In /etc/fstab the mountpoint is specified as none. Since swap is not mounted, I'd not expect the swap partition to appear in the output of mount or findmnt.

mount | grep sdb gives no output because the string sdb does not appear in the mount-output. Remember, /dev/sdb is your swap, it is not mounted.

mount | grep sd? gives no output because the string sd? does not appear in your mount-output. A drive named sd? does not exist, as simple as that. If you want grep to treat sd? as an extended regex, you'd use mount | grep -E sd? and your output wouldn't be empty but it probably wouldn't be what you expect since the meaning of the ? is very different in regex and globbing.

mount | grep "/dev/sd" is what gives you the output you desire here.


Some more details:

From info grep chapter 2.4 'grep' programms:

’-G’
‘--basic-regexp’
     Interpret patterns as basic regular expressions (BREs).  This is
     the default.

From info grep chapter 3.6 Basic vs Extended Regular Expressions:

Basic vs Extended Regular Expressions
       In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose
       their special meaning; instead use  the  backslashed  versions
       \?, \+, \{, \|, \(, and \).

    Portable scripts should avoid the following constructs, as POSIX says
    they produce undefined results:
    
    ...
    • Basic regular expressions that use ‘\?’, ‘\+’, or ‘\|’.
    ...

But even when I use \? it does not work as expected for me, mount | grep sd\? still gives empty output which definitely shouldn't be the case. It only gives me expected output with the -E-option, with or without the backslash. Probably this is a bug.

Archemar avatar
cn flag
on a side note `mount | grep /dev/sd?` will give an error, as it expand to `mount | grep /dev/sda /dev/sdb /dev/sdc`
raj avatar
cn flag
raj
`grep` uses regular expressions by default, you do not have to specify the `-e` parameter. As the manpage for `grep` says, the `-e` parameter is "used to specify multiple search patterns, or to protect a pattern beginning with a hyphen". Otherwise it is not needed.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.