Find the answer to your Linux question:
Results 1 to 3 of 3
Hey everyone, So I'm writing a shell script to use LSHW to pull only specific information about a system for me, but I need to get the IF statement to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Location
    Minnesota, USA
    Posts
    4

    use if to test for YES or Y, but case insensitive


    Hey everyone,

    So I'm writing a shell script to use LSHW to pull only specific information about a system for me, but I need to get the IF statement to compare to four "words" and be case-insensitive for all.

    Ex.

    Code:
    #!/bin/bash
    echo 'Enter the three digit store code'
    echo
    read store
    echo
    echo
    echo 'Is '$store' correct?'
    echo
    read stans
    echo
    if [ $stans = "yes" OR "y" (insensitive) ]
       then
           //Code
       elif [$stans = "no" OR "n" (insensitive)]
          //Code
       else
          //Code
    fi
    Anyone have any input? Using bash as the shell for the script

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    One easy way to do it might be:
    Code:
    if echo "$stans" | grep -qiE "^y(es)?$"; then
        # Yes
    elif echo "$stans" | greq -qiE "^no?$"; then
        # No
    else
        # Other
    fi
    Here, we're using grep to match patterns against the value of $stans. "-q" means "quiet", and will use the return value to determine whether or not the match was successful. Note that we do not use [ ... ] when we use grep -q; we simply put the grep into the if statement directly.

    "-i" means case-insensitive match, which handles the many possibilities of cases.

    "-E" means to use extended regular expressions, which enables the "?" character in the expression. This makes it easy to match either "yes" or "y".

    Does this make sense?

  3. #3
    Just Joined!
    Join Date
    Jul 2011
    Location
    Minnesota, USA
    Posts
    4

    Perfect!

    Thank you for the help Cabhan!! I've got another one for ya.

    What I'm doing now is using a combination of
    [code]
    cat -n /tmp/disk | grep "*-" | awk '{print $1}' | sed 's/://g' > /tmp/num
    1
    8
    20
    [/code

    ]cat -n /tmp/disk to print file with line numbers, grep to select the lines with the "headers" of *-(any), awk to print just the line number with the colon and sed to remove the colon and output the pure line numbers to a file formatted as such

    Essentially, I'm trying to take this:
    Code:
         1  *-disk
         2       description: SCSI Disk
         3       physical id: 0.0.0
         4       bus info: scsi@0:0.0.0
         5       logical name: /dev/sda
         6       size: 3827MiB (4012MB)
         7       capabilities: partitioned partitioned:dos
         8  *-disk
         9       description: ATA Disk
        10       product: ST3160815A
        11       vendor: Seagate
        12       physical id: 0
        13       bus info: ide@0.0
        14       logical name: /dev/hda
        15       version: 3.AAD
        16       serial: 9RAA4N9W
        17       size: 149GiB (160GB)
        18       capabilities: ata dma lba iordy smart security pm partitioned parti
    tioned:dos
        19       configuration: mode=udma5 signature=0004b15b smart=on
        20  *-cdrom
        21       description: DVD reader
        22       product: DVD-ROM DDU1621
        23       physical id: 0
        24       bus info: ide@1.0
        25       logical name: /dev/hdc
        26       version: VER S3.1
        27       capabilities: packet atapi cdrom removable nonmagnetic dma lba iord
    y audio dvd
        28       configuration: mode=udma2 status=open
    and split it up into multiple files based on the headers on line 1, 8, and 20. Problem is, there could be more or less than 3 headers with *- depending on the system.

    For example:

    the first *-disk > /tmp/disk0
    the second *-disk > /tmp/disk1
    *-cdrom > /tmp/cdrom

    Any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •