If condition matching with special chars

Hi,

I have file

#cat drivers.txt
fcs0
fcs1
vscsi1
vscsi2

In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1

I tried with "if condition" but it is not working.

cat drivers.txt| while read ADAP
do
echo "Checking for $ADAP"
if [ $ADAP = fcs* ];then
 echo "FC adapter is availabe"
 fi
done

Anyone, pls advice

Why not?

if [ $ADAP = "fcs" ];then

Hi Joeyg,

No, its not working.

First of all tell us what is your OS and SHELL:

uname
echo $SHELL

Also show us what output you expect from your input file?

$ cat sample10.txt
fcs0
fcs1
vscsi1
vscsi2

$ cat sample10.txt | while read ADAP; do if [ "$ADAP" = "fcs0" ]; then  echo "yes, FCS is available"; fi; done
yes, FCS is available


$ cat sample10.txt | while read ADAP; do if [ "$ADAP" = "fcs" ]; then  echo "yes, FCS is available"; fi; done

Hi Yoda,

The OS is Linux and Shell is Bash.

Understand, "joeyg" is providing correct detail

Regards,
Siva

In bash version >= 3, you can use built-in regular expression comparison operator =~

#!/bin/bash

while read ADAP
do
        if [[ "$ADAP" =~ fcs.* ]]
        then
                printf "FC adapter: $ADAP is available\n"
        fi
done < drivers.txt
$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
1 Like

Hi Yoda,

Opened my eyes, much thanks for your trick.

Regards,
Siva

---------- Post updated 04-11-13 at 03:22 AM ---------- Previous update was 04-10-13 at 03:10 PM ----------

Hi Yoda,

But in AIX it doesn't working properly. It throws the below error.

Syntax error at line 4 : `=~' is not expected

Regards,
Siva

Why not use grep?

grep -q "fcs" drivers.txt && echo "FC is available"

If your grep doesn't support -q option, redirect the output

grep "fcs" drivers.txt >/dev/null 2>&1 && echo "FC is available"

--ahamed

Case is always interesting cmd to make pattern testing. Dash, sh, ksh, bash, zsh, ...

while read "$ADAP"
do
        case "$ADAP" in
                        fcs*)  echo "FC adapter: $ADAP is available" ;;
                        *scsi*) echo "Some scsi $ADAP ... " ;;
                        *) echo adapter $ADAP unknown " ;;
        esac
done < drivers.txt

Or update aix ksh88 to ksh93
Download ksh