Grep Command Help

Hello Unix Forum,

I am trying to create an bash script to expand a LVM disk, I mean, trying to automatize all the process from the video bellow:
Youtube search:

How To Extend LVM Disk For Linux Virtual Machine On VMware

At this point, I am very new in bash script and I am facing this just as a try, I don't know if I will get the final result that I need.

Well, I am stuck at the grep option bellow:

#!/bin/bash
# Increase LVM Disk
echo"$(fdisk -l)"
echo Which disk you want to increase the size? "(Please provide the full path, Example: /dev/sda2):"
read disk1
diskcompare="$(fdisk -l | grep "$disk1 " -o --max-count=1)"
if [ "$diskcompare" ==  "$disk1 " ]; then
echo"Valid Disk."
else
echo"Invalid Disk."
fi

I would like to compare the path (disk1) with the fdisk -l command to check if it is a valid disk.

The problem is that the way that it is writed at the moment it accept you to continue the script if you not type the entire path, example:

[root@linuxtest ~]# ./test.sh

Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d345c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    16777215     7339008   8e  Linux LVM
/dev/sda3        16777216    31457279     7340032   8e  Linux LVM

Disk /dev/mapper/centos-root: 14.2 GB, 14164164608 bytes, 27664384 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 859 MB, 859832320 bytes, 1679360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Which disk you want to increase the size? (Please provide the full path, Example: /dev/sda2):
/sda2
Valid Disk.
[root@linuxtest ~]#

It should match only the exactly values /dev/sda1 , /dev/sda2 or /dev/sda3.

Any help will be welcomed :slight_smile:

Ty!

Hi, try:

grep "^${disk1} " -o --max-count=1 
1 Like

It worked, can you explain a little bit why?

Regards,

how about a little bit more "interesting" - with the menu:

#!/bin/bash

menu=$(fdisk -l | awk '/Device Boot/ {for(i=2;i<=NF;i++) print substr($i,1, index($i, " ")-1)}' RS= FS='\n';echo 'quit')

PS3='Which disk you want to increase the size?'

     select choice in ${menu}
     do
     case "$choice" in
          "")
               echo "You selected a non-existing item"
               ;;

          "quit")
               echo "You are leaving the loop"
               break
               ;;

          *)
               echo "You selected $choice"
               ;;
          esac

     done
1 Like

wow, I really liked the menu, trying to undestand the lines and how you done that!

Ty!

The caret sign ( ^ )means that the expression only matches text at the beginning of the line.
There already had be a space at the end.
These two measures make that a partial match will not work.