#!/bin/sh usage () { echo "zdtol [options] " echo " Searches a label for partition or disk /dev/" echo " Options: " echo " -h Show this message then exit" echo " -d Discard prefix (/dev/)" echo " -n Show only nth label" } error () { printf "\033[1;31m%s\033[0m\n" "$1" >&2 } while getopts ":hdn:" opt; do case $opt in h) usage exit 0 ;; n) if ! echo "$OPTARG" | grep -Eq '^[0-9]+$' then error "n argument has to be an integer" exit 2 fi if [ -z "$OPTARG" ] then error "n needs an argument" exit 2 fi if [ "$OPTARG" -lt 1 ] then error "argument has to be greater than 0" exit 2 fi _arg_n=$OPTARG _opt_n=y ;; d) _opt_d=y ;; \?) echo "Unknown option: $OPTARG" >&2 usage exit 1 ;; esac done shift $((OPTIND-1)) if [ $# -le 0 ] then usage exit 2 fi LIST=$(lsblk -lnio NAME,LABEL | tr -s ' ') lsize=$(echo "$LIST" | wc -l) for I do [ -n "$_opt_d" ] && I=${I#/dev/} ret=$(echo "$LIST" | grep "^$I" | awk '{if($2){$1="";for(i=2;i