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
joeyg
April 10, 2013, 1:46pm
2
Why not?
if [ $ADAP = "fcs" ];then
Yoda
April 10, 2013, 3:14pm
4
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?
joeyg
April 10, 2013, 3:27pm
5
$ 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
Yoda
April 10, 2013, 3:44pm
7
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
kshji
April 11, 2013, 12:18pm
10
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